Introduction to XML part 2

XML

I wrote a post in the past about the basics of XML structure and how to build a DTD to define your document type. DTDs describe well how elements are arranged in a document but say very little about the content in the document. Also any element in a document has to have a corresponding declaration in the DTD. These problems and limitations could be solved using schemas. Schemas support rules (and ability to define content) which should be followed in order for a document to be valid. For example lets say that you need to describe a city element. The schema code should look like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name=”city” type=”xs:string”/>
</xs:schema>

The xs:element element acts like the !ELEMENT declaration in DTDs. Its name declares a name and its type attribute defines the data type. The xs:string describes the data type of the element “city”. Schemas provide many more features. For example lets say that you want to describe a date element. Date should have some restrictions, for example month should be filled with certain numbers (from 1 to 12) and so on. Enhancing these restrictions in a schema is very easy:

<xs:simpleType name="month"
  <xs:restriction base=”xs:integer”>
     <xs:minInclusive value=”1″/>
     <xs:maxInclusive value=”12″/>
  </xs:restriction>
</xs:simpleType>

To sum up, schemas are more powerfull than DTDs and allow more flexible data quality control. DTDs though are commonly used and more easy to structure.

One thought on “Introduction to XML part 2

  1. Seems like schemas is the future in XML usage, but up to now most of XML sublanguages are structured using DTDs! Nice intro though!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.