我正在尝试检查命名空间"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 函数不考虑不同的命名空间。除了 name() 之外,是否还有另一个 xquery 函数,它接受一个命名空间参数,该参数将给出所需的结果。
谢谢索尼
表达式
$X[namespace-uri() != */namespace-uri()]
将选择$X中至少有一个子元素的所有元素,这些子元素的命名空间 URI 不等于父元素的命名空间 URI。这是使用 "!=" 作为隐式存在的罕见示例(如果对于序列的任何成员为 true,则为 true。
local-name()
函数仅匹配元素的本地名称(如您在$types中列出的名称)。
或者,如果您想非常具体,可以使用 local-name()
和 namespace-uri()
函数的组合来匹配元素名称和命名空间 URI。