Sams Teach Yourself XML in 21 Days
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- Day 2. Creating XML Documents
- Day 3. Creating Well-Formed XML Documents
- Day 4. Creating Valid XML Documents: DTDs
- Declaring Attributes in DTDs
- Day 6. Creating Valid XML Documents: XML Schemas
- Day 7. Creating Types in XML Schemas
- Part I. In Review
- Day 8. Formatting XML by Using Cascading Style Sheets
- Day 9. Formatting XML by Using XSLT
- Day 10. Working with XSL Formatting Objects
- Part II. In Review
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Day 12. Putting XHTML to Work
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Day 14. Handling XLinks, XPointers, and XForms
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Day 16. Using Java and .NET: DOM
- Day 17. Using Java and .NET: SAX
- Day 18. Working with SOAP and RDF
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Day 20. Working with XML and Databases
- Day 21. Handling XML in .NET
- Part V. In Review
- Appendix A. Quiz Answers
Creating XML Comments
You can use comments to include explanatory and descriptive notes in a document. Comments are ignored by XML parsers and may appear anywhere in a document outside other XML markup. XML comments look very much like HTML comments. As in HTML, you start a comment with <!-- and end it with -->. Here's an example:
<?xml version="1.0" encoding="UTF-8"?>
<!--Here comes the document element...-->
<document>
<!--The next element contains a heading.-->
<heading>
Hello From XML
</heading>
<!--The next element contains the actual message.-->
<message>
This is an XML document!
</message>
</document>
The text inside a comment is ignored by XML processors (unless, with some XML processors, that text includes markup, which is sometimes mistakenly treated as markup).
You're only supposed to use comments outside markup—for example, this is not legal:
<?xml version="1.0" encoding="UTF-8"?>
<document >
<heading <!--This is the heading element-->>
Hello From XML
</heading>
<message>
This is an XML document!
</message>
</document>
You should not use the character sequence -- in the text of a comment, because when some XML processors see that sequence, they assume the comment is ended. For example, don't do this:
<?xml version="1.0" encoding="UTF-8"?>
<document >
<heading>
Hello From XML
</heading>
<!--This is our--friendly--message element-->
<message>
This is an XML document!
</message>
</document>
In particular, the XML 1.0 specification says that comments cannot end with the sequence --->. Also, comments cannot come before an XML declaration (nothing can). So this usage is not legal:
<!--This document contains a message.-->
<?xml version="1.0" encoding="UTF-8"?>
<document >
<heading>
Hello From XML
</heading>
<message>
This is an XML document!
</message>
</document>
It's also worth knowing that in most XML processors, you can use comments to exclude sections of a document from being treated as markup. For example, here's how you might remove the <heading> element as far as an XML processor is concerned:
<?xml version="1.0" encoding="UTF-8"?>
<document >
<!--
<heading>
Hello From XML
</heading>
-->
<message>
This is an XML document!
</message>
</document>
As far as most XML processors are concerned, here's what the content of this XML document looks like:
<?xml version="1.0" encoding="UTF-8"?>
<document >
<message>
This is an XML document!
</message>
</document>
Creating Processing Instructions | Next Section

Account Sign In
View your cart