在 xQuery 中使用属性名称

发布于 2024-09-06 22:58:34 字数 749 浏览 1 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-09-13 22:58:34

您没有提及您正在使用的系统,但是有一种 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)

:据我所知,标准 XQuery 没有比您所做的更简单的方法。其他系统可能有类似的扩展。

You didn't mention what system you're using, but there is a MarkLogic-specific way to do this:

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. Other systems may have similar extensions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文