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

时间:2011-05-23 08:09:33

标签: xml xpath xquery xml-namespaces

我正在尝试检查名称空间“X”的节点是否具有不同名称空间“Y”的子节点。我尝试了以下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

我希望结果为:<libx:book-entry><book>Book 1</book><author>Author 1 PQR</author><title>Title 1</title></libx:book-entry>。但是,查询返回null,因为名称函数没有考虑不同的命名空间。除了name()之外还有另一个xquery函数,它接受一个命名空间参数,该参数可以提供所需的结果。

谢谢, 索尼

2 个答案:

答案 0 :(得分:0)

您可以使用local-name()功能仅匹配元素的本地名称(正如您在 $ types 中列出的那样)。

或者,如果您想要非常具体,可以使用local-name()namespace-uri()函数的组合来匹配元素名称和命名空间URI。

答案 1 :(得分:0)

表达式

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

将选择$ X中具有至少一个子元素的所有元素,该子元素的名称空间URI不等于父元素的名称空间URI。这是使用“!=”作为隐式存在的一个罕见的例子(如果对序列的任何成员都适用,则为true。)