I've found other questions on moving nodes upwards into the parent but I'm missing the trick to move them down into a newly created node.
Given:
<Villain>
<Name>Dr Evil</Name>
<Age>49</Age>
<Like>Money</Like>
<Like>Sharks</Like>
<Like>Lasers</Like>
</Villain>
I'm trying to transform this with XSLT to:
<Villain>
<Name>Dr Evil</Name>
<Age>49</Age>
<Likes>
<Like>Money</Like>
<Like>Sharks</Like>
<Like>Lasers</Like>
</Likes>
</Villain>
In other words, insert a new child node and move all the child nodes called "Like" under it.
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Like[1]">
<Likes>
<xsl:apply-templates select="../Like" mode="copy"/>
</Likes>
</xsl:template>
<xsl:template match="*" mode="copy">
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="Like"/>
</xsl:stylesheet>
when applied on the provided XML document:
<Villain>
<Name>Dr Evil</Name>
<Age>49</Age>
<Like>Money</Like>
<Like>Sharks</Like>
<Like>Lasers</Like>
</Villain>
produces the wanted, correct result:
<Villain>
<Name>Dr Evil</Name>
<Age>49</Age>
<Likes>
<Like>Money</Like>
<Like>Sharks</Like>
<Like>Lasers</Like>
</Likes>
</Villain>
Do note:
The use and overriding of the identity rule.
The use of modes to specify a somewhat different processing.