93
<!-- here is some comment --
                            ^
                            |
                    what can be here apart from '>'?

XML seems not to like '--' inside comments. I read somewhere that '--' switchs some modes inside <! ... > thing, but <!-- -- -- --> (even number of --s) seem to be invalid too. If it is some historic feature, what is "pro" part of it? ("contra" part is inability to have -- in comments).

What is the reason of complicating comment processing by not making just '-->' end of comment and allowing '--' inside?

0

5 Answers 5

69

Maybe it can be helpful for someone. I had a problem, that I wanted to comment out a command line parameter in XML that starts with --:

<arg line="-v --line-break 0" />  

so naturally normal way like this

<!-- <arg line="-v --line-break 0" /> -->

didn't work, but I found out, that if the - is replaced by it's UTF-8 equivalent &#x002D; or &#45; it works and can be tolerated inside comments.

So in my case the string

<arg line="-v &#45;&#45;line-break 0" />

is parsed correctly and can be part of comments.

Of course it looks a little ugly, but if someone want to keep a string with -- as comment in his XML - I think it's still better than nothing.

4
  • 29
    You can also use the escape sequence &#45; which is the ASCII escape version and is a bit shorter than the unicode version. Mar 7, 2014 at 22:12
  • 2
    Heheh, everybody complains, you come up with an elegant and obvious solution. Bravo. Aug 25, 2020 at 10:29
  • 5
    You can also only replace the second hyphen, i.e. replace -- with -&#45;.
    – IS4
    Feb 14, 2021 at 12:11
  • when I open the xml in Firefox hyphens still appear as &#45; instead of - Jul 12 at 14:31
67

From the standards document:

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

[Definition: Comments may appear anywhere in a document outside other markup; in addition, they may appear within the document type declaration at places allowed by the grammar. They are not part of the document's character data; an XML processor may, but need not, make it possible for an application to retrieve the text of comments. For compatibility, the string " -- " (double-hyphen) must not occur within comments.] Parameter entity references must not be recognized within comments.

4
  • So "pro" is compatibility with SGML.
    – Vi.
    May 31, 2012 at 22:36
  • 2
    I'd call it an engineering trade off.
    – asawyer
    May 31, 2012 at 22:38
  • 13
    This should be changed IMO, because we now have css parameters, which start with a double hyphen and can be inside a style attribute in an SVG element. Commenting out these elements will break compatibility with the XML standard, only because of some legacy format that isn't around anymore.
    – Waruyama
    May 9, 2018 at 17:33
  • 3
    Would that it could be changed! Unfortunately, as the painful experience with XML 1.1 shows, changing the spec is one thing; getting people to update parsers written 20 years ago is another. And having the world full of parsers that implement different versions of the standard is not going to help anyone. May 13, 2020 at 8:07
43

It's one of those stupid rules that's in XML because it was in SGML and people didn't want to break compatibility. Why it's in SGML is anyone's guess: probably because it saved three lines of code in the original parser.

35

-- is not allowed for compatibility with SGML. From On SGML and HTML:

White space is not permitted between the markup declaration open delimiter("<!") and the comment open delimiter ("--"), but is permitted between the comment close delimiter ("--") and the markup declaration close delimiter (">"). A common error is to include a string of hyphens ("---") within a comment. Authors should avoid putting two or more adjacent hyphens inside comments.

So in SGML <! and > open and close "markup declarations" and -- opens and closes comments.

0

This problem sooner or later will affect anyone who likes to use comments in XML to disable the content that they don't need. I had major issues for several days with Spring context configs failing to load, without any detail explanation why. Problem was a habit to comment blocks of content like this:

<value>ABC1</value>
<!-- <value>ABC2</value> -->
<value>ABC3</value>

commented out it has to be changed to this:

<!--
    <value>ABC1</value>
    !-- <value>ABC2</value> --
    <value>ABC3</value>
-->

The dashes in the commented out block instead of being ignored, were messing up the parsing of the whole file no matter where they are placed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.