我的目的是获得一个XML文件(如下图所示)中所有唯一标题的计数.但是,我收到如下错误:

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

找不到哪里出了问题.

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

我的JavaScript代码如下所示:

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);
}

错误的原因是什么?我如何更正它?

推荐答案

当前浏览器中的XPath实现通常是XPath 1.0实现;distinct-values是XPath 2.0中引入的函数.

但是,您可以使用Saxon-JS from Saxonica在浏览器中运行XPath 3.1(请参阅https://www.saxonica.com/download/javascript.xml下载):

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

Javascript相关问答推荐

我们如何从一个行动中分派行动

空的结果抓取网站与Fetch和Cheerio

你怎么看啦啦队的回应?

Prisma具有至少一个值的多对多关系

在浏览器中触发插入事件时检索编码值的能力

如何在Svelte中从一个codec函数中调用error()?

当用户点击保存按钮时,如何实现任务的更改?

如何在JAVASCRIPT中合并两组对象并返回一些键

回溯替代方式

在ChartJS中使用spanGaps时,是否获取空值的坐标?

如何有条件地返回Java脚本中的函数?

Reaction Chart.js为显示格式化日期的xax提供相同的差距

Electron -如何指向ASAR生成的应用程序之外的路径

在每次迭代中,JavaScript for循环会重新声明变量吗?

删除帖子的 comments 失败?

如何 Select 名称正在更改的DOM元素

异步组件DidMout和forceUpdate

Mongoose updateOne()不能传入选项

如何延迟Java脚本滚轮事件

在Next.js v12.3.1和Reaction v18.2.0";中缩小了REACT错误#418和#423