tags:

views:

59

answers:

2

Hello!

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" defaultLanguage="C#">
      <!-- this is a comment -->
    </compilation>
  </system.web>
</configuration>

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?

A: 

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

CharlesLeaf
+1  A: 

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.

Dimitre Novatchev
This is called the identity pattern transform for XSLT, and as @Dimitre Novatchev pointed out, it's extremely powerful.
lavinio