简体   繁体   中英

xquery: find if a parent node has a child node of a different namespace

I am trying to check if a node of namespace 'X' has a child node of a different namespace 'Y'. I tried the following xquery:

declare namespace atom = "http://www.w3.org/2005/Atom";
declare namespace libx = "http://libx.org/xml/libx2";

let $doc := <node><entries xmlns="http://www.w3.org/2005/Atom" xmlns:libx="http://libx.org/xml/libx2"> 
             <libx:book-entry><book>Book 1</book><author>Author 1 PQR</author><title>Title 1</title></libx:book-entry> 
             <libx:car-entry><car>Car 1</car><model>Model 1</model><price>Price 1 PQR</price></libx:car-entry>
           </entries></node>, 
    $types := <types xmlns="http://libx.org/xml/libx2"><type>book-entry</type></types>, 
    $key := 'PQR'

for $type in $types/libx:type
return
   for $entry in $doc/atom:entries/*[name() eq $type]
     return $entry

I expect the result to be: <libx:book-entry><book>Book 1</book><author>Author 1 PQR</author><title>Title 1</title></libx:book-entry> . But, the query returns null as the name function does not take into account a different namespace. Is there another xquery function apart from name() which takes in a namespace parameter which would give the desired result.

Thanks, Sony

You could use the local-name() function to match just the elements' local names (as you have listed them in $types ).

Or if you wanted to be really specific, a combination of the local-name() and namespace-uri() functions to match both element name and Namespace URI.

The expression

$X[namespace-uri() != */namespace-uri()]

will select all elements in $X that have at least one child element whose namespace URI is not equal to the namespace URI of the parent element. This is a rare example of using "!=" as an implicit existential (true if it's true for any member of the sequence.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM