2011-04-29 107 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

相关问题