1. Home
  2. Computing & Technology
  3. Web Design / HTML

Converting an XML File to be Valid
Now That it's Well-Formed, Let's Make it Valid

By , About.com Guide

A valid XML document is validated against a Document Type Definition (DTD) or XML Schema. These are a set of rules created by the developer or a standards organization that define the semantics of the XML document. These tell the computer what to do with the markup.

In the case of the About Markup Language, since this is not a standard XML language, like XHTML or SMIL, the DTD would be created by the developer. That DTD would most likely be on the same server as the XML document, and referenced at the top of the document.

Before you start developing a DTD or Schema for your documents, you should realize that simply through being well-formed, an XML document is self-describing, and thus doesn't need a DTD.

For example, with our well-formed AML document, there are the following tags:

  • <newsletter>
  • <long_title>
  • <filename>
  • <section1_linktitle1>
  • <section1_url1>
  • <section1_annotation1>

If you are familiar with the Web Writer newsletter, you may recognize the different sections of the newsletter. This makes it very easy to create new XML documents using the same standard format. I know that I would always put the full long title in the <long_title></long_title> tag, and the first section URL in the <section1_url1></section1_url1> tag.

DTDs

If you are required to write a valid XML document, either to use the data or to process it, you would include it in your document with the <!DOCTYPE> tag. In this tag, you define the base XML tag in the document, and the location of the DTD (usually a Web URI). For example:

<!DOCTYPE newsletter SYSTEM "aml-newsletter.dtd">

One nice thing about DTD declarations is that you can declare that a DTD is local to the system where the XML document is with the "SYSTEM". You can also point to a public DTD, such as with an HTML 4.0 document:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

When you use both, you are telling the document to use a specific DTD (the public identifier) and where to find it (the system identifier).

Finally, you can include an internal DTD directly in the document, within the DOCTYPE tag. For example (this is not a complete DTD for the AML document):

<!DOCTYPE newsletter
[
  <!ENTITY long_title (#PCDATA)>
  <!ENTITY filename (#PCDATA>
  <!ENTITY volume (#PCDATA)>
  <!ENTITY meta_title (#PCDATA)>
  <!ENTITY meta_description (#PCDATA)>
  <!ENTITY meta_keywords (#PCDATA)>
  <!ENTITY section1 (section1_title | section1_linktitle1 | section1_url1 | section1_annotation1 | section1_toc1>
]>

XML Schema

In order to create a valid XML document, you can also use an XML Schema document to define your XML. XML Schema is an XML document that describes XML documents. Learn how to write a schema.

Note

Just pointing to a DTD or XML Schema is not enough. The XML that is in the document must follow the rules in the DTD or Schema. Using a validating parser is a simple way to check that your XML is following the DTD rules. You can find many such parsers online.

First page > Making it Well-Formed > Page 1, 2

Previous Features

Explore Web Design / HTML
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Web Design / HTML
  4. XML
  5. Converting an XML File to be Valid>

©2009 About.com, a part of The New York Times Company.

All rights reserved.