Change a single attribute using XSLT

What simple XSLT can you think of converting the value of the first, in this case only the /configuration/system.web/compilation/@debug attribute from true to false ?

+7
xml xslt
source share
2 answers

This conversion is :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*" name="identity"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="system.web/compilation[1]/@debug"> <xsl:attribute name="debug">false</xsl:attribute> </xsl:template> </xsl:stylesheet> 

when applied to this XML document:

 <configuration> <system.web> <compilation debug="true" defaultLanguage="C#"> <!-- this is a comment --> </compilation> <compilation debug="true" defaultLanguage="C#"> <!-- this is another comment --> </compilation> </system.web> </configuration> 

creates the desired correct result: changes the debug attribute of the first child compilation element of any system.web element (but we know that there is only one system.web element in the configuration file.

 <configuration> <system.web> <compilation debug="false" defaultLanguage="C#"> <!-- this is a comment --> </compilation> <compilation debug="true" defaultLanguage="C#"> <!-- this is another comment --> </compilation> </system.web> </configuration> 

As we can see, only the first debug attribute should be modifird to false if necessary.

+6
source share

<xsl:attribute name="debug">false</xsl:attribute> inside <compilation> ? Or do I not understand the question?

-one
source share

All Articles