Introduction to XML part 2

November 3rd, 2007

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.

Tags: , , , ,

Related posts:
5 tips for a nice-looking favicon
Create a Web 2.0 logo using Abobe Photoshop
Web 2.0 is here. What about Web 3.0?
Create a web 2.0 like button in Photoshop part 2: Round Button
Create a Web 2.0 logo using Abobe Photoshop part 2

One Response to “Introduction to XML part 2”

  1. kostas Says:

    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

TopTOP