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

烂漫一生 提交于 2019-12-12 03:39:10

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6094493/xquery-find-if-a-parent-node-has-a-child-node-of-a-different-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!