需要XSLT根据另一个节点更改节点的值

时间:2011-04-29 02:15:49

标签: xslt

如果以下XML文件中EnabledFalse,我需要XSLT将Name的值更改为XYZ

我的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>

1 个答案:

答案 0 :(得分: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>