【问题标题】:Xquery with multiple element with distinct clause具有不同子句的多个元素的 Xquery
【发布时间】:2014-06-09 15:12:05
【问题描述】:

在我的 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>

【问题讨论】:

    标签: xml xquery


    【解决方案1】:
    for $a in distinct-values(/a/test/(go|from))
    return <city>{ $a }</city>
    

    【讨论】:

      【解决方案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))
      

      (并将其包装在您已有的循环中)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-29
        • 1970-01-01
        • 2020-03-16
        • 1970-01-01
        • 2019-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多