SCDJWS Study Guide: XML Basic


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

XML Comments

Comments provide a way to insert into an XML document text that isn't really part of the document, but rather is intended for people who are reading the XML source itself.

You can add comments to an XML document just as you can add comments to the HTML document. Comment starts with the string <!--and end with the string -->, as shown here:

<tree>
<type>ever green</type>
<!--planting information-->
<season>spring</season>
<sunshine>medium</sunshine>
</tree>

The XML specification states that an XML parser doesn't need to pass these comments on to the application, meaning that you should never count on being able to use the information inside a comment from your application.

Comments in XML documents must follow these rules:

  • Can't have a comment inside a tag. The following statement is illegal:

<season></season <!--early summer is also fine--> >

  • Can't use "-" or "--" inside a comment. The following statement is also illegal:

<!--early summer is also fine -- but not recommended-->

  • Can't place a comment inside of an entity declaration.
  • Can't put a comment inside another comment.
  • Can't place a comment before the XML declaration that must always be the first line in any XML document.
  • Comments can be used to comment out tag sets. When comment out blocks of tags, make sure that the remaining XML is well-formed. In the following example, the season and sunshine elements are ignored:

<tree>
<type>ever green</type>
<!--planting information-->
<!--commented out for testing
<season>spring</season>
<sunshine>medium</sunshine>
-->
</tree>



Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |