2

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]
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231

1 Answers1

3

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]
wst
  • 11,681
  • 1
  • 24
  • 39