75

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> -->
bluish
  • 26,356
  • 27
  • 122
  • 180
Laxmikanth Samudrala
  • 2,203
  • 5
  • 28
  • 45
  • 1
    ... wondering why noone mentioned using [`CDATA`](http://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean) as a hacky block comment ... – Martin Ba Apr 18 '16 at 08:36

7 Answers7

59

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.

bluish
  • 26,356
  • 27
  • 122
  • 180
Brent Writes Code
  • 19,075
  • 7
  • 52
  • 56
  • 2
  • 1
    System.Xml.XmlException: This is an invalid comment syntax. – b w Aug 28 '09 at 17:14
  • 2
    It's to ensure compatibility with SGML. – Brent Writes Code May 14 '10 at 22:06
  • 12
    @Brent Nash So how do you comment out a big block that has comments in it? – David Doria Sep 10 '13 at 19:39
  • 1
    @DavidDoria Unfortunately, I don't think you can do it easily. It's like trying to comment out a chunk of code with `/* */` that has another `/* */` nested inside it somewhere. I think your best bet is to find an XML editor that can auto insert/remove comments for you on a per line basis and fix the few outliers yourself. It is unpleasant at best. – Brent Writes Code Sep 11 '13 at 02:16
  • 5
    @BrentNash I'll take your word for it that it's just to ensure backward compatibility :). Still, for a format designed in the 90s, not allowing nested comments is not cool. – Manav Feb 18 '15 at 06:06
  • @BrentWritesCode: Agree with Manav. The spec *should* have allowed for single-line comments like C and BASIC already had for *DECADES* prior. Then, IDEs (like VS already does with C# even though C# has a multi-line comment feature), keep adding more single-line comment prefixes to each line even if multiple lines are marked for commenting. That's a no brainer. The possibility of the resulting HTML, XML, XAML, etc. being in error is no excuse. The same problem exists with any programming language that allows single-line (or even multi-line) comments. It's the programmer's responsibility. – Tom Jul 24 '19 at 18:06
  • @BrentWritesCode: Yes, but XML is all about nesting things! XML is designed so we can describe nested stuff!!! Didn't they think nesting is a good thing??? (twelve years late comment) :-) – André Caldas Nov 27 '21 at 16:44
45

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> 
?>
Community
  • 1
  • 1
psychoslave
  • 2,783
  • 3
  • 27
  • 44
  • This is a great help for cases when badly-behaved NuGet packages mutilate my lovingly curated `web.config` files by adding their own elements and dozens of comments! – Dai Jul 20 '17 at 07:19
5

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.

annakata
  • 74,572
  • 17
  • 113
  • 180
4

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

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}>
Philip Beck
  • 375
  • 2
  • 10
0

Without a workaround described below, it's not allowed because -- can't appear in a comment.

Potential solutions:

  1. Visual Studio Code Nested Comments extension
  2. Notepad++ XML Tools plugin
  3. Using an XML processing instruction
Andrew D. Bond
  • 902
  • 1
  • 11
  • 11
0

Here is how I accomplished the task. It's quick and dirty, but it shows the process. It parses properly and uses the NP++ Block comment/uncomment style

Python:

dummyCoordinates = [(0,0),(1000,0),(1000,10000),(0,10000),]

comment1 = ET.Comment("<CutoutSubdesign>")
xmlTag.insert(1, comment1)

comment2 = ET.Comment("\t<Polygon>")
xmlTag.insert(2, comment2)

idxCount = 3

for X,Y in dummyCoordinates:
    comment = ET.Comment("\t\t<Point x=\"{:.3f}um\" y=\"{:.3f}um\"/>".format(X,Y))
    xmlTag.insert(idxCount, comment)
    idxCount += 1

comment3 = ET.Comment("\t</Polygon>")
xmlTag.insert(idxCount, comment3)

comment4 = ET.Comment("</CutoutSubdesign>")
xmlTag.insert(idxCount + 1, comment4)

Result:

<!--<CutoutSubdesign>-->
<!--    <Polygon>-->
<!--        <Point x="0.000um" y="0.000um"/>-->
<!--        <Point x="1000.000um" y="0.000um"/>-->
<!--        <Point x="1000.000um" y="10000.000um"/>-->
<!--        <Point x="0.000um" y="10000.000um"/>-->
<!--    </Polygon>-->
<!--</CutoutSubdesign>-->
SailorDad
  • 11
  • 2