Set attribute to all child elements via xsl:choose
- by Camal
Hi,
assuming I got following XML file :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<MyCarShop>
<Car gender="Boy">
<Door>Lamborghini</Door>
<Key>Skull</Key>
</Car>
<Car gender="Girl">
<Door>Normal</Door>
<Key>Princess</Key>
</Car>
</MyCarShop>
I want to perform a transformation so the xml looks like this :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<MyCarShop>
<Car gender="Boy">
<Door color="blue">Lamborghini</Door>
<Key color="blue">Skull</Key>
</Car>
<Car gender="Girl">
<Door color="red">Normal</Door>
<Key color="red">Princess</Key>
</Car>
</MyCarShop>
So I want to add a color attribut to each subelement of Car depending on the gender information.
I came up with this XSLT but it doesnt work :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<!--<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>-->
<xsl:template match="/">
<xsl:element name="MyCarShop">
<xsl:attribute name="version">1.0</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Car">
<xsl:element name="Car">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Door">
<xsl:element name="Door">
<xsl:attribute name="ViewSideIndicator">
<xsl:choose>
<xsl:when test="gender = 'Boy' ">Front</xsl:when>
<xsl:when test="gender = 'Girl ">Front</xsl:when>
</xsl:choose>
</xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template match="Key">
<xsl:element name="Key">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Does anybody know what might be wrong ?
Thanks again!