2

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
sony
  • 1,557
  • 10
  • 36
  • 50
  • Also, is there a way to use print statement in xquery so I know what I am retrieving? – sony Sep 23 '10 at 23:40
  • Console logging is implementation dependent. Check the documentation for your XQuery processor. – wst Jul 10 '15 at 16:27

2 Answers2

2

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]
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Nick Jones
  • 6,413
  • 2
  • 18
  • 18
  • 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 Sep 24 '10 at 20:31
  • The result of returning $parent_entry := $feed/atom:entry[atom:id=$parent_id] is as follows:[ 33 Process COinS 2009-02-19T01:05:31Z Builder ] The problem is with "//libx:entry[@src=$child_id]".But, unsure how to fix it – sony Sep 24 '10 at 20:32
  • Note: Parent_id in this case is 33 and child_id is 34 – sony Sep 24 '10 at 21:29
0

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 Webb
  • 14,688
  • 7
  • 55
  • 109
  • 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 Sep 24 '10 at 20:27