英文:
How to drop the duplicate node from input xml using xquery
问题
以下是XQuery中的XML输入,我想要删除重复的元素
let $doc := <a>
<b>
<b1/>
</b>
<c>
<c1/>
<c1/>
</c>
<b>
<b1/>
</b>
<d>
<d1>
<d11/>
<d11/>
</d1>
</d>
</a>
预期输出:我的预期输出如下,意思是从输入中删除重复的节点
<a>
<b>
<b1/>
</b>
<c>
<c1/>
</c>
<d>
<d1>
<d11/>
</d1>
</d>
</a>
谢谢。
英文:
Below are the Xml input in xquery, i want to drop the duplicate element
let $doc := <a>
<b>
<b1/>
</b>
<c>
<c1/>
<c1/>
</c>
<b>
<b1/>
</b>
<d>
<d1>
<d11/>
<d11/>
</d1>
</d>
</a>
return distinct-values($doc)
expected output: my expected output is below means drop the duplicate node from input
<a>
<b>
<b1/>
</b>
<c>
<c1/>
</c>
<d>
<d1>
<d11/>
</d1>
</d>
</a>
Thanks..
答案1
得分: 1
distinct-values
函数创建一个包含不同原子值(如字符串、数字、日期)的序列。要识别和消除重复节点,首先需要理解如何定义一个节点是另一个节点的重复项,无论是仅基于子元素结构还是值。
您似乎还希望递归消除重复项,因此,为了给您一个示例,使用了一个递归函数,该函数将所有子元素按照调用XQuery 3中的serialize
函数的结果进行分组。以下是示例代码:
declare function local:eliminate-duplicates($element as element())
{
element { $element => node-name() }
{
for $child at $child-pos in $element/*
group by $xml := serialize($child)
order by head($child-pos)
return
$child => head() => local:eliminate-duplicates()
}
};
let $a := <a>
<b>
<b1/>
</b>
<c>
<c1/>
<c1/>
</c>
<b>
<b1/>
</b>
<d>
<d1>
<d11/>
<d11/>
</d1>
</d>
</a>
return
local:eliminate-duplicates($a)
结果可以在以下链接中找到:https://xqueryfiddle.liberty-development.net/gWmuPrX/2
结果是:
<a>
<b>
<b1/>
</b>
<c>
<c1/>
</c>
<d>
<d1>
<d11/>
</d1>
</d>
</a>
英文:
The function distinct-values
creates a sequence of distinct atomic values like strings, numbers, dates. To identify and eliminate duplicate nodes, we first need to understand how you define that a node is a duplicate of another, whether that is based solely on the child element structure or the values as well.
You also seem to want to eliminate duplicates recursively, so to give you an example that uses a recursive function that groups all children by the result of calling the serialize
function in XQuery 3 here is a sample:
declare function local:eliminate-duplicates($element as element())
{
element { $element => node-name() }
{
for $child at $child-pos in $element/*
group by $xml := serialize($child)
order by head($child-pos)
return
$child => head() => local:eliminate-duplicates()
}
};
let $a := <a>
<b>
<b1/>
</b>
<c>
<c1/>
<c1/>
</c>
<b>
<b1/>
</b>
<d>
<d1>
<d11/>
<d11/>
</d1>
</d>
</a>
return
local:eliminate-duplicates($a)
Result at https://xqueryfiddle.liberty-development.net/gWmuPrX/2 is
<a>
<b>
<b1/>
</b>
<c>
<c1/>
</c>
<d>
<d1>
<d11/>
</d1>
</d>
</a>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论