I'm trying to select nodes in XML document by provided name.
declare @xdoc xml = '
<data>
<box><id>1</id><weight>10</weight></box>
<tube><id>2</id><weight>20</weight></tube> <!-- Should be skipped -->
<box><id>3</id><weight>30</weight></box>
</data>
'
declare @node nvarchar(100)='box'
select
ref.value('id[1]','bigint') as Id,
ref.value('weight[1]','bigint') as Weight
from @xdoc.nodes('/data/*[local-name()=[sql:variable("@node")]]') as xdata(ref)
-- WORKS FINE:from @xdoc.nodes('/data/*[local-name()="box"]') as xdata(ref)
and it gives me error message:
XQuery [nodes()]: Syntax error near '[', expected a step expression.
How do I access local variable in xml.nodes()? Or may be there is some better way to achieve it?