I get an error when I try to execute an XQuery statement

S. Tiss

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?

Martin Honnen

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()"/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I get 404 error when I try endpoint on localhost

Why do I get an error for "__CrtGetFileInformationByHandleEx " when I try to compile

I get stray '#' in program error when I try to compile this program

Why I get error when I try to create stored procedure?

When i try to create react project i get error

I get error when i try to add value to defaultdict(str)

I get and error when I try to do pip install

I get an Error when I try to use Firebase Auth

When I try to use scanf with 2 arrays I get an error

I get an error when I try to finish an arguement in Android Studio

Error I get when I try install IDE Anjuta

Why when I try to execute a program as normal user I get denied, and when I execute it with sudo I get "Not Found"

I get an error when I try to initiate the front camera (Swift)

When I try to CURL a website I get SSL error

Why do I get the error "Thrift::TException=HASH(0x122b9e0)" when I try to execute a statement with Thrift::API::HiveClient?

I get this error when I try to install

Error when I try to execute a script: ImportError: No module named tools

I get an error when I try to show the comments for each post

I get the error when i try get 50% of the image with cropperJS

Why I get error when I try to install pyaudio?

When I try to sync my Gradle files I get an error

Lua: I get error when I try to run a script in Windows

I get a syntax error when I try to send a discord message

Why I get error when I try to declare local variable?

Why I get an error when I try to upload a picture

When I try to define settings, I get a different error

When I try to declare a global variable it throws the error Statement expected

I get an error when i try apt-get update?

Java is Throwing Syntax Error When I Try to Execute an SQL Query