XQuery how to get whole xml document with WHERE query

699 Views Asked by At

I have XML db with only one collection (container) and I don't know the document names. How to get a entire XML document from db, which complies WHERE clause?

<root>
      <node1>
            <node2>
                   <node3>My Content</node2>
            </node2>
      </node1>
<root>

When I have queries

query 'collection("data1.dbxml")/root/node1/node2[node3 = "My Content"]/string()'

it returns a content from that node3

'My Content'

and

query 'collection("data1.dbxml")/root/ode1/node2/node3'

it returns 2 internal nodes with the content

<node2><node3>My Content</node3></node2>

But how to get whole document which complies this WHERE clause (sth like SELECT * FROM data2.dbxml WHERE node3='My Content'?

2

There are 2 best solutions below

0
On

Simply use a predicate as you did in the first example:

  collection("data1.dbxml")/root[node1/node2/node3 = "My Content"]

You can think of the predicate in XQuery as the WHERE in SQL and the SELECT-part is everything before.

1
On

another resolution ... after studying :)

query 'for $x in collection("data1.dbxml") 
       where $x/root/node1/node2/node3 = "My Content"
       return $x'

or when we know the depth of node in XMLdoc and node's name

query 'for $x in collection("data1.dbxml") 
       where $x/*/*/*/node3 = "My Content"
       return $x'

thanks W3Schools