Allows notes and other human readable comments to be included within an XML file. XML Parsers should ignore XML comments.
Some constrains govern where comments may be placed with an XML file.
XML Data | Notes |
<?xml version="1.0" encoding="UTF-8" ?> <!-- My Comment --> <DocumentElement/> |
A Valid Comment |
<!-- My Comment Before the Document Type Tag --> <?xml version="1.0" encoding="UTF-8" ?> <DocumentElement/> |
Invalid, the XML Declaration (if present) must be the first thing in the document. |
<!-- My Comment --> <DocumentElement/> |
This is valid as there is no XML Declaration |
<DocumentElement myAttribute="myAttributeValue"> <!-- My Comment --> <ChildElement>Element Value<ChildElement/> </DocumentElement> |
A Valid Comment |
<DocumentElement myAttribute="my Attribute Value"> <ChildElement> Element <!-- My Comment --> Value <ChildElement/> </DocumentElement> |
A Valid Comment |
<DocumentElement <!-- My Comment --> myAttribute="my Attribute Value"> <ChildElement>Element Value<ChildElement/> </DocumentElement> |
Invalid a comment can not appear within an element tag |
<DocumentElement myAttribute="My <!-- Comment --> Value"> <ChildElement>Element Value<ChildElement/> </DocumentElement> |
Technically this is invalid, but most parsers will treat it as data. According to the W3C Spec the '<' should be escaped using < In either case it will not be treated as a comment. |
<DocumentElement> <![CDATA[ Some <!-- My Comment --> Value ]]> </DocumentElement> |
This is valid, but the value contained within DocumentElement is deemed to Some <!-- My Comment --> Value The comment is not ignored by the parser and becomes part of the value. |
<DocumentElement> <!-- Outer Comment <!-- Inner Comment --> Outer Comment --> </DocumentElement> |
This is invalid, the sequence -- is not allowed to appear within a comment, the second <!-- breaks this rule and invalidates the document. Nested comment are not allowed in XML. |
The syntax for an XML Comment described by the W3C as using EBNF as follows.
[15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'