Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML comments and "--"

Tags:

comments

xml

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

like image 831
Vi. Avatar asked May 31 '12 22:05

Vi.


People also ask

Can you have comments in XML?

XML comments are similar to HTML comments. The comments are added as notes or lines for understanding the purpose of an XML code. Comments can be used to include related links, information, and terms. They are visible only in the source code; not in the XML code. Comments may appear anywhere in XML code.

How do you comment out multiple lines in XML?

If you are using Eclipse IDE you can comment out lines in an XML file by highlighting them and press Ctrl+Shift+c.

How are comments represented in XML?

An XML comment encountered outside the document type declaration is represented by the Comment value syntax element. It contains the comment text from the XML message. If the value of the element contains the character sequence --> , the sequence is replaced with the text --&gt; .


2 Answers

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.

like image 135
asawyer Avatar answered Sep 27 '22 18:09

asawyer


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.

like image 39
Daniel Avatar answered Sep 27 '22 18:09

Daniel