SQL Server Developer Center >
SQL Server Forums
>
SQL Server XML
>
Where does a value occur in an XML snippet?
Where does a value occur in an XML snippet?
- Hi,
I am using using .nodes(),.query() & .value() to return values from inside of my XML documents - this is working fine.
What I would really like to do is know from WHERE in the XML document the value occurred. For example, take the following (very silly) query:
Is there a way of finding out exactly where (defined by an XPath) in the original XML document the returned values come from?declare @x xml = ' <root> <node1>node1</node1> <node2> <node4>node4</node4> </node2> <node3>node3</node3> </root>'; select nd.value('.','nvarchar(max)') from @x.nodes('//.') X(nd)
Essentially I want to say to my user, "I have found these values in your XML document that might warrant your attention, here is where you can go to find them!"
Hope that makes sense!
thanks
Jamie
http://sqlblog.com/blogs/jamie_thomson/ | http://jamiethomson.spaces.live.com/ | @jamiet
Answers
- The easiest way is to use OPENXML and its edge-table format, eg
As you may know, OPENXML is demanding on server memory so call sp_xml_removedocument as soon as possible. It's possible to reproduce this type of thing with ranking functions, eg ROW_NUMBER(), RANK() etc, but none of them are quite as comprehensive as OPENXML's edge table; really a map of the whole XML document with unique ids for each element, attribute and text.DECLARE @x XML SET @x = ' <root> <node1>node1</node1> <node2> <node4>node4</node4> </node2> <node3>node3</node3> </root>'; DECLARE @hDoc int EXEC sp_xml_preparedocument @hDoc OUTPUT, @x SELECT * FROM OPENXML( @hDoc, '//*', 2 ) EXEC sp_xml_removedocument @hDoc
This is the only reason I still use OPENXML. More here:
http://msdn.microsoft.com/en-us/library/aa276847(SQL.80).aspx- Marked As Answer byJian KangMSFT, ModeratorMonday, November 16, 2009 10:04 AM
All Replies
- The easiest way is to use OPENXML and its edge-table format, eg
As you may know, OPENXML is demanding on server memory so call sp_xml_removedocument as soon as possible. It's possible to reproduce this type of thing with ranking functions, eg ROW_NUMBER(), RANK() etc, but none of them are quite as comprehensive as OPENXML's edge table; really a map of the whole XML document with unique ids for each element, attribute and text.DECLARE @x XML SET @x = ' <root> <node1>node1</node1> <node2> <node4>node4</node4> </node2> <node3>node3</node3> </root>'; DECLARE @hDoc int EXEC sp_xml_preparedocument @hDoc OUTPUT, @x SELECT * FROM OPENXML( @hDoc, '//*', 2 ) EXEC sp_xml_removedocument @hDoc
This is the only reason I still use OPENXML. More here:
http://msdn.microsoft.com/en-us/library/aa276847(SQL.80).aspx- Marked As Answer byJian KangMSFT, ModeratorMonday, November 16, 2009 10:04 AM


