Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

comments

xml

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 --> 
like image 289
dov.amir Avatar asked Dec 20 '11 16:12

dov.amir


People also ask

How do I comment out a section in XML?

The syntax for adding XML comments in your code is triple slashes /// followed by one of the supported XML tags.

Can XML files have comments?

XML comments are similar to HTML comments. The comments are added as notes or lines for understanding the purpose of an XML code. Comments can be used to include related links, information, and terms. They are visible only in the source code; not in the XML code. Comments may appear anywhere in XML code.

Can comments be nested in XML?

No, the string -- is not permitted to appear within comments in XML. So the fact you have -- show up inside another comment is going to cause failures.


1 Answers

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?

like image 86
The Impaler Avatar answered Sep 24 '22 09:09

The Impaler