I have a bunch of XMLs that I need to parse with SQL.
The XML can take on multiple forms:
<Grandparent>
<parent>
<child1>something</child1>
<child2>something</child2>
</parent>
</Grandparent>
or
<Grandparent>
<child1>something</child1>
<child2>something</child2>
</Grandparent>
Additionally, the number of "child" nodes is variable and there is no way of knowing before hand how many children there are.
What i have done so far is:
@xml.nodes('/Grandparent')
which returns either the <parent> node and children or simply the child nodes depending on the format of the xml.
The version of SQL and the fact that i'm writing it as an SQL function seems to mean that trying to get valueas shown in this anwser does not work.
Therefore, I decided to parse the string. Essentially, I look for < and take the substring from there until > for the node name. Then I take anything between > and </ for the value. I do so in a while loop until the xml string is finished. It works perfectly unless the xml has that parent node.
I don't know how to determine whether that parent node is there and how to ignore it if it is. This is where I am stuck.
What I want to get in either case is:
Node | Value
child1 | something
child2 | something
etc for as many child nodes that there is.
@xml.nodes('/Grandparent'), why then you can't getvalueas shown in that answer link? And given that 2 samples what is the output you expect to get?