- Create new attributes using <xsl:attribute>
For example, given this XML document (animals.xml):
<?xml version="1.0"?>
<animals>
<animal name="dog" class="mammal" legs="4"/>
<animal name="shark" class="fish" legs="0"/>
<animal name="chicken" class="bird" legs="2"/>
</animals>
Transform it to this XML document (pets.xml):
<?xml version="1.0" encoding="UTF-8"?>
<pets>
<mammal name="dog" legs="4"/>
<fish name="shark" legs="0"/>
<bird name="chicken" legs="2"/>
</pets>
We use this XSL Style Sheet:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="animals">
<pets>
<xsl:apply-templates select="animal"/>
</pets>
</xsl:template>
<xsl:template match="animal">
<xsl:element name="{@class}">
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:attribute name="legs">
<xsl:value-of select="@legs"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>