Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This

Valid XML Documents

Most XML browsers will check your document to see if it is well formed. Some of them can also check whether it's valid. An XML document is valid if there is a document type definition (DTD) or XML schema associated with it, and if the document complies with that DTD or schema.

A document's DTD or schema specifies the correct syntax of the document, as we'll see in Chapter 3, "Valid Documents: Creating Document Type Definitions," for DTDs and Chapter 5, "Creating XML Schemas," for schemas. For example, DTDs can be stored in a separate file, or they can be stored in the document itself using a <!DOCTYPE> element. Here's an example in which I add a <!DOCTYPE> element to the greeting XML document we developed earlier:

Listing ch01_07.xml

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/css" href="first.css"?>
<!DOCTYPE DOCUMENT [ 
  <!ELEMENT DOCUMENT (GREETING, MESSAGE)> 
  <!ELEMENT GREETING (#PCDATA)>
  <!ELEMENT MESSAGE (#PCDATA)>
]>
<DOCUMENT>
  <GREETING>
    Hello From XML
  </GREETING>
  <MESSAGE>
    Welcome to the wild and woolly world of XML.
  </MESSAGE>
</DOCUMENT>

We'll see more about DTDs in Chapter 3, but this DTD indicates that you can have <GREETING> and <MESSAGE> elements inside a <DOCUMENT> element, that the <DOCUMENT> element is the root element, and that the <GREETING> and <MESSAGE> elements can hold text.

Most XML browsers will check XML documents for well-formedness, but only a few will check for validity. I'll talk more about where to find XML validators later in this chapter.

We've gotten an overview of XML documents now, including how to display them using stylesheets and what a well-formed and valid document is. However, this is only part of the story: Many XML documents are not designed to be displayed in browsers at all—or, even if they are, they are not designed to be used with modern stylesheets (such as browsers that convert XML into industry-specific graphics such as molecular structure, physics equations, or even musical scales). The more powerful use of XML involves parsing an XML document to break it down into its component parts and then handling the resulting data yourself. I'll take a look at a few ways of parsing XML data that are available to us next.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.