Well-Formed vs. Valid Documents
April 12, 2002
We learned that if a document follows the XML syntax rules
discussed in the previous section, the document is said to be
well-formed, the minimal requirement to be an XML document.
That is, if a document isn't well-formed, it can't even be called
XML (excepting XML fragments).
Validity
Even if a document is well-formed, however, it may not be
valid. According to the XML specification
(See
http://www.w3.org/TR/REC-xml#sec-documents
and
http://www.w3.org/TR/REC-xml#dt-valid),
A data object is an XML document if it is well-formed, as defined
in this specification. A well-formed XML document may in addition
be valid if it meets certain further constraints. . . . An XML
document is valid if it has an associated document type
declaration and if the document complies with the constraints
expressed in it.
In other words, since a document type declaration (and therefore
a DTD) is optional, only documents that refer to a DTD can be
checked for validity. This makes sense because, following the
well-formedness rules, only indicates adherence to basic
syntactical constraints; it says nothing about meeting the
more stringent requirements of a specific structural model.
To reinforce the difference, let's take another look at the
Employees example from chapter 2. The DTD is repeated here in
for convenience.
Listing 3-4 Employees DTD (employee.dtd)
<!ELEMENT Employees ( Employee+ ) >
<!ELEMENT Employee ( Name, Title, Projects, Email, PhoneNumbers, Address ) >
<!ATTLIST Employee sex NMTOKEN #REQUIRED >
<!ELEMENT Name ( First, Last ) >
<!ELEMENT First ( #PCDATA ) >
<!ELEMENT Last ( #PCDATA ) >
<!ELEMENT Title ( #PCDATA ) >
<!ELEMENT Projects ( Project+ ) >
<!ELEMENT Project ( #PCDATA ) >
<!ELEMENT Email ( #PCDATA ) >
<!ELEMENT PhoneNumbers ( Home, Office, Cell ) >
<!ELEMENT Home ( #PCDATA ) >
<!ELEMENT Office ( #PCDATA ) >
<!ELEMENT Cell ( #PCDATA ) >
<!ELEMENT Address ( Street, City, State, Zip ) >
<!ELEMENT Street ( #PCDATA ) >
<!ELEMENT City ( #PCDATA ) >
<!ELEMENT State ( #PCDATA ) >
<!ELEMENT Zip ( #PCDATA ) >
Now consider the document in Listing 3-5. Is it valid?
Listing 3-5 Employee Example 1 (employee-WF.xml)
<?xml version='1.0' standalone='yes'?>
<Employees>
<Employee sex="female">
<Name>
<First>Betty Jo</First>
<Last>Bialowsky</Last>
<!-- aka: Melanie Haber, Audrey Farber, Susan Underhill, Nancy -->
</Name>
<Title>Project Leader</Title>
<Projects>
<Project>MegaProject</Project>
<Project>SmallProject</Project>
<Project>others</Project>
</Projects>
<Email>BettyJo.Bialowsky@home.com</Email>
<PhoneNumbers>
<Home>555-abc-1235</Home>
<Office>555-xyz-4321</Office>
<Cell>555-pqr-1267</Cell>
</PhoneNumbers>
<Address>
<Street>321 Carmel Court</Street>
<City>Columbia</City>
<State>MD</State>
<Zip>20777</Zip>
</Address>
</Employee>
</Employees>
At first glance, it appears to be valid because it follows the
structural rules of the DTD. However, because it does not contain
a document type declaration, the parser has no DTD to compare the
document instance against in order to determine its validty.
Therefore, it is well formed, but not valid, or at least its
validity cannot be determined. What about the document in
Listing 3-6?
Listing 3-6 Employee Example 2 (employee-Miss-Elt.xml)
<?xml version='1.0' standalone='no'?>
<!DOCTYPE Employees SYSTEM "employee.dtd" >
<Employees>
<Employee sex="female">
<Name>
<First>Betty Jo</First>
<Last>Bialowsky</Last>
<!-- aka: Melanie Haber, Audrey Farber, Susan Underhill, Nancy -->
</Name>
<Projects>
<Project>MegaProject</Project>
<Project>SmallProject</Project>
<Project>others</Project>
</Projects>
<Email>BettyJo.Bialowsky@home.com</Email>
<PhoneNumbers>
<Home>555-abc-1235</Home>
<Office>555-xyz-4321</Office>
</PhoneNumbers>
<Address>
<Street>321 Carmel Court</Street>
<City>Columbia</City>
<State>MD</State>
<Zip>20777</Zip>
</Address>
</Employee>
</Employees>
With the inclusion of a reference to a DTD, a validating parser
can check this instance. It will conclude, however, that there
are two missing elements, namely Title and
Cell, so this document is well formed but invalid.
In fact, most parsers diagnose the problem quite clearly.
For example, the free parser XML Validator
from ElCel Technology (
http://www.elcel.com/products/xmlvalid.html) reports:
employee-Miss-Elts.xml [10:15] : Error: element content invalid. Element
'Projects' is not expected here, expecting 'Title'
employee-Miss-Elts.xml [19:20] : Error: premature end to content of element
'PhoneNumbers'. Expecting child element 'Cell'
This indicates, among other things, that the errors are
detected on lines 10 and 19.
And what about this one in Listing 3-7?
Listing 3-7 Employee Example 3 (employee-BJB.xml)
<?xml version='1.0' standalone='no'?>
<!DOCTYPE Employees SYSTEM "employee.dtd" >
<Employees>
<Employee sex="female">
<Name>
<First>Betty Jo</First>
<Last>Bialowsky</Last>
<!-- aka: Melanie Haber, Audrey Farber, Susan Underhill, Nancy -->
</Name>
<Title>Project Leader</Title>
<Projects>
<Project>MegaProject</Project>
<Project>SmallProject</Project>
<Project>others</Project>
</Projects>
<Email>BettyJo.Bialowsky@home.com</Email>
<PhoneNumbers>
<Home>555-abc-1235</Home>
<Office>555-xyz-4321</Office>
<Cell>555-pqr-1267</Cell>
</PhoneNumbers>
<Address>
<Street>321 Carmel Court</Street>
<City>Columbia</City>
<State>MD</State>
<Zip>20777</Zip>
</Address>
</Employee>
</Employees>
This document is well formed and valid because it matches the
structure defined by the DTD, which is referenced in the document
type declaration.
CDATA Sections
XML Family of Specifications: A Practical Guide
Well-Formed or Toast?
|