Reputation: 2045
I want to modify an XML file, I have some attributes in this XML file and I want to change this i.e if the producer is VW, then Iwant to change the country to "Germany" hier is my XML:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="example.xslt"?>
<Auto>
<lkw producer="VW" country="USA">
<name>Polo</name>
<price>$5955.2</price>
<color>red</color>
</lkw>
<lkw producer="Audi" country="germany">
<name>A8</name>
<price>$8955.2</price>
<color>black</color>
</lkw>
<lkw producer="BMW" country="USA">
<name>Polo</name>
<price>$6955.2</price>
<color>blue</color>
</lkw>
<lkw producer="VW" country="China">
<name>Pasat</name>
<price>$2955.2</price>
<color>red</color>
</lkw>
</Auto>
and this is my XSLT:
<?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:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@producer[parent::VW]">
<xsl:attribute name="country">
<xsl:value-of select="'germany'"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
but I see no change in my XML file, Could you please tell me, Where is my mistake in XSLT?
Upvotes: 0
Views: 162
Reputation: 70618
Looking at your current template...
<xsl:template match="@producer[parent::VW]">
This is actually equivalent to this...
<xsl:template match="VW/@producer">
So, it is looking for an element called VW, when you really want to be checking the value of the attribute.
What you are really trying to do, is match the @country attribute for elements which have an @producer attribute equal to VW
<xsl:template match="lkw[@producer='VW']/@country">
<xsl:attribute name="country">
<xsl:value-of select="'germany'"/>
</xsl:attribute>
</xsl:template>
Upvotes: 1