How to select an attribute by a variable in xquery?

若如初见. 提交于 2019-12-22 12:26:37

问题


I know how to select an attribute like so:

$table/@id

However how do I do this if the attribute name is stored as a variable. For example:

let $x = "id"
$table/@[$x]

回答1:


You can use the functions local-name or node-name to capture the value of the attribute and match it the predicate. local-name will simply return a string that matches the element name, and node-name will return a fully qualified name, which is generally recommended, but practically speaking, is only necessary if you are dealing with namespaces.

let $x = "id"
return $table/@*[local-name(.) = $x]

let $x := xs:QName("id")
return $table/@*[node-name(.) = $x]


来源:https://stackoverflow.com/questions/34025758/how-to-select-an-attribute-by-a-variable-in-xquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!