使用XSLT修改单个属性

时间:2021-11-04 15:29:19

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?

要转换第一个的值(在本例中仅为/configuration/system),您能想到的最简单的XSLT是什么?web/编译/@debug属性从true到false?

2 个解决方案

#1


6  

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:

当应用于此XML文档时:

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

生成所需的正确结果:修改任何系统的第一个编译子程序的调试属性。web元素(但是我们知道只有一个系统。配置文件中的web元素。

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

正如我们所看到的,只有第一个debug atribute被修改为false。

#2


-1  

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

false < compile >?还是我误解了这个问题?

#1


6  

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:

当应用于此XML文档时:

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

生成所需的正确结果:修改任何系统的第一个编译子程序的调试属性。web元素(但是我们知道只有一个系统。配置文件中的web元素。

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

正如我们所看到的,只有第一个debug atribute被修改为false。

#2


-1  

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

false < compile >?还是我误解了这个问题?