XQuery method question, trying to sum values read from xml
Posted
by Buck
on Stack Overflow
See other posts from Stack Overflow
or by Buck
Published on 2009-12-11T06:37:48Z
Indexed on
2010/03/16
17:01 UTC
Read the original article
Hit count: 419
xquery
I'm pretty new to XQuery and I'm trying to write an example function that I can't get to work. I want to read an xml file, parse out the "time" values, sum them as they're read and return the sum. This is trivial and I'm looking to build more functionality into it but I'd like to get this working first. Also, I know there's a "sum" directive in XQuery that would do just this but I want to add more to it so the built-in sum is insufficient for my needs.
Here's my funtion:
bool example(Zorba* aZorba) { XQuery_t lQuery = aZorba->compileQuery( "for $i in fn:doc('/tmp/products.xml')//time" "let $sum := xs:integer($i)" " return $sum" ); DynamicContext* lCtx = lQuery->getDynamicContext(); lCtx->setContextItemAsDocument("temp_measurements.xml", lDocStream); try { std::cout << lQuery << std::endl; } catch (DynamicException& e) { std::cerr << e.getDescription() << std::endl; return false; } catch (StaticException& f){ std::cerr << f.getDescription() << f.getErrorCodeAsString(f.getErrorCode()) << std::endl; return false; } }
It's called with an appropriate main(). If I comment out the line that starts "let $sum..." then this works in that it returns the time values as a series of integers like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3....
Input file looks like this:
<?xml version="1.0" encoding="UTF-8"?> <temps> <temp> <time>0</time> <lat>0</lat> <long>0</long> <value>0</value> </temp> <temp> <time>1</time> <lat>0</lat> <long>1</long> <value>0</value> </temp> ...
© Stack Overflow or respective owner