[英]using an attribute name in an xQuery
I have this Xml: 我有这个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!!
提前致谢!!
You didn't mention what system you're using, but there is a MarkLogic -specific way to do this: 您没有提到您使用的是什么系统,但是有一种MarkLogic特定的方式来执行此操作:
let $data :=
<Item key = "id">
<SubItem id = "1" a = "2"/>
<SubItem id = "1" b = "3"/>
<SubItem x = "1"/>
<SubItem y = "1"/>
<SubItem z = "1"/>
</Item>
let $path := fn:concat("$data/SubItem[exists(./@", fn:data($data/@key), ")]")
return xdmp:value($path)
As far as I know, standard XQuery doesn't have a simpler way than what you did. 据我所知,标准的XQuery并没有比您更简单的方法。 Other systems may have similar extensions.
其他系统可能具有类似的扩展名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.