views:

105

answers:

2

Hi,

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.

Thanks, Sony

A: 

Hey Sony!

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

Travis J Webb
Hey Travis!! Didn't expect to catch you on stackoverflow hehe :D I am debugging the xqueries from the command line. Thanks! I tried returning $feed and $parent_entry and both return expected results. Although when I try to set $parent_entry to $feed/atom:entry[atom:id=$parent_id]//libx:entry[@src=$child_id] it returns nothing []. I still think the problem is with xpath expression or namespaces
sony
+1  A: 

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]
Nick Jones
Hey Nick, I don't see any problem with the feed. I changed my xquery to "let $parent_entry := $feed/atom:entry[atom:id=$parent_id]return $parent_entry" after the declaration statements. When I run this query it returns the expected result. But, when I change the query to let $parent_entry := $feed/atom:entry[atom:id=$parent_id]//libx:entry[]@src=$child_id] return $parent_entry , the query returns nothing
sony
The result of returning $parent_entry := $feed/atom:entry[atom:id=$parent_id] is as follows:[<entry xmlns="http://www.w3.org/2005/Atom" xmlns:libx="http://libx.org/xml/libx2"> <id>33</id> <title>Process COinS</title> <updated>2009-02-19T01:05:31Z</updated> Builder</content> <libx:libapp> <libx:entry xmlns:libx="http://libx.org/xml/libx2" src="34"/> <libx:entry xmlns:libx="http://libx.org/xml/libx2" src="35"/> <libx:entry xmlns:libx="http://libx.org/xml/libx2" src="37"/> </libx:libapp></entry>] The problem is with "//libx:entry[@src=$child_id]".But, unsure how to fix it
sony
Note: Parent_id in this case is 33 and child_id is 34
sony