I was applying markdown comments in the xml comments of a config file when the XmlParser reported that two hyphens (--) are not allowed in xml comments.

Checking the XML Specification, it appears that xml comment isn't designed to contain two hyphens for compatibility reasons with SGML parsers.

Why do SGML parsers disallow double hyphens in comments?

  • 2
    I don't think you will get a better answer than "because that's what the standard says" – jk. May 17 '13 at 12:11
  • Well, it's that's the only answer, then I have no choice :-(, though there might be better answer than that. – OnesimusUnbound May 17 '13 at 13:12

This page outlines quite a bit of the HTML/SGML history, and the rather convoluted rules of those two consecutive hyphens (double dash).

The relevant part about SGML:

To put it simply, the double dash at the start and end of the comment do not start and end the comment. Double dash indicates a change in what the comment is allowed to contain. The first -- starts the comment, and tells the browser that the comment is allowed to contain > characters without ending the comment. The second -- does not end the comment. It tells the browser that if it encounters a > character, it must then end the comment. If another -- is added, then it goes back to allowing the > characters.

  • 7
    The section you're referring to. When I read what the SGML specs intended for -- within the comment, my head spins around on the complexity it will introduce later on. – OnesimusUnbound May 17 '13 at 14:22
  • 1
    The advice to never use -- inside a comment seems good to me. But, is there a standard way of escaping it? Suppose I want to create (and share) an output filter to ensure foo -- bar never causes a problem. Is there an SGML equivalent of foo -\- bar? (I'm sure it's not backslash though!) Or - (see this answer), or something else? If we just replace -- with - or - -, the escaping is not reversible. – fazy Oct 9 '14 at 16:59

Because a double hyphen is the comment delimiter in SGML. The <! starts an SGML instruction, the -- indicates a comment. So basically it is for the same reason that a C++ comment cannot contain */.

  • 2
    I think --> is the comment delimiter. – OnesimusUnbound May 17 '13 at 13:54
  • 11
    No, it is not. <! starts an SGML instruction, > ends it. Within an SGML instruction -- both starts and ends a comment. – Jörg W Mittag May 17 '13 at 14:44
  • 7
    Ahh add your comment to the answer, this is illuminating because it means you could write <!someRelevantSgmlTag -- a comment -- someAttribute="blabla" -- another comment --> and the semantic meaning would be <!someRelevantSgmlTag someAttribute="blabla"> – Jimmy Hoffa May 17 '13 at 15:05
  • 3
    Ah, makes sense. --> is actually two tokens, the -- to delimit comment, and > to end SGML instruction. Now, I have an idea on where <![CDATA[ ... ]]> originated. – OnesimusUnbound May 17 '13 at 15:09
  • I've merged the comment. – Joris Timmermans May 17 '13 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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