Apr 292012
 

(Leer este artículo en español)

XSD is a language used to write specifications of the structure of XML documents. XSD replaces the older DTD language, adding functionality to specify in more detail the target XML structure. Here is a sample XSD document named “note.xsd”:

We can see that an XSD document is itself written in XML.

Making reference to a XSD specification in a XML document

Once an XSD specification has been written we can write an XML document and state in the document itself that it should conform to that specification. This is done by adding some attributes to the root element of the document:

Line 3 states that elements used in the XML document shoud be defined in the “http://www.openalfa.com” namespace.

Line 4 states that also elements from the “http://www.w3.org/2001/XMLSchema-instance”  namespace can be used in the XML document. Where they appear, they must be prefixed by “xsi:”.

In line 5, the value of the xsi:schemaLocation attribute is the location of the XSD document that the XML document should conform to (In our example XML, a document named “note.xsd”). We can see the this attribute is prefixed by “xsi:”, and thus it is defined in the namespace referenced in line 4.

 Simple elements and attributes

In the schema we specify the names elements that can be used in the XML document, and their data types, by means of xs:element entries:

The most common data types are:

      • xs:string
      • xs:decimal
      • xs:integer
      • xs:boolean
      • xs:date
      • xs:time

An element can also have attributes. Attributes are specified in the same way as elements, replacing xs:element with xs:attribute. Examples:

It is also possible to create new data types by imposing restrictions on the valid values of a predefined data type. Examles:

Summary of Restrictions for Datatypes

Constraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

Complex Data Types

Complex data types are defined as elements of type <xs:complexType>, and contain a set of elements of simple or complex data types. These can be modified by means of order indicators such as <xs:sequence>, group indicators and  occurrence indicators, as explained below.

Example of complex data type:

In the example above we can see the definitions of:

      • a complex type named “personinfo”
      • a complex type “fullpersoninfo”, defined as an extension of the “personinfo” type. This is done by means of the <xs:complexContent> and <xs:extension> tags.
      • an element “employee” of type “fullpersoninfo”

Indicators

There are seven indicators that can be used in the definition of a complex data type.

Order Indicators:

    • <xs:all>…</xs:all>  – The elements inside may appear in any order in an instance elemento of this data type.
    • <xs:choice>…</xs:choice> – Only one of the elements inside can be present in an instance of the element.
    • <xs:sequence>…</xs:sequence> – Elements inside an instance of an element of this type must appear in the same order as they appear in the definition.

Occurrence Indicators:

By default, elements defined as part of a complex data type must be present exactly once in an instance element of that type. Attributes maxOccurs and minOccurs modify this requirement:

    • maxOccurs=”n” – The element may appear up to “n” times. If maxOccurs=”unbounded”, there is no limit in the number of occurrences of the element.
    • minOccurs=”n” – The element must appear at least “n” times. minOccurs=”0″ means that the element is optional, and may not be present.

Group Indicators:

    • Group name – We define and name a group of elements by enclosing them in <xs:group name=”groupName”>…</xs:group>.
      Once the element group is defined, it can be referenced inside the definition of a complex data type using the syntax: <xs:group ref=”groupName”/>
    • attributeGroup name – We define and name a group of attributes by enclosing them in  <xs:attributeGroup name=”groupName”>…</xs:attributeGroup>
      Once the attribute group is defined, it can be referenced inside the definition of a complex data type using the syntax: <xs:attributegroup ref=”groupName”/>

 

 Posted by at 7:50 am

  One Response to “Introduction to the XSD (XML Schema Definition) language”

 Leave a Reply

(required)

(required)