TechBubbles

XML Schema Basics(XSD)

Introduction

This post gives a basic overview of Schema and how to use in development. schema describes what document contains and content of the document what fields and sub elements it can contain.

Standards for Describing Document

  • DTD : was the First formulized standard.
  • XDR: Much comprehensive standard than DTD.
  • XSD: Currently de facto standard for describing the documents. There are two versions in use 1.0 and 1.1. An XSD schema it self is a document.

XSD Elements

Elements are building blocks for any document, element defines the structure of the document. The following is the syntax to define the element in XSD Schema.

Example:

<xs: element name=”abc” type=”xyz” /> 

  1. An Element must have the name property, the same will appear in the document.
  2. Type property describes about what can be contained in the element when it appears in document. eg: xs:string, xs:integer, xs:boolean or xs:date

Example:

Sample XSD

<xs:element name="Employee_dob"
                    type="xs:date"/>

Sample

<Employee_dob>
     2000-01-12T12:13:14Z
</Employee_dob>

Fixed and Default Properties

The XSD element can contain the Fixed or Default properties.

Default means that if no value specified in the document then application uses the default value specified in the XSD document.

Example:

<xs:element name="Employee_name" type="xs:string" default="unknown"/>

Fixed means the value in the document can only have the value specified in the XSD.

Example:

<xs:element name="Customer_location" type="xs:string" fixed="UK"/> 

Cardinality

Specifies how many times an element can appear can be called Cardinality. it is specified using the attributes minOccurs and maxOccurs. Both attributes can assigned a non-negative value.

The default value for minOccurs and maxOccurs is 1.

Example:

<xs:element name="Employee_hobbies"

                    type="xs:string"

                    minOccurs="2"

                    maxOccurs="10"/>

 

Employee_hobbies element must appear at least twice and not more than 10 times in the document.

 

Simple Types

We can define our own types by modifying the existing types.

example: Define a code, that may be an integer with a max limit.

<xs:element name="Employee" type="xs:string"/> 
Complex Types:  is a container for other elements which specifies child
elements an element can contain.
Example:
<xs:element name="Employee">
    <xs:complexType>
            <xs:sequence>
                <xs:element name="Dob" type="xs:date" />
                <xs:element name="Address" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
</xs:element> 
  1. Created a definition for element Employee
  2. <xs:complexType> is a container for other <xs:elements>
  3. Complex type do not have the type attribute and contains the <xs:sequence> element.
  4. The sequence element specifies that document must appear in the same order they are declared in the XSD Schema.
<Employee>
    <Dob> 2000-01-12T12:13:14Z </Dob>
    <Address> 39 spring field road London </Address>
</Employee>

2 Comments so far

  1. DotNetKicks.com August 28th, 2008 6:29 pm

    XML Schema Overview (XSD with examples)…

    You’ve been kicked (a good thing) - Trackback from DotNetKicks.com…

  2. [...] This post explains how to apply  XML Schema Definition Language(XSD) to Extensible Markup Language (XML) document. Basics of XSD. [...]

Leave a reply

Why ask?