Search code examples
xmlcomments

How do I comment out an XML section that has comments inside?


How do I comment out an XML section that has comments inside?

The following won't work. It only comments out stuff0:

<!--
stuff0
<!-- stuff1 -->
stuff2
<!-- stuff3 -->
stuff4
<!-- stuff5 -->
stuff6
-->

Solution

  • -- is not allowed in XML comments. You can add a space between the two -. If you want to do that programmatically, an XSLT may do the job for you. For example, the following XSLT:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0">
        <xsl:template match="/">
            <a>
                <xsl:comment select="'a -- b -- c'"/>
            </a>
        </xsl:template>
    </xsl:stylesheet>
    

    Gives the following output:

    <a><!--a - - b - - c--></a>
    

    But the XSLT processor may also output an error. Given the specification, it's up to the implementation to add a space or raise an error.