tags:

views:

26

answers:

1

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!

A: 

I changed value in test to ../@gender and now this template adds color attribute to the 'Door' node depending on value of 'gender' attribute of the Car. .. means 'get parent node'. @ means 'get value of the attribute'.

<xsl:template match="Door">
<xsl:element name="Door">
  <xsl:attribute name="color">
    <xsl:choose>
      <xsl:when test="../@gender = 'Boy' ">Red</xsl:when>
      <xsl:when test="../@gender = 'Girl' ">Green</xsl:when>
    </xsl:choose>
  </xsl:attribute>
</xsl:element>

You should do the same for 'Key' template (or better reuse the 'choose' code by extracting it into separate named template).

Hope this helps.

Alex
First thanks for you replay but this solution doesn´t work. My output is now "<Door color="Red"/>" but I want this "<Door color="Red">Lamborghini</Door>
Camal
I now got it! I just added <xsl:apply-templates/> in front of the </xsl:element> :)
Camal