Xquery 中的双重追加

发布于 2024-09-16 06:12:55 字数 1620 浏览 11 评论 0原文

我想附加一些 XML 标记的值,然后再次附加结果。但我在附加结果时遇到了一些麻烦,所以我只有一个最终的附加结果。

想象一下你有这样的情况:

<nodeA>
<nodeB>
 <item>1</item>
 <item>2</item>
 <yesno>blah</yesno>
</nodeB>
<nodeC>
 <thing>A</thing>
</nodeC>
</nodeA>

<nodeA>
<nodeB>
 <item>3</item>
 <item>4</item>
 <yesno>blah</yesno>
</nodeB>
<nodeC>
 <thing>B</thing>
</nodeC>
</nodeA>

我想得到这个结果A,1,2,B,3,4 但我得到了这个: A,1,2B,3,4

我一直在读这个:XQuery:如何在 for 循环中正确附加 , 但我仍然不能让它发挥作用。这是我的代码示例。

let $contador := count(
    for $x in $productDoc//*[substring(local-name(.),2,23)="nodeB"]
    where not(exists ($x/ns0:yesno))
    return
     $x)

 return
    if ($contador = 0) then
      <result> </result>
    else
      for $x in $productDoc//*[substring(local-name(.),2,18)="nodeB"]
      where not(exists ($x//ns0:yesno))
      return
          for $y in $x//*[matches(name(.),'thing')]
          let $z := $x//*[matches(name(.),'item')]
          return
            for $i in $y
            let $c := concat($i,",",string-join($z,","))
            return          
                let $d := string-join($c,",")
                <result>{ $d }</result>

我想发生的事情是我错误地附加了部分结果......或者附加在错误的位置,但我不知道在哪里做。也许我应该从另一个对结果进行字符串连接的函数调用此函数。你觉得怎么样?

I want to append the values of some XML tags, and then append again the result. But I'm having some trouble appending the results, so I only have one final result of appending.

Imagine you have this:

<nodeA>
<nodeB>
 <item>1</item>
 <item>2</item>
 <yesno>blah</yesno>
</nodeB>
<nodeC>
 <thing>A</thing>
</nodeC>
</nodeA>

<nodeA>
<nodeB>
 <item>3</item>
 <item>4</item>
 <yesno>blah</yesno>
</nodeB>
<nodeC>
 <thing>B</thing>
</nodeC>
</nodeA>

I want to get this result <result>A,1,2,B,3,4</result>
But I get this one: <result>A,1,2</result><result>B,3,4</result>

I've been reading this: XQuery: how to properly append , in for loop but still I can't make it work. Here is the sample of my code.

let $contador := count(
    for $x in $productDoc//*[substring(local-name(.),2,23)="nodeB"]
    where not(exists ($x/ns0:yesno))
    return
     $x)

 return
    if ($contador = 0) then
      <result> </result>
    else
      for $x in $productDoc//*[substring(local-name(.),2,18)="nodeB"]
      where not(exists ($x//ns0:yesno))
      return
          for $y in $x//*[matches(name(.),'thing')]
          let $z := $x//*[matches(name(.),'item')]
          return
            for $i in $y
            let $c := concat($i,",",string-join($z,","))
            return          
                let $d := string-join($c,",")
                <result>{ $d }</result>

I suppose what is happening is that I'm appending the partial results wrongly... or in a wrong place, but I don't know where to do it. Maybe I should call this function from another one that makes a string-join of the result. What do u think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-09-23 06:12:55

以下查询准确返回您需要的内容(在您提供的示例上运行):

<result>{
string-join(
  for $nodeA in $productDoc//nodeA
  return ($nodeA//thing, $nodeA//item), ",")
}</result>

如果您需要更具体的内容,请在问题中指定更多详细信息。

The following query returns exactly what you need (running on example you provided):

<result>{
string-join(
  for $nodeA in $productDoc//nodeA
  return ($nodeA//thing, $nodeA//item), ",")
}</result>

Specify more details in your question if you need something more specific.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文