|
Well-Formed XML Documents
If an XML document is not understood successfully by an XML processor then the processor
cannot format the document. To handle that, XML documents are subject to two constraints: well
formedness and validity, well formedness being the
basic constraint.
Well-Formed Document
As set by the W3C, for an XML document to be well formed it should follow the document
production containing three parts in the document.
-
A prolog
-
A root element
-
Optional miscellaneous part
The prolog should include an XML declaration such as <?xml
version="1.0"?>. It can also contain a Document Type Definition (DTD).
The root element of a document can hold other elements and
the document should contain exactly one root element. All other elements should be
enclosed within the root element.
The optional miscellaneous part can be made up of XML comments, processing instructions
and whitespaces.
Also the XML document should follow the syntax rules specified in the XML 1.0 recommendation
set by W3C.
An example of a well formed document is listed below :
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<CONSUMER>
<NAME>
<FIRST_NAME>
BEN
</FIRST_NAME>
<LAST_NAME>
HOLLIAKE
</LAST_NAME>
</NAME>
<PURCHASE>
<ORDER>
<ITEM>
DVD
</ITEM>
<QUANTITY>
1
</QUANTITY>
<PRICE>
200
</PRICE>
</ORDER>
</PURCHASE>
</CONSUMER>
<CONSUMER>
<NAME>
<FIRST_NAME>
ADAM
</FIRST_NAME>
<LAST_NAME>
ANDERSON
</LAST_NAME>
</NAME>
<PURCHASE>
<ORDER>
<ITEM>
VCR
</ITEM>
<QUANTITY>
1
</QUANTITY>
<PRICE>
150
</PRICE>
</ORDER>
</PURCHASE>
</CONSUMER>
</DOCUMENT>
|
Understanding the above document for well-formedness:
The document starts with a
prolog, which is the xml declaration.
The First element, which is the root element
is the <DOCUMENT> element which contains all other elements.
Next is the <CONSUMER> element inside
the root element which is for two consumers.
For each consumer, their name is stored in the
<NAME> element which itself contains elements like <FIRST_NAME> and <LAST_NAME>.
The details of the purchases which the consumer
made is stored in the <ORDER> element in the <PURCHASE> element which
in turn contains the elements <ITEM><QUANTITY><PRICE> which records
the item purchased, quantity and price which the consumer purchased.
The document ends with the closing </DOCUMENT>
element.
Data can be stored for as many consumers as wanted and handling such kind of data
is not a problem for the XML processor.
The following are the basic rules that should be kept on mind when creating a Well-Formed
XML document.
-
The document should start with an XML declaration
-
The document should be included with one or more elements
-
For elements that are not empty include start and end tags
-
All elements of the document should be contained within the root element
-
Elements should be nested correctly
Documents like the one above can be extended as long as we can. XML doesn't have any
problem handling such kind of documents, as long as they are wellformed.
|