I'm using XSLT to process my ASP.Net web.config file to insert some extra log4net configuration. It's applied by the NANT standard task called <style>
. While it successfully inserts the new content, it turns the many self-closing tags into empty paired tags. For example, a partial web.config looks like this before:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<appSettings>
<add key="SomeKey" value="SomeValue"/>
</appSettings>
After applying the stylesheet, the <section>
and <add>
tags (and all other tags) are no longer self-closing:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net">
</section>
</configSections>
<appSettings>
<add key="SomeKey" value="SomeValue">
</add>
</appSettings>
My stylesheet looks like this:
<?xml version="1.0" encoding="utf-8"?>
<!-- This stylesheet is applied to web.config files to insert log4net appender
filters that will prevent logging messages resulting from pages requested by
AIS monitoring systems. -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes" />
<xsl:preserve-space elements="configuration"/>
<!-- Copy input to output, most of the time -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- Within log4net <appender> elements, insert standard filters to
exclude logging traffic resulting from AIS monitoring. Any existing
filters are preserved. -->
<xsl:template match="/configuration/log4net/appender">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<xsl:comment
> Filters inserted by build server during deployment </xsl:comment>
<filter name="AIS monitor"
type="log4net.Filter.PropertyFilter">
<regexToMatch value="^35\.8\.113\.[0-9]+$"/>
<key value="ClientIP"/>
<acceptOnMatch value="false"/>
</filter>
<filter name="AIS load balancer"
type="log4net.Filter.PropertyFilter">
<regexToMatch value="^10\.160\.0\.[0-9]+$" />
<key value="ClientIP"/>
<acceptOnMatch value="false"/>
</filter>
<filter name="localhost" type="log4net.Filter.PropertyFilter">
<stringToMatch value="127.0.0.1"/>
<key value="ClientIP"/>
<acceptOnMatch value="false"/>
</filter>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Before using NANT to process the stylesheet, I tried MSBuild, using the MSBuild Extension Pack task XmlTask
. It preserved the self-closing tags, but would lose most of the line breaks, which made the file human-unreadable (though otherwise correct). Using NANT fits in nicely with my build process, so I'd prefer to use it if I can.
It seems like I should be able to specify that I want to keep self-closing tags in the stylesheet, but I can't figure out how.