More of reflection that a problem . Im creating a stored procedure that takes xml as an input parameter and having some issues when querying the data.
This is one of the queries
DECLARE @xVar XML
SET @xVar =
'<?xml version="1.0"?>
<Workflow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ait.com/workflow/">
<Users>
<User ObjectId="1232">
<UserId>123</UserId>
</User>
<User ObjectId="1232">
<UserId>124</UserId>
</User>
</Users>
</Workflow>';
WITH XMLNAMESPACES(DEFAULT 'http://ait.com/workflow/')
SELECT [UserId] = reportdata.item.value('UserId[1]', 'varchar(36)')
FROM @xVar.nodes('//Workflow/Users/User') AS reportdata(item)
This just returns the userid’s in the xml document . If i take a close look at the last part of the select statement
FROM @xVar.nodes('//Workflow/Users/User') AS reportdata(item)
It works and seems logical , start from the root and specify path, what is strange to me is that this also works
FROM @xVar.nodes('//Users/User') AS reportdata(item)
and even stranger that this works
FROM @xVar.nodes('//User') AS reportdata(item)
Probably missed some page in the XML Book for dummies, could someone enlighten me please
The double
//means that it will give you all descendant nodes that match the expression.This
//Workflow/Users/Userdoes not mean “start from the root”. An expression that matches from the root looks like this/Workflow/Users/User.So
//Userwill give you all descendant user nodes regardless of where they are.Try this:
Result: