cespon
cespon

Reputation: 5760

XQuery 3.1 evaluation using SaxonJS 2.5 (browser)

What I want

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>"

What I did

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

Answers (1)

Michael Kay
Michael Kay

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

Related Questions