[英]XQuery Nodes with identic attribute values
I have an XML Document of the following Structure. 我有以下结构的XML文档。
<Content>
<Function Name="A">
<SubNode1>
<SubNode2>
</SubNode2>
</SubNode1>
</Function>
<Function Name="B">
<SubNode1>
<SubNode2>
</SubNode2>
</SubNode1>
</Function>
<Function Name="A">
<SubNode1>
<SubNode2>
</SubNode2>
</SubNode1>
</Function>
Now I want to display all functions with XQuery, using this code. 现在,我想使用此代码显示XQuery的所有功能。
for $currentFunction in $node where local-name($currentFunction) = 'Function'
return
(
<Function>
{
local:printAttributes($currentFunction)
}
</Function>
)
This works fine. 这很好。 But I want to display only functions with different Name attributes. 但是我只想显示具有不同Name属性的函数。 It should only show me Function with Name A und the one with Name B, the last function with name A should not be included. 它只应显示名称为A的功能和名称为B的功能,不应包含名称为A的最后一个功能。
How can i do this? 我怎样才能做到这一点? Any suggestions? 有什么建议么? I'm using XQuery 1.0 我正在使用XQuery 1.0
You can use group by
to find functions with the same name: 您可以使用group by
查找具有相同名称的函数:
for $func in /Content/Function
let $name := $func/@Name
group by $name
let $last-func := ($func/.)[last()]
return <Function Name="{$name}">{
local:printAttributes($last-func)
}</Function>
Since the order inside groups is not standardized, you need to use a self step /.
由于组内的顺序未标准化,因此您需要使用自步/.
in $last-func
to sort the elements in document order . 在$last-func
中将元素按文档顺序排序 。
In XQuery 1.0 you can simulate group by
by using fn:distinct-values(...)
: 在XQuery 1.0中,您可以使用fn:distinct-values(...)
模拟group by
:
for $name in distinct-values(/Content/Function/@Name)
let $funcs := /Content/Function[@Name = $name]
let $last-func := $funcs[last()]
return <Function Name="{$name}">{
local:printAttributes($last-func)
}</Function>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.