Nested comments in XML?

XmlComments

Xml Problem Overview


Are these nested comments allowed in a XML file?

<!-- Making only one observation attempting to correct the error code -->
<!-- <component>
       <!-- Result observation template -->
            <!-- <id root="2.16.840.1.113883.19.5.10" extension="103220"/>
     </component> -->

Xml Solutions


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

And trying to post that answer also broke the text entry parsing ;)

For further proof, check the W3C specification:

http://www.w3.org/TR/2008/REC-xml-20081126/#sec-comments

The phrase >For compatibility, the string " -- " (double-hyphen) MUST NOT occur within comments.]

appears in the first paragraph of the section on XML comments.

Solution 2 - Xml

As it is said in How do I comment out a block of tags in XML?, you can try to wrap your code with a non-existing processing-instruction, e.g.:

<?ignore
<component>
       <!-- Result observation template -->
            <!-- <id root="2.16.840.1.113883.19.5.10" extension="103220"/>
     </component> 
?>

Solution 3 - Xml

In a word - no.

The first encountered end-comment marker will, er... end the comment and the rest of it will look somewhat unpleasant from there on out.

Solution 4 - Xml

You can't. -- both starts and terminates a comment. This makes nesting them impossible.

Solution 5 - Xml

Notepad++ together with the plugin XML Tools can do this.

Select a block of xml and on the xml tools submenu selected "Comment Selection".

Each existing "inner xml comment" will be altered so that it looks like this

  <!{1}** inner xml comment **{1}>

and if you add another outer comment in this way, those original inner comments will be further altered to

  <!{2}** inner xml comment **{2}>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLaxmikanth SamudralaView Question on Stackoverflow
Solution 1 - XmlBrent Writes CodeView Answer on Stackoverflow
Solution 2 - XmlpsychoslaveView Answer on Stackoverflow
Solution 3 - XmlannakataView Answer on Stackoverflow
Solution 4 - XmlQuentinView Answer on Stackoverflow
Solution 5 - XmlPhilip BeckView Answer on Stackoverflow