I have an XML file which is in the following format:
<root>
<category>
<doctype>
<name>Doc1</name>
<site>
<name>Site1</name>
<target>iframe</target>
<url>http://www.gmail.com</url>
</site>
</doctype>
<doctype>
<name>Doc2</name>
<site>
<name>Site2</name>
<target>iframe</target>
<url>http://www.bbc.co.uk</url>
</site>
</doctype>
</category>
</root>
I need to use it on a standard .net 2.0 TreeView control which requires the XML in the following format
<root>
<category>
<doctype name="Doc1">
<site name = "Site1" target = "iframe" url = "http://www.gmail.com">
</site>
</doctype>
<doctype name="Doc2">
<site name = "Site2" target = "iframe" url = "http://www.bbc.co.uk">
</site>
</doctype>
</category>
</root>
The biggest complication is the fact that some child nodes of the DOCTYPE node need to be converted to attributes (i.e. NAME) while some stay as child nodes which require attributes of their own (i.e. SITE).
How can this be done using XSLT?
Best Answer
The following XSLT 1.0 transformation does what you intend.
Output: