Search code examples
xmlxpathxqueryxquery-3.0

How to select an element by a variable in xquery?


I know how to select an element like so:

$table/foo

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

let $x = "foo"
$table/[$x]

I know how do this if it was a property from: How to select an attribute by a variable in xquery?


Solution

  • This is nearly identical to the answer for the question How to select an attribute by a variable in xquery? but instead of using the attribute selector @*, you would use the element selector, * (or element()):

    $table/*[local-name() = 'foo']
    
    $table/element()[local-name() = 'foo']