user11004963
user11004963

Reputation:

Get list of xml from parent xml in sql

Need to get the list of xml present inside an xml. This is what I have tried.

DECLARE @xml xml
SET @xml = '<a><b /><c><d /><d /><d /></c></a>';

DECLARE @i_xml xml = @xml.value('(/a/b/c)[1]', 'VARCHAR(100)')
Select @i_xml

But this gives me NULL

Upvotes: 0

Views: 31

Answers (1)

Ed Bangga
Ed Bangga

Reputation: 13006

Based on your sample xml, c and b are on same child record of <a>. value() will give a scalar result, use query() instead.

DECLARE @xml xml
SET @xml = '<a><b /><c><d /><d /><d /></c></a>';

DECLARE @i_xml xml = @xml.query('(//a/c/child::*)') 
SELECT @i_xml

Upvotes: 1

Related Questions