We have a current system that outputs an XML file which is in the following format:
<INVENTORY>
<ITEM>
<SERIALNUMBER>something</SERIALNUMBER>
<LOCATION>something</LOCATION>
<BARCODE>something</BARCODE>
</ITEM>
</INVENTORY>
I need to use this data to load into the standard .NET 2.0 grid. But the grid needs the XML to be in the following format:
<INVENTORY>
<ITEM serialNumber="something" location="something" barcode="something">
</ITEM>
</INVENTORY>
i.e. the child nodes of item need to be converted into attributes of the item node.
Does someone know how this can be done using XSLT?
If your source looks like this:
and you want it to look like this:
then this XSLT should work:
These two templates should do it:-
Here is probably the simplest solution that will convert any children-elements of
ITEM
to its attributes and will reproduce everything else as is, while converting the element names to any desired attribute names:when the above transformation is applied on the provided XML document:
the wanted result is produced:
Do note the following:
The use of the identity rule
The use of
<xsl:strip-space elements="*"/>
The use of the variable
vrtfNameMapping
without anyxxx:node-set()
extension function.The fact that we handle any mapping between a name and a newName, not only simple lower-casing.
That should work:
HTH
This ought to do it:
Or using David's shortcut: