XQuery插入父节点和子节点

时间:2013-08-05 12:05:17

标签: xml xquery

我正在尝试用一些文本替换节点以及其中的子节点。下面是我的XQuery。我没有得到理想的结果。

for $x in collection('articles')//Doc[Title/@No='Article 20']
  return replace value of node $x/Desc with "Article on Molecular Physics 
  <Date @d='20130805'/> in continuation with Part 1."

期待 -

....
<Desc>Article on Molecular Physics 
  <Date @d='20130805'/> in continuation with Part 1.</Desc>
....

但我得到了 -

....
<Desc>Article on Molecular Physics 
  &lt;Date @d='20130805'/&gt; in continuation with Part 1.</Desc>
....

2 个答案:

答案 0 :(得分:1)

为了获得所需的结果,我首先删除了节点,然后按如下方式插入 -

 let $c := <Desc>Article on Molecular Physics <Date d='20130805'/> in continuation with Part 1.</Desc>
 for $x in collection('articles')//Doc[Title/@No='Article 20']
     return 
     (
         delete node $x/Desc,
         insert node $c after $x/Title 
     )

这对我有用:)

答案 1 :(得分:0)

因为你在字符串中包含了新的XML元素,所以它被视为一个;即逃脱。尝试使用元素中所需节点的序列替换字符串,如下所示:

for $x in collection('articles')//Doc[Title/@No='Article 20']
  return replace value of node $x/Desc with 
  ("Article on Molecular Physics ", <Date d='20130805'/>, " in continuation with Part 1.")