1

I need XSLT to change the value of Enabled to False if the Name is XYZ in the below XML file.

My XML file is:

<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
  • By "change the value of Enabled to False" do you mean change all Enabled elements in the sample xml to false, or change some other enabled value to false? Commented Apr 29, 2011 at 2:27

1 Answer 1

1

This is how I would handle it:

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>

output

<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>
Sign up to request clarification or add additional context in comments.

1 Comment

More typical: Category[Name='ABC']/Enabled or Enabled[../Name='ABC'] if you like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.