XML syntax requires proper nesting of tags. Here is an example of properly nested and improperly nested tags.
Properly Nested Tags
<Brew>Wait, impatiently!!</Brew> <Dispense MugSize="24" UnitMeasure="Ounces">Pour, finally. </Dispense>
Improperly Nested Tags
<Brew>Wait, impatiently!! <Dispense MugSize="24" UnitMeasure="Ounces">Pour, finally.</Brew> </Dispense>
The second markup is not well-formed because <Dispense> opens before the closing tag, </Brew> is encoded. The open and close tags are crossed. The coffee brewing instructions would not parse, resulting in no coffee and that would be disastrous!
Validity is another important XML concept. A document instance is valid when it parses successfully against its accompanying DTD or schema. As mentioned XML does not require the use of a DTD, but when a document instance invokes a DTD or schema, then a parser will validate the document instance against it. If it parses successfully, the document is both well-formed and valid. A document instance can be well-formed, but not valid if it has a DTD and violates the rules of its DTD. For example, its schema may require an element to be numeric only. If the numeric only element contains an alpha character, it would be invalid. Parsed without invoking its DTD and it could very likely be well-formed. If a document instance is valid, then it is always well-formed.
Early in the development of XML, “namespaces” was introduced to allow for the resolution of ambiguous elements and attributes that inevitably occurs as information zooms around the Internet or even a corporate intranet. Extending our coffee example illustrates a simple case of namespace use.
<?xml version="1.0"?> <Startday xmlns:USA="http://coffee.org/usa/measures" xmlns:UK="http://coffee.org/uk/measures"> <USA:ounces>24</ounces> <UK:ounces>24</ounces> </Startday>
In this example, using the namespace syntax disambiguates the “ounces.” The first instance of “ounces” has an element value 24, but the namespace syntax tells the parser to use the definition of “ounces” found at the url pointing to the USA subdirectory. The second “ounces” element points to the subdirectory of UK. At each unique url, the parser will find a definition for the element, “ounces.” It is possible that both schemas define “ounces’ similarly, but without the schema or DTD, the element remains ambiguous to the parser.
XML does not require a DTD, but DTD’s are very useful. When a DTD accompanies its document instance, the recipient can do much more with the document. The DTD defines the elements, attributes, and entities used within the document instance. This enhances understanding and reduces ambiguity. An XML DTD allows for much of the functionality described above.
In addition to elements, SGML and XML allow for attributes and entities. Briefly, an attribute is meta data about an element.
<Beans Grind="perc" Type="Java">
“Grind” and “Type” are attributes and provide additional descriptive and processing information about the element, “Beans. The element “Beans” could appear in the document instance as, <Beans>, but with the addition of the attributes, you gain greater understanding of the element.