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

25,229

Solution 1

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

Solution 2

You shouldn't do it, I know. Per the XML specification you aren't supposed to.

Having said that... I really needed it badly, so here's the trick. Surround the section you want to comment with an unused processing instruction:

<some-tags />

<?comment
  <!-- traditional comment 1 -->
  <badtag prop1="123">
    <!-- traditional comment 2 -->        
  </badtag>
  <!-- traditional comment 3 -->
?>

<rest-of-my-tags />

You can use any processing instruction that's not in use. I chose "comment" as the unused processing instruction, so I started the big comment with <?comment and ended it with ?>.

Pretty much any word will do as a processing instruction, since they aren't really used most of the time. I mean, when was the last time you used one?

Solution 3

You don't. XML as per its specification doesn't support nested comments.

Solution 4

You have two options:

  1. Delete it, rather than commenting out - you can always revert afterwards.
  2. Replace all instances of -- with something else first.

Solution 5

Here's another approach: You could just use nested comments as though it were supported in XML:

<!-- Outer comment
  <SomeXML> ... </SomeXML>
  <!-- Inner comment 
    <SomeXML> ... </SomeXML>
  -->
-->

Then only use this XML inside a text editor. The XML you actually feed into a process is the output of some simple program you write that reads the nested-comment-XML and spits out unnested-comment-XML.

Another similar trick is to use a C compiler's preprocessor. I did this years ago with Microsoft's C++ compiler. The compiler, cl.exe, actually invoked two separate programs, cl1.exe and cl2.exe. Cl1.exe is the preprocessor. So you could feed anything into that thing (XML, nmake scripts, whatever) and it'll preprocess it. So something like this would work:

<SomeXML> ... </SomeXML>
#ifdef PRODUCE_THIS
<SomeMoreXML> ... </SomeMoreXML>
#endif
Share:
25,229

Related videos on Youtube

dov.amir
Author by

dov.amir

JAVA fullstack developer GitHub https://github.com/DovAmir Tech blog http://opensourceinsights.blogspot.co.il Linkedin http://www.linkedin.com/in/dovamir

Updated on July 25, 2022

Comments

  • dov.amir
    dov.amir almost 2 years

    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
    -->
    
  • Paul Wintz
    Paul Wintz almost 6 years
    This is hands-down the best solution, at least for me.