【问题标题】:XQuery not inserting child nodeXQuery 不插入子节点
【发布时间】:2013-12-09 05:58:57
【问题描述】:

下面是 XML 结构。它是我原始结构的一个样本,而不是确切的。

<Docs>
  <Doc>
   <Para>
      <P n="1"><B>Constants : T</B>he value of pi is 3.14</P>
      <P n="2">pi is a geometric term.</P>
   </Para>
  </Doc>
  <Doc>
   <Para>
     <P n="1"><B>Constants : T</B>he value of g is 9.81 m/sqr of sec</P>
     <P n="2">g is a acceleration due to gravity.</P>
   </Para>
  </Doc>
  <Doc>
    <Para>
      <P n="1"><B>Constants : T</B>he value of c is 3.00 x 10 power 8 m/sec</P>
      <P n="2">c is a speed of light in vacuum.</P>
    </Para>
  </Doc>
</Docs>

我以编程方式生成了 XML 文件。 B 节点有数据Constant : T,而它应该只有Constants :。我编写了一个 XQuery 来进行必要的更改,但它没有按预期工作。

以下是 XQuery - 第 1 版

for $x in doc('doc1')//Doc
where $x/Para/P[@n="1"]/B/text()="Constants : T"

return
let $p := $x/Para/P[@n="1"]
let $pText := concat("T", $p/text())
let $tag := <P n="1">{$pText}</P>

return
(
delete node $p,
insert node $tag as first into $x/Para,
insert node <B>Constants :</B> as first into $x/Para/P[@n="1"]  
)

版本 - 2(更小、更甜但不工作!!!)

let $b := <B> Constants :</B>
for $x in doc('doc1')//Doc/Para[P[@n="1"]/B/text()="Constants : T"]/P[@n="1"]

return
(
 replace value of node $x with concat("T", $x/text()),
 insert node $b/node() as first into $x
)

两个查询都没有插入&lt;B&gt;Constants : &lt;/B&gt;。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: xquery basex


    【解决方案1】:

    您面临的问题与 XQuery 更新的性质有关。它使用挂起的更新列表并在查询结束时应用所有更新。更新操作的顺序是明确定义的,因此与您在更新语句中给出的顺序无关。在https://docs.basex.org/wiki/Updates#Pending_Update_List 上查看更多信息。

    因此,在您的情况下,insertreplace 之前应用,因此您实际上是在替换刚刚插入的节点,从而覆盖此更改。

    要解决这个问题,我只需替换文本值并替换 B 节点。因此,您的两个操作都相互独立,并且可以毫无问题地更改它们的执行顺序。

    let $b := <B> Constants :</B>
    for $x in doc('doc1')//Doc/Para[P[@n="1"]/B/text()="Constants : T"]/P[@n="1"]
    
    return
    (
        replace value of node $x/text() with concat("T", $x/text()),
        replace node $x/B with $b
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      相关资源
      最近更新 更多