简体   繁体   English

过滤XML属性(fn:node-name与fn:name)

[英]Filtering XML attributes (fn:node-name vs. fn:name)

Using XQuery (in eXist-db 2.2), I am trying to process a set of XML elements and remove a single attribute from each element in the result set. 我正在尝试使用XQuery(在eXist-db 2.2中)处理一组XML元素,并从结果集中的每个元素中删除单个属性。

Example XML: XML示例:

<results xmlns:abc="http://example.org">
    <abc:element1 abc:selected="false" type="Class"/>
    <abc:element2 abc:selected="false" type="Property"/>        
</results>

The attribute to be removed is @abc:selected . 要删除的属性是@abc:selected My question is, what is the difference between filtering an attribute set by fn:name vs. fn:node-name, when the attribute has an xs:QName? 我的问题是,当属性具有xs:QName时,过滤由fn:name与fn:node-name设置的属性之间有什么区别?

If I use fn:node-name and compare to the xs:QName of the attribute, then @abc:selected is not removed. 如果我使用fn:node-name并与属性的xs:QName进行比较,则不会删除@abc:selected

XQuery: XQuery:

for $r in $results
let $name := node-name($r)
return
    element {$name} {
        $r/@*[node-name(.) != xs:QName('abc:selected')]             
    }

Result: 结果:

<results xmlns:abc="http://example.org">
    <abc:element1 abc:selected="false" type="Class"/>
    <abc:element2 abc:selected="false" type="Property"/>        
</results>

However, if I use fn:name and compare to the string value of the attribute, then @abc:selected is successfully removed. 但是,如果我使用fn:name并与该属性的字符串值进行比较,则@abc:selected已成功删除。

XQuery: XQuery:

for $r in $results
let $name := node-name($r)
return
    element {$name} {
        $r/@*[name(.) != 'abc:selected']
    }

Result: 结果:

<results xmlns:abc="http://example.org">
    <abc:element1 type="Class"/>
    <abc:element2 type="Property"/>        
</results>

What is the difference here? 这里有什么区别? Why doesn't the first approach, with fn:node-name and xs:QName, work as I expect it to? 为什么使用fn:node-name和xs:QName的第一种方法不能按我期望的那样工作?

Looks like that isn't the proper way to create the expected xs:QName instance. 看起来这不是创建预期的xs:QName实例的正确方法。 This expression xs:QName('abc:selected') is throwing error "No namespace declared for prefix abc" for me (tested in BaseX). 此表达式xs:QName('abc:selected')对我抛出错误“没有为前缀abc声明任何名称空间” (在BaseX中测试)。

You can use fn:QName() which return xs:QName instance instead, and that should properly remove abc:selected attributes : 您可以使用fn:QName() ,它返回xs:QName实例,并且应该正确删除abc:selected属性:

for $r in $results
let $name := node-name($r)
return
    element {$name} {
        $r/@*[node-name(.) ne QName('http://example.org', 'selected')]             
    }

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

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