开发者

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

开发者 https://www.devze.com 2023-03-07 13:38 出处:网络
I am trying 开发者_C百科to check if a node of namespace \'X\' has a child node of a different namespace \'Y\'. I tried the following xquery:

I am trying 开发者_C百科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


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.)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号