Home > Back-end >  I get an error when I try to execute an XQuery statement
I get an error when I try to execute an XQuery statement

Time:09-17

My intention is to get the count of all the unique titles in an XML file (like the one shown below). However, I get an error as such:

Uncaught DOMException: Document.evaluate: The expression is not a legal expression

Cannot figure out what went wrong.

<channel>
<item>
<title>Trans Researchers Want Google Scholar to Stop Deadnaming Them</title>
<link>https://www.wired.com/story/trans-researchers-want-google-scholar-to-stop-deadnaming-them/</link>
<description>The academic search engine’s policy on name changes is out of step with other search tools and publishers.</description>
<category>Business</category>
<thumbnail>https://media.wired.com/photos/630fe159567860ab6c66c667/master/pass/business-deadnames-google.jpg</thumbnail>
</item>
<item>
<title>Babylon Disrupted the UK’s Health System. Then It Left</title>
<link>https://www.wired.com/story/babylon-disrupted-uk-health-system-then-left/</link>
<description>The AI-powered online doctor app is ditching its controversial NHS contracts as it focuses on the US market.</description>
<category>Business</category>
<thumbnail>https://media.wired.com/photos/63042ca2f06d7dee6f53bbc6/master/pass/Babylon-Leaves-NHS-Business-1253255825.jpg</thumbnail>
</item>
</channel>

My JavaScript code is as follows:

function displayCount() {
        // get the number of distinct titles using xquery
        var xquery = "count(distinct-values(/channel/item/title))";
        var count = xmlFile.evaluate(xquery, xmlFile, null, XPathResult.ANY_TYPE, null);
        //document.getElementById("count").innerHTML = count.textContent;
        console.log(count.numberValue);
}

What is the reason for the error and how do I correct it?

CodePudding user response:

The XPath implementation in current browsers is usually an XPath 1.0 implementation; and distinct-values is a function introduced in XPath 2.0.

You can, however, run XPath 3.1 in the browser using Saxon-JS from Saxonica (also see https://www.saxonica.com/download/javascript.xml for download):

const xml = `<channel>
<item>
<title>Trans Researchers Want Google Scholar to Stop Deadnaming Them</title>
<link>https://www.wired.com/story/trans-researchers-want-google-scholar-to-stop-deadnaming-them/</link>
<description>The academic search engine’s policy on name changes is out of step with other search tools and publishers.</description>
<category>Business</category>
<thumbnail>https://media.wired.com/photos/630fe159567860ab6c66c667/master/pass/business-deadnames-google.jpg</thumbnail>
</item>
<item>
<title>Babylon Disrupted the UK’s Health System. Then It Left</title>
<link>https://www.wired.com/story/babylon-disrupted-uk-health-system-then-left/</link>
<description>The AI-powered online doctor app is ditching its controversial NHS contracts as it focuses on the US market.</description>
<category>Business</category>
<thumbnail>https://media.wired.com/photos/63042ca2f06d7dee6f53bbc6/master/pass/Babylon-Leaves-NHS-Business-1253255825.jpg</thumbnail>
</item>
</channel>`;

var doc = new DOMParser().parseFromString(xml, 'application/xml');


function displayCount() {
        // get the number of distinct titles using XPath
        var xpath = "count(distinct-values(/channel/item/title))";
        var count = SaxonJS.XPath.evaluate(xpath, doc);
        console.log(count);
}
<script src="https://martin-honnen.github.io/xslt3fiddle/js/SaxonJS2.js"></script>

<input type="button" value="test XPath 3.1" onclick="displayCount()"/>

  • Related