Reputation: 5760
I need to create a Javascript function that allows to get the result of evaluating an XQuery 3.1 expression against an XML.
The function:
function evaluateXQuery(xmlStr, xqueryStr) {
// XQuery 3.1 evaluation
return resultStr;
}
Example:
// XML Document
const xml =
`<bookstore>
<book>
<title>XQuery Kick Start</title>
<price>30.00</price>
</book>
<book>
<title>Harry Potter</title>
<price>24.99</price>
</book>
</bookstore>`
// XQuery
const xquery =
`for $x in /bookstore/book
where $x/price>25
return $x/title`;
// XQuery 3.1 evaluation
const output = evaluateXQuery(xml, xquery);
// Output
console.log(output); // "<title>XQuery Kick Start</title>"
I used SaxonJS 2.5 (browser version) to evaluate XPath expressions, but I can't find anything in the documentation regarding the evaluation of XQuery queries.
To evaluate XPath expressions I made the following function:
async function evaluteXPath(xmlStr, xpathStr) {
const doc = await SaxonJS.getResource({
text: xmlStr,
type: 'xml'
});
return SaxonJS.XPath.evaluate(xpathStr, doc);
}
I also made a similar function to perform XSLT transformations using SaxonJS.XPath.evaluate
.
I am looking for something similar to the above, but applied to XQuery.
Any help is welcome.
Upvotes: 0
Views: 170
Reputation: 163322
SaxonJS does not include XQuery support: only XSLT 3.0 and XPath 3.1.
Your particular example query can of course be written easily in XPath:
/bookstore/book[price>25]/title
Upvotes: 1