79

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> -->
3
  • 1
    ... wondering why noone mentioned using CDATA as a hacky block comment ... Commented Apr 18, 2016 at 8:36
  • @MartinBa CDATA is not comment but real document content. Commented Dec 2, 2024 at 11:09
  • @DavidBalažic - that's why I wrote "hacky" ... depending on what is processing the XML, the additional text node with all the cdata content might be ignored completely. Did I say that it is hacky? :-) Commented Dec 3, 2024 at 19:15

7 Answers 7

60

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.

Sign up to request clarification or add additional context in comments.

8 Comments

<!-- this is not true -- or is it? -->
<!-- it -- is an error --> System.Xml.XmlException: This is an invalid comment syntax.
It's to ensure compatibility with SGML.
@Brent Nash So how do you comment out a big block that has comments in it?
@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.
|
51

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

3 Comments

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!
We all know this is the real answer that OP and everyone landing here looked for.
The only issue is, Visual Studo doesn't allow this. I get an error "Unrecognized element." when I run the web project. But I found out you can select the block and hit the "Comment" button. Everywhere there is a "<!--" it will put "-->" in front of it, and where there is a "-->" it will put "<!--" after it.
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.

Comments

5

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

Comments

4

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.