简体   繁体   English

xquery:查找父节点是否具有不同名称空间的子节点

[英]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'. 我正在尝试检查名称空间“ X”的节点是否具有其他名称空间“ Y”的子节点。 I tried the following xquery: 我尝试了以下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> . 我希望结果是: <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. 除了name()之外,是否还有另一个xquery函数,它接受一个可提供所需结果的名称空间参数。

Thanks, Sony 谢谢,索尼

You could use the local-name() function to match just the elements' local names (as you have listed them in $types ). 您可以使用local-name()函数仅匹配元素的本地名称(如您在$ 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. 或者,如果您想真正具体一点,可以结合使用local-name()namespace-uri()函数来匹配元素名称和命名空间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. 将选择$ X中具有至少一个子元素的所有元素,这些子元素的名称空间URI与父元素的名称空间URI不相等。 This is a rare example of using "!=" as an implicit existential (true if it's true for any member of the sequence.) 这是一个罕见的示例,使用“!=”作为隐式存在(如果该序列的任何成员为true,则为true。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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