How to select an element by a variable in xquery?

谁都会走 提交于 2019-12-12 03:25:30

问题


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?


回答1:


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']



回答2:


Would this still work if the element name was more complex like "foo/bar/hat"

No, the predicate [name() = 'foo/bar/hat'] would not select anything, because foo/bar/hat is a path expression, not an element name. Variables in XPath hold values, not expressions or expression fragments - it's not like a shellscript (or other macro language) where variables are expanded and the expanded expression is then re-parsed.

XQuery does not have any general capability for constructing an expression dynamically as a string and then evaluating it. Many products have extension functions to do this, often called xx:eval() or xx:evaluate(). XSLT 3.0 has an xsl:evaluate instruction.



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

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