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 Valid XML Documents
An XML processor will usually check whether your XML document is well-formed, but only some are also capable of checking whether it's valid. An XML document is valid if it adheres to the syntax you've specified for it, and you can specify that syntax in either a Document Type Definition ( DTD ) or an XML schema. We'll see DTDs in Days 4 and 5, and XML schemas in Days 6 and 7.
As an example, you can see how you add a DTD to our XML document in Listing 1.7. DTDs can be separate documents, or they can be built into an XML document as we've done here using a special element named <!DOCTYPE>.
Example 1.7. An XML Document with a DTD (ch01_07.xml)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="ch01_04.css"?>
<!DOCTYPE DOCUMENT [
<!ELEMENT document (heading, message)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT message (#PCDATA)>
]>
<document>
<heading>
Hello From XML
</heading>
<message>
This is an XML document!
</message>
</document>
We'll create DTDs like this one in Day 4, "Creating Valid XML Documents: Document Type Definitions"; briefly, the DTD in Listing 1.7 is the <!DOCTYPE> element, which specifies that the root element, <document>, should contain a <heading> element and a <message> element. We're also specifying that the <heading> and <message> elements may contain text data. Using a DTD like this, you're able to specify the syntax your XML document should obey—what elements should be inside what other elements, what attributes an element can have, and so on—and if an XML processor can perform validation, it can check your document and head off problems (we'll validate this document tomorrow).
Today's discussion has introduced us to the basic XML concepts that we'll need for the coming days. Now it's time to start taking an in-depth look at how XML is used in the real world and what it's good for.
How XML Is Used in the Real World | Next Section

Account Sign In
View your cart