I have some (untyped) XML being stored in SQL Server 2005 that I need to transform into a normalized structure. The structure of the document currently looks like so:
<wrapper>
<parent />
<node />
<node />
<node />
<parent />
<node />
<node />
<node />
<wrapper>
I want to transform it to look like this:
<wrapper>
<parent>
<node />
<node />
<node />
</parent>
<parent>
<node />
<node />
<node />
</parent>
<wrapper>
I can select the XML out into a relational structure if I need to, put the problem is there are no attributes linking the parent and the child nodes together, so order becomes an issue when using set-based operations. How can I use the .nodes()/.value()/other SQL Server XML APIs to transform this data? The transformation needs to run as part of a batch SQL script so extracting it into another tool/language is not a reasonable option for me.