Search code examples
asp.netxsltazure-devopsazure-pipelinesweb.config-transform

XML File Transformations for inner text of an element?


I am in the process of setting up a release pipeline for one of our solutions, but I am struggling to use file transformations on my web.config to alter inner text values of elements.

From what it looks like, you can replace/insert/etc values attached to specific attributes, but not the inner text. Does this mean I will not be able to use file transformations for my purposes?

<setting name="Test" serializeAs="String">
<value>True</value>
</setting>

That "True" value must be replaced with False. There are quite a number of similar instances that need to be replaced. Can this be done with XML file transformations? I cannot use the variable substitution method as it only applies to certain elements like connectionString, etc.

Thanks in advance.


Solution

  • You could use a simple XSL transformation.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        version="1.0">
    
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="setting[@name='Test']/value">
        <xsl:element name="value">False</xsl:element>
      </xsl:template>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    See it working here : https://xsltfiddle.liberty-development.net/bEzknsB