XML technologies / XML / XML in a Nutshell / XML basics / Checking well-formedness of documents

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

Every XML document, without exception, must be well-formed. This means following a set of rules, including these:

  1. Every start-tag must have a matching end-tag.

  2. Elements may nest, but must not overlap.

  3. There must be exactly one root element.

  4. Attribute values must be quoted.

  5. An element must not have two attributes with the same name.

  6. Comments and processing instructions must not appear inside tags.

  7. Character data in an element or attribute must not contain unescaped < or & characters.

This list is far from exhaustive. A document can violate well-formedness rules in many, many ways. The XML reference provides a complete list. Some problems concern constructs we have not yet covered, such as DTDs. Others are extremely unlikely if you follow the examples on this page, such as inserting whitespace between the opening < and the element name in a tag.

Whether an error is minor or serious, likely or unlikely, an XML parser reading the document must report it. It may also report several well-formedness violations it finds. But it must never try to repair the document by inserting whatever it believes the author intended. It cannot simply add missing quotes around attribute values, supply missing end-tags, or ignore a comment inside a start-tag. It must return an error. The aim is to avoid the compatibility wars that raged between early browsers and continue today. Always check an XML document’s well-formedness before publishing it, whether it is a web page, database input, or anything else.

The simplest method is loading the document into an XML-capable browser such as Mozilla. A well-formed document is displayed; otherwise, an error appears.

You can also use an XML parser directly. Most are not intended for end users: they are class libraries embedded in friendlier programs such as Mozilla. If they offer a command-line interface at all, it is minimal and usually poorly documented. Nevertheless, processing several files through it is often faster than loading each into a browser. Once you know about DTDs and schemas, the same tools can also validate documents, something few browsers can do.

Many XML parsers exist in many languages. Here we demonstrate using the GNOME project’s libxml to check well-formedness. This open-source package is written in fairly portable C and runs on most major platforms, including Windows, Linux and Mac OS X. Many Linux distributions include it preinstalled. Other parsers should work similarly, though details differ.

Strictly speaking, libxml is a library, but its package includes a program called xmllint that uses it to check files for well-formedness. xmllint runs like other command-line programs in a Unix shell or DOS window. Its arguments are URLs or filenames of the files to check. Here is the result that xmllint produced when checking an early version of the code example A narrative XML document using attributes. The first output line tells you where the first problem occurs:

% xmllint 2-5.xml
2-5.xml:5: error: Unescaped '<' not allowed in attributes values
   <person geboren='1912/06/23'
   ^
2-5.xml:5: error: attributes construct error
   <person geboren='1912/06/23'
   ^
2-5.xml:5: error: error parsing attribute name
   <person geboren='1912/06/23'
   ^
2-5.xml:5: error: attributes construct error
   <person geboren='1912/06/23'
   ^
2-5.xml:5: error: xmlParseStartTag: problem parsing attributes
   <person geboren='1912/06/23'
   ^
2-5.xml:5: error: Couldn't find end of Start Tag image line 3
   <person geboren='1912/06/23'
   ^

As you can see, an error was found. Here the message was not especially helpful: the real problem was not a < character in an attribute value, but a missing closing quote on the attribute value height. Still, that gave enough information to find and fix the problem. Despite its long output, xmllint reports only the first error. You may therefore need several runs to find and fix all errors. Once the example is well-formed, xmllint simply outputs the document it read:

% xmllint 2-5.xml
<biographie xmlns:xlink="http://www.w3.org/1999/xlink/">

   <bild src="http://www.turing.org.uk/turing/pi1/busgroup.jpg" width="152" height="345"/>

   <absatz><person geboren='1912-06-23' gestorben='1954-06-07'><vorname>Alan</vorname>
...

Now proven well-formed, the document can be passed to a browser, database or other program. Almost every nontrivial handwritten document contains well-formedness violations, which is why checking your work before publication is so important.

  

 

 

 

back to top