Whitespace and Comments
Home ] Up ] The Document Prolog ] XML Elements ] The Root Element ] XML Attributes ] Attribute vs. Element? ] [ Whitespace and Comments ] XML Character Entities ] CDATA Sections ] Processing Instructions ] Well-Formed Documents ]

 

 

Whitespace

XML, unlike HTML, preserves whitespace in content.

In HTML, if you enter:

<h4>Bilbo      Baggins</h4>

it will render as:

Bilbo Baggins

i.e., the extra whitespace is removed.  (whitespace includes space, tab, and newlines).

XML will not remove whitespace from content in this way.

Comments

XML comments are almost like HTML comments:

<?xml version="1.0" encoding="utf-8"?>
<!-- Here follows an <address> tag
     and it encloses <street>, <city>,
     <state> and <zip> tags -->
<address>
  <street>31 Concord Rd.</street>
  <city>Lower Farthing</city>
  <state>Old Hampshire</state>
  <zip>99999</zip>
</address>

Click here to see how this is rendered in your browser.

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

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.

Here are some rules about where you can locate comments:

  • Comments may not be placed before the XML declaration (Microsoft IE will recognize them, but Firefox does not)
  • Comments may not be placed inside a tag.
  • Comments may be used to surround and hide tags
  • The two hyphen (--) string may not occur inside a comment, except as part of its opening or closing tag.  This also means that you cannot use ---> (with the extra hyphen) to end a comment.

Note that XML comments are not part of the structure or semantics of an XML document.

 
The page was last updated February 19, 2008