You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写XQuery语句提取title含指定子串的所有item节点?

提取XML中包含指定子串的item节点(XQuery+JavaScript实现)

需求

从给定XML中提取所有<title>节点包含指定子串的<item>节点,例如子串为"rupted"时返回对应的item节点。

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>

XQuery语句

下面的XQuery定义外部变量接收搜索子串,筛选符合条件的item节点:

declare variable $search-substring as xs:string external;
/channel/item[contains(title, $search-substring)]

JavaScript实现示例

借助xmldom解析XML,xquery-js执行XQuery查询:

const { DOMParser } = require('xmldom');
const xquery = require('xquery-js');

// XML内容
const xmlContent = `
<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>
`;

// 解析XML文档
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlContent, 'application/xml');

// 目标搜索子串
const targetSubstring = 'rupted';

// 执行XQuery查询,传入外部变量
const matchedItems = xquery(xmlDoc, `
declare variable $search-substring as xs:string external;
/channel/item[contains(title, $search-substring)]
`, { 'search-substring': targetSubstring });

// 输出结果
console.log(matchedItems.toString());

预期结果

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

内容的提问来源于stack exchange,提问作者S. Tiss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 10:11:07