2012-11-02 40 views
0

我想修改一個XML文件,我在這個XML文件中有一些屬性,我想改變這個,即如果生產者是VW,那麼Iwant將國家改爲「德國」 票數是我的XML:用XSLT修改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> 

,這是我的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> 

但我看到在我的XML文件中沒有變化,能否請你告訴我,哪裏是我的XSLT錯誤?

回答

1

縱觀目前的範本......

<xsl:template match="@producer[parent::VW]"> 

這實際上相當於這個...

<xsl:template match="VW/@producer"> 

所以,它正在尋找一個元素稱爲VW,當你真的想要檢查屬性的值。

你真正要做的,是符合@country屬性的元素,其有一個@producer屬性等於VW

<xsl:template match="lkw[@producer='VW']/@country"> 
    <xsl:attribute name="country"> 
    <xsl:value-of select="'germany'"/> 
    </xsl:attribute> 
</xsl:template> 
+2

屬性部分可以簡化爲的德國