Removing node using XQuery Update

sony picture sony · Sep 24, 2010 · Viewed 8.1k times · Source

I am trying to remove a child node from a parent node using xquery. Say, I have an entry shown below in my xml:

 <entry>
        <id>36</id>
        <title>Engineering Village Author Tool</title>
        <updated>2009-09-30T12:55:42Z</updated>
        <libx:libapp>
          <libx:entry xmlns:libx="http://libx.org/xml/libx2" src="37"/>
        </libx:libapp>
      </entry>

How do I delete the child node

<libx:entry xmlns:libx="http://libx.org/xml/libx2" src="37"/>

I am using the following xquery code:

declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $child_id as xs:string external;
declare variable $parent_id as xs:string external;
declare variable $doc_name as xs:string external;
declare variable $feed      as xs:anyAtomicType := doc($doc_name)/atom:feed;
let $parent_entry := $feed/atom:entry[atom:id=$parent_id]
return delete node $parent_entry//libx:entry[@libx:src=$child_id]

The values of the variables passed here are: child_id = 37 parent_id = 36 doc_name = name of the document being used

I am guessing something is wrong either with the way am using namespaces or the xpath expression i am using in my xquery at line :

return delete node $parent_entry//libx:entry[@libx:src=$child_id]

Please help me fix this.

Answer

Nick Jones picture Nick Jones · Sep 24, 2010

You are declaring $feed as xs:anyAtomicType, but you are setting it and using it as a node().

I'm actually suprised the query compiles. Try removing the xs:anyAtomicType or replacing it with element().

Also you only want @src to select your child node, not @libx:src. So

declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $child_id as xs:string external;
declare variable $parent_id as xs:string external;
declare variable $doc_name as xs:string external;
declare variable $feed := doc($doc_name)/atom:feed;
let $parent_entry := $feed/atom:entry[atom:id=$parent_id]
return delete node $parent_entry//libx:entry[@src=$child_id]