开发者

How to get the value of an attribute whose name is not a literal in xQuery?

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 !


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜