I need to get all values from XML either it presents in element or attribute.
Example:
DECLARE @XML = '<root>
<row1 attr1="x">1</row1>
<row2 attr2="x">2</row2>
</root>'
Here, my expected output:
Column Value
---------------------
row1 1
attr1 x
row2 1
attr2 x
I am able to get elements and attributes values separately by using below queries.
SELECT
element.value('local-name(.)', 'VARCHAR(50)'),
element.value('.', 'VARCHAR(100)')
FROM
@OldXML.nodes('/root/*') node(element)
SELECT
element.value('local-name(.)', 'VARCHAR(50)'),
element.value('.', 'VARCHAR(100)')
FROM
@OldXML.nodes('/root/row1/@*') node(element)
SELECT
element.value('local-name(.)', 'VARCHAR(50)'),
element.value('.', 'VARCHAR(100)')
FROM
@OldXML.nodes('/root/row2/@*') node(element)
But, I need to get the values in a single query.
I surfed many ways, but I can't get a solution for this requirement.
Is there any way to do this?