如何在 xQuery 中获取名称不是文字的属性的值?

发布于 2024-09-06 16:32:06 字数 331 浏览 0 评论 0原文

例如:

let $targetAtt "= "att1";

let $doc := <Xml att1 = "la", att2 = "la la">

所以这可行:

return $doc/@*[(name(.) = $targetAtt)]

但是是否可以使用更简洁的语法?

例如:以下内容不起作用

例如

return $doc/@$targetAtt.

谢谢!

For example:

let $targetAtt "= "att1";

let $doc := <Xml att1 = "la", att2 = "la la">

So this works:

return $doc/@*[(name(.) = $targetAtt)]

But is a more succinct syntax possible?

For example: the following Does not work

such as

return $doc/@$targetAtt.

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

意中人 2024-09-13 16:32:06

简短的回答:不。

路径表达式需要文字 NameTest。这是没有办法解决的。某些实现可能会提供 eval 函数,例如 eval(concat("$doc/@", $targetAtt)) ,但一般来说,如果有帮助的话,应该避免这种情况。

在普通 XQuery 中整理语法的唯一方法是使用用户定义的函数:

declare function local:attribute($node as node(), $name as xs:QName)
{
    $node/@*[name(.) = $name]
};

local:attribute($doc, $targetAtt)

Short answer: no.

A path expression requires a literal NameTest. There is no way around this. Some implementations may provide an eval function, eg eval(concat("$doc/@", $targetAtt)) but in general this is to be avoided if it can be at all helped.

The only way to tidy the syntax in vanilla XQuery is with a user defined function:

declare function local:attribute($node as node(), $name as xs:QName)
{
    $node/@*[name(.) = $name]
};

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