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
<time0</time
<lat0</lat
<long0</long
<value0</value
</temp
<temp
<time1</time
<lat0</lat
<long1</long
<value0</value
</temp
...