Ok so I have gone through many related posts but have not been able to pinpoint an answer to my problem. I need to write an XSLT to transform an XML in the following format
<Message>
<Receiver>
<name>123</name>
<address>111</address>
<phone>1000</phone>
</Receiver>
<List>
<item>
<no>1</no>
<desc>one</desc>
</item>
<item>
<no>2</no>
<desc>two</desc>
</item>
</List>
<Message>
to this –
<Message>
<Receiver name=123>
<address>111</address>
<phone>1000</phone>
</Receiver>
<List>
<item no=1>
<desc>one</desc>
</item>
<item no=2>
<desc>two</desc>
</item>
</List>
</Message>
I have tried using the xsl template match tag.
but has failed miserably. Any ideas or help is much appreciated/
**note i have edited the post to show the actual problem – i initially posted just a part as to avoid confusion but as it seems the problem needs to be stated as a whole. Apologies for inconveniences.
This can be done by building upon the identity template. First you need a template to match the Receiver element, copy it but adding the name attribute at the same time
You can do a similar one for the item element. Note how this makes use of “Attribute Value Templates” to create the name attribute from the value of the name element.
Then, you just need a template to match the name and no elements and ignore them, so they are not output.
Here is the full XSLT
When applied to your XML the following is output
Now, if you wanted to be more generic, and have a rule where the first ‘leaf’ element of any parent element is turned into an attribute, then try this XSLT
This should also output the same results. I’ll leave it as an exercise to the reader how it works….