2011-04-29 108 views
1

如果在以下XML文件中的NameXYZ,我需要XSLT將Enabled的值更改爲False需要XLST根據另一個節點更改節點的值

我的XML文件是:

<MyRoot> 
    <Category> 
     <Name>XYZ</Name> 
     <Location>mylocation</Location> 
     <Enabled>True</Enabled> 
    </Category> 
    <Category> 
     <Name>ABC</Name> 
     <Location>mylocation1</Location> 
     <Enabled>True</Enabled> 
    </Category> 
    <Category> 
     <Name>DEF</Name> 
     <Location>mylocation2</Location> 
     <Enabled>True</Enabled> 
    </Category> 
</MyRoot> 
+0

通過「將Enabled的值更改爲False」是否將示例xml中的所有Enabled元素更改爲false,或者將某個其他啓用的值更改爲false? – Justin 2011-04-29 02:27:07

回答

1

這是我將如何處理它:

XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Category[Name='ABC']/Enabled"> 
    <Enabled>False</Enabled> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

<MyRoot> 
    <Category> 
     <Name>XYZ</Name> 
     <Location>mylocation</Location> 
     <Enabled>False</Enabled> 
    </Category> 
    <Category> 
     <Name>ABC</Name> 
     <Location>mylocation1</Location> 
     <Enabled>True</Enabled> 
    </Category> 
    <Category> 
     <Name>DEF</Name> 
     <Location>mylocation2</Location> 
     <Enabled>True</Enabled> 
    </Category> 
</MyRoot> 
+1

更典型:​​如果您喜歡,可以使用'Category [Name ='ABC']/Enabled'或'Enabled [../ Name ='ABC']'。 – 2011-04-29 03:26:12

相關問題