0

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:

  • Must receive two parameters: the XML document (String) and the XQuery expression (String).
  • Must return the result of the XQuery evaluation (String).
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.

cespon
  • 5,630
  • 7
  • 33
  • 47
  • 2
    As Mike told you in his answer, SaxonJS doesn't support XQuery 3.1. https://github.com/FontoXML/fontoxpath claims to support it, however. – Martin Honnen Apr 22 '23 at 13:08

1 Answers1

1

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
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • I will have to look for another library. The function is for an education tool and XQuery is a requirement. Thanks for your reply. – cespon Apr 22 '23 at 14:16
  • The only project I know of is XQIB, but I think it hasn't been updated for many years and is unlikely to support XQuery 3.1. Let us know if you find anything. – Michael Kay Apr 23 '23 at 10:35
  • I just learned via Martin that FontoXPath has some XQuery support. Incomplete, and the exact conformance level is not well documented, but unlike XQIB it's an active and lively project. – Michael Kay Apr 23 '23 at 10:50