在我的xml文件中
<a>
<test id="bk1">
<go>B</go>
<from>A</from>
<test/>
<test id="bk2">
<go>C</go>
<from>D</from>
<test/>
<test id="bk2">
<go>D</go>
<from>E</from>
<test/>
</a>
所以我想选择go和from值并创建一个元素调用$ elements
预期产出:
<city>A</city>
<city>B</city>
<city>C</city>
<city>D</city>
<city>E</city>
在我的xquery查询中:
for $a in /a/test
for $elements in distinct-values($go) //How to add from into elements ?
return
<city>
{$elements}
</city>
答案 0 :(得分:2)
for $a in distinct-values(/a/test/(go|from))
return <city>{ $a }</city>
答案 1 :(得分:2)
distinct-values($sequence)
只接受单个序列变量。但是在XQuery中连接序列非常容易,因为它们会自动变平(XQuery中没有嵌套序列):
((1, 2, 3), (4, 5, 6))
得
(1, 2, 3, 4, 5, 6)
您可以通过将第一行作为XQuery执行来轻松地重现这一点。
应用于distinct-values
和您的问题,值,执行
distinct-values($a/test/(go, from))
(并将其包装在你已经拥有的循环中)。