简单的例子:
sample.xml
<customers>
<customer>
<name>Tony</name>
<order>Burger</order>
<id>1234</id>
</customer>
<customer>
<name>Kate</name>
<order>Soda</order>
<id>5678</id>
</customer>
</customers使用XQuery
for $a in distinct-values(doc("sample.xml")/customers/customer)
return
$y将会返回
Tony
Burger
1234
Kate
Soda
5678但是,如果我这样做了:
for $a in distinct-values(doc("sample.xml")/customers/customer)
return
$y/name为了只返回名字,它抛出了一个错误。有谁能解释一下原因吗?我想要访问某些值,而不是整个树。同样,如果我尝试用$y/(某物)做任何事情,比如在where表达式中使用它。
更新:通过使用distinct-values()作为返回表达式,我想出了在某种程度上绕过整体问题的方法,但是我仍然不明白为什么不允许我访问distinct-values for循环中的子元素
发布于 2014-05-23 00:09:37
distinct- values ()将把输入转换成原子值。这意味着只返回内容,因此xml表示将丢失。
如果你真的想这样做,你可以使用像这样的定制函数: functx:distinct-deep:http://www.xqueryfunctions.com/xq/functx_distinct-deep.html
但请记住,在大多数情况下,您不希望这样做,这可能会对性能造成很大的影响
https://stackoverflow.com/questions/23811792
复制相似问题