Restructure XML nodes using XSLT
- by Brian
Looking to use XSLT to transform my XML. The sample XML is as follows:
<root>
<info>
<firstname>Bob</firstname>
<lastname>Joe</lastname>
</info>
<notes>
<note>text1</note>
<note>text2</note>
</notes>
<othernotes>
<note>text3</note>
<note>text4</note>
</othernotes>
I'm looking to extract all "note" elements, and have them under a parent node "notes".
The result I'm looking for is as follows:
<root>
<info>
<firstname>Bob</firstname>
<lastname>Joe</lastname>
</info>
<notes>
<note>text1</note>
<note>text2</note>
<note>text3</note>
<note>text4</note>
</notes>
</root>
The XSLT I attempted to use is allowing me to extract all my "note", however, I can't figure out how I can wrap them back within a "notes" node.
Here's the XSLT I'm using:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="notes|othernotes">
<xsl:apply-templates select="note"/>
</xsl:template>
<xsl:template match="*">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
The result I'm getting with the above XSLT is:
<root>
<info>
<firstname>Bob</firstname>
<lastname>Joe</lastname>
</info>
<note>text1</note>
<note>text2</note>
<note>text3</note>
<note>text4</note>
</root>
Thanks