2
votes

Can anyone please help me with this xquery for eXist database. I have the following xml structure

<A>  
   <B>  
     <D/>  
     <D/>  
     <D/>  
     <E/>  
  </B>  
</A> 

I'm trying to get the following structure

<A>  
   <B>  
    <C>  
     <D/>  
     <D/>  
     <D/>  
     <E/>   
    </C>   
  </B>  
</A>

How do I insert the <C> tag?

Thanks
--SD

2
I figured out a way to do this suppose the entire xml is in a doc, say example.xml let $document := doc(example.xml) update replace $document/B with <B> <C> <D/> <D/> <D/> <E/> </C> </B>SDS
Good question, +1. See my answer for a complete, simple, very short and easy solution.Dimitre Novatchev
Hi there it looks to me that SDS is asking for a normal action on a eXist database... UPDATE is the answer and I really think Shilagae's answer is elegant and really appropriate... plus it separates logic from data... as it should always be...SimonQuest

2 Answers

1
votes

I can't verify, but it should be so:

let $x := doc('namedocument.xml')/A/B
update insert <C>$x</C> into  doc('namedocument.xml')/A/B
0
votes

You don't need XQuery Update to do this kind of operations.

This XQuery application:

<A>
 <B>
  <C>
  {for $n in /A/B/node()
   return $n}
  </C> 
 </B>
</A>

when applied on the provided XML document:

<A>      
  <B>
    <D/>
    <D/>
    <D/>
    <E/>
  </B>   
</A>  

produces exactly the wanted, correct result:

<A>
   <B>
      <C>
         <D/>
         <D/>
         <D/>
         <E/>
      </C>
   </B>
</A>