sony
sony

Reputation: 1587

Removing node using XQuery Update

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.

Upvotes: 2

Views: 8433

Answers (2)

Nick Jones
Nick Jones

Reputation: 6493

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]

Upvotes: 2

Travis Webb
Travis Webb

Reputation: 15018

Not knowing what the output or error is, I would guess that $parent_node isn't being set properly; that is, the query $feed/atom:entry[atom:id=$parent_id] isn't returning anything. I would try $feed/entry[id=$parent_id] for fun. Also, make sure $feed is getting set correctly as well, and also try removing the "atom:" from the declare variable $feed statement.

As for a print statement (presumably to debug), that is probably specific to the vendor of database you are using. If you are still using BaseX, run this query from the command line and add "-v" for output. Now that I look at that code again, I wonder why I used let $parent_entry... instead of declare variable $parent_entry....

xquery is a pain, isn't it?

-tjw

Upvotes: 0

Related Questions