如何删除XQuery中的重复节点? [英] How can I remove duplicate nodes in XQuery?
问题描述
我的函数看起来像:
declare function local:start2(){
let $ data:= local:scan_books()
return<书籍> {$数据}< /书籍>
};
示例输出是:
<书籍>
< book>
< title> XML在24小时< / title>
< author> Some Guy< / author>
< / book>
< book>
< title> XML在24小时< / title>
< author> Some Guy< / author>
< / book>
< / books>
我只想在我的书籍根标记中输入一个条目,还有其他标签,如说小册子在那里也需要删除重复。任何想法?
更新了以下评论。通过唯一节点,我的意思是删除多个具有完全相同内容和结构的节点。
更简单和更直接的一线XPath解决方案:
只需使用以下XPath表达式:
/ * / book
[index / of(/ * / book / title,
title
)
[1]
]
应用于例如以下XML文件
: < books>
< book>
< title> XML在24小时< / title>
< author> Some Guy< / author>
< / book>
< book>
< title>西雅图食品< / title>
< author> Some Guy2< / author>
< / book>
< book>
< title> XML在24小时< / title>
< author> Some Guy< / author>
< / book>
< book>
< title>西雅图食品< / title>
< author> Some Guy2< / author>
< / book>
< book>
< title>如何解决XPAth问题< / title>
< author> Me< / author>
< / book>
< / books>
上述XPath表达式正确选择以下节点:
< book>
< title> XML在24小时< / title>
< author> Some Guy< / author>
< / book>
< book>
< title>西雅图食品< / title>
< author> Some Guy2< / author>
< / book>
< book>
< title>如何解决XPAth问题< / title>
< author> Me< / author>
< / book>
解释很简单:对于每个书
,仅选择其中一个出现 - 使其全书中的索引与标题$的第一个索引相同c $ c>在全部标题。
I have an XML document I generate on the fly, and I need a function to eliminate any duplicate nodes from it.
My function looks like:
declare function local:start2() {
let $data := local:scan_books()
return <books>{$data}</books>
};
Sample output is:
<books>
<book>
<title>XML in 24 hours</title>
<author>Some Guy</author>
</book>
<book>
<title>XML in 24 hours</title>
<author>Some Guy</author>
</book>
</books>
I want just the one entry in my books root tag, and there are other tags, like say pamphlet in there too that need to have duplicates removed. Any ideas?
Updated following comments. By unique nodes, I mean remove multiple occurrences of nodes that have the exact same content and structure.
A simpler and more direct one-liner XPath solution:
Just use the following XPath expression:
/*/book
[index-of(/*/book/title,
title
)
[1]
]
When applied, for example, on the following XML document:
<books>
<book>
<title>XML in 24 hours</title>
<author>Some Guy</author>
</book>
<book>
<title>Food in Seattle</title>
<author>Some Guy2</author>
</book>
<book>
<title>XML in 24 hours</title>
<author>Some Guy</author>
</book>
<book>
<title>Food in Seattle</title>
<author>Some Guy2</author>
</book>
<book>
<title>How to solve XPAth Problems</title>
<author>Me</author>
</book>
</books>
the above XPath expression selects correctly the following nodes:
<book>
<title>XML in 24 hours</title>
<author>Some Guy</author>
</book>
<book>
<title>Food in Seattle</title>
<author>Some Guy2</author>
</book>
<book>
<title>How to solve XPAth Problems</title>
<author>Me</author>
</book>
The explanation is simple: For every book
, select only one of its occurences -- such that its index in all-books is the same as the first index of its title
in all-titles.
这篇关于如何删除XQuery中的重复节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!