在 xQuery 中使用属性名称
我有这个 Xml:
<Item key = "id">
<SubItem id = "1" a = "2"/>
<SubItem id = "1" b = "3"/>
<SubItem x = "1"/>
<SubItem y = "1"/>
<SubItem z = "1"/>
</Item>
我希望选择具有名称等于 (.) 键的值的属性的子项
我想要一个返回此值的查询:
<SubItem id = "1" a = "2"/>
<SubItem id = "1" b = "3"/>
所以我尝试了:
let $x := ... my xml here...
let $key = $x/@id
return $x/*/@*[name(.) = $key]/..
这有效......
但我想要更简洁的东西,例如:
let $x := ... my xml here...
let $key = $x/@id
return $x/*/@$key
问题是,该代码无法编译,因为它不允许我在 @ 之后使用非文字(或通配符)。
难道真的不可能吗?提前致谢!!
I have this Xml:
<Item key = "id">
<SubItem id = "1" a = "2"/>
<SubItem id = "1" b = "3"/>
<SubItem x = "1"/>
<SubItem y = "1"/>
<SubItem z = "1"/>
</Item>
I wish to select the SubItems that have an attribute whose name equals the value of (.) key
I want a query that returns this:
<SubItem id = "1" a = "2"/>
<SubItem id = "1" b = "3"/>
So I tried:
let $x := ... my xml here...
let $key = $x/@id
return $x/*/@*[name(.) = $key]/..
this works...
but I wanted something more succinct like:
let $x := ... my xml here...
let $key = $x/@id
return $x/*/@$key
problem is, that code does not compile because it won't let me use a non literal (or wildcard) after the @.
Is it really impossible? Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提及您正在使用的系统,但是有一种 MarkLogic 特定的方法可以做到这一点
:据我所知,标准 XQuery 没有比您所做的更简单的方法。其他系统可能有类似的扩展。
You didn't mention what system you're using, but there is a MarkLogic-specific way to do this:
As far as I know, standard XQuery doesn't have a simpler way than what you did. Other systems may have similar extensions.