Pete Montgomery
Pete Montgomery

Reputation: 4100

Alter a single attribute with XSLT

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

Upvotes: 7

Views: 894

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

This transformation:

<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 on 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>

produces the wanted, correct result: modifies the debug attribute of the first compilation child of any system.web element (but we know that there is only one system.web element in a config 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 see, only the first debug atribute is modifird to false, as required.

Upvotes: 6

CharlesLeaf
CharlesLeaf

Reputation: 3201

<xsl:attribute name="debug">false</xsl:attribute> inside the <compilation>? Or am I misunderstanding the question?

Upvotes: -1

Related Questions