Reputation: 11637
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
-->
Upvotes: 30
Views: 29707
Reputation: 48865
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?
Upvotes: 13
Reputation: 2998
--
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.
Upvotes: 8
Reputation: 1110
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
Upvotes: 2
Reputation: 6956
You have two options:
--
with something else first.Upvotes: 2
Reputation: 27974
You don't. XML as per its specification doesn't support nested comments.
Upvotes: 7