Real World XML (2nd Edition)

XML comments are very much like HTML comments. You can use comments to include explanatory notes in your document that are ignored by XML parsers; comments may appear anywhere in a document outside other markup. As in HTML, you start a comment with <!-- and end it with --> . Here's an example:

<?xml version="1.0" encoding="UTF-8"?> <DOCUMENT> <!--Start the document off with a greeting.--> <GREETING> <!--Here's the greeting's text.--> Hello from XML! </GREETING> </DOCUMENT>

You should follow a few rules when adding comments to an XML document:

Comments must not come before an XML declaration. For example, this is incorrect:

<!--Here's my document.--> <?xml version="1.0" encoding="UTF-8"?> <DOCUMENT> <GREETING> Hello from XML! </GREETING> </DOCUMENT>

You can't put a comment inside markup, like this:

<?xml version="1.0" encoding="UTF-8"?> <DOCUMENT <!--Start the document-->> <GREETING> Hello from XML! </GREETING> </DOCUMENT>

You cannot use -- inside a comment because XML parsers look for that string within a comment as indicating the end of the comment. For example, this is incorrect:

<?xml version="1.0" encoding="UTF-8"?> <DOCUMENT> <!--Start the document off--politely--with a greeting.--> <GREETING> Hello from XML! </GREETING> </DOCUMENT>

You can also use comments to remove parts of documents as far as a parser is concerned . For example, here I'm commenting out the <MESSAGE> element:

<?xml version="1.0" encoding="UTF-8"?> <DOCUMENT> <GREETING> Hello From XML </GREETING> <!-- <MESSAGE> Welcome to the wild and woolly world of XML. </MESSAGE> --> </DOCUMENT>

Here's how a parser treats this document:

<?xml version="1.0" encoding="UTF-8"?> <DOCUMENT> <GREETING> Hello From XML </GREETING> </DOCUMENT>

Категории