Simple Introduction to XML Schema ( XDS )

In the same way we defined the Document Type Definition (DTD), XML Schema (XSD) describes the structure of an XML document. However, the XML schema syntax is expressed in XML and has richer features including inheritance, complex types and possibility to add data constraints and pattern for XML documents.

[info]In case you have NOT seen the DTD, here is a simple tutorial[/info]

Why XML Schema : DTD VS XSD

The main reasons for using XML schema instead of DTD are listed in the table below:

DTD XSD
DTD has its own Syntax : Additional Parser Required XSD is written in XML. Only one XML parser is needed.
Basic Data Types only (#PCDATA, CDATA,..) Richer Data Types (xs:string , xs:decimal , xs:integer , xs:boolean , xs:date , xs:time)
No support for new types Supports creating new data types
No support for inheritance Supports inheritance
No Support for Data Constraints and Restriction Supports Data Constraints, Restrictions, RegEx Pattern…

XML Schema Syntax Summary

XSD, Explanation and XML Examples
Empty Element <xs:element name=”element-name” />
Example : <xs:element name=”br” />
or <xs:element name=”br” ></xs:element>
Empty elements are written as empty elements within the XSD:
<br />
Element <xs:element name=”element-name” type=”data-type”>
Example :
<xs:element name=”title” type=”xs:string”></xs:element>
Elements with only content inside are declared with their corresponding data type:
xs:string , xs:decimal , xs:integer , xs:boolean , xs:date , xs:time
In XML, <title>Java Programming</title>
Default or Fixed Data <xs:element name=”element-name” type=”data-type” default=”value”>..
Or <xs:element name=”element-name” type=”data-type” fixed=”value”>
Example :
<xs:element name=”city” type=”xs:string” default=”York”></xs:element>
Element data can have a default or fixed value inside using
In XML, <city>York</city>
Sub Elements


  
    
      
      
    
  
 
Sub-elements are enclosed within the structure of ComplexType as a sequence


   Feature Extraction
   Mark Nixon

Occurence


  
    
      
      
      
      
    
  
 
The number of occurences is specified using : maxOccurs and minOccurs. For unlimited, we use the keyword: unbounded
The default value for maxOccurs and minOccurs is 1


    Jane
    Tim
    Rich
    John
    Marry

Choice


  
    
      
      
    
  
 
Here we specify that the element Doctor must have a choice of either Hospital or Clinic


    SouthWest Clinic

Attribute for Elements with no data <xs:attribute name=”attribute-name” type=”data-type”/>


  
      
	
      
       
  
 
That’s an attribute of an element without text data inside.


  Feature Extraction 

Attribute for elements with data I <xs:attribute name=”attribute-name” type=”data-type”/>


  
      
	
	    
	      
		
		  
		
	      
	    
	
      
       
  
 
That’s an attribute of an element with data inside. The use of xs:extensionis required with the keyword: xs:simpleContent


  Feature Extraction 

Attribute for elements with data II <xs:attribute name=”attribute-name” type=”data-type”/>


  
      
	
	    
		  
	    
	
      
       
  
 
That’s an attribute of an element with data inside. The use of mixed=”true” to tell that a complex type element can contain attributes, elements, and text.


  Feature Extraction 

Pointers using ref <xs:attribute name=”attribute-name” type=”data-type”/>


  
      
	
      
       
  
 

  
      
	
      
  
 


    
	  
    

The use of ref=”title” to point or reference an element already defined.


  Feature Extraction 


  Need for Speed 7 

Restriction of Element Data

    
      
	
	      
	      
	      
	
      
    

That’s to restrict the element data between a min and max values. Note that attributes can be added between the keyword: xs:restriction

72
Choice of Element Data

    
      
	
	      
	      
	      
	      
	
      
    

That’s to restrict the element data between a set of given choices (Enumeration)

Audi
Regular Expression Pattern

    
      
	
	      
	      
	
      
    

That’s to restrict the element data based on a a regular expression pattern ( [a-z]{4} which means a word containing 4 small letters).

mark
Creating Data Types


  
    
    
  

We have create a new type called persontype. The student element is of type persontype and having two sub-elements.


  Asma
  Bouchrika

Creating Data Types



  
    
    
  



  
    
      
	
          
    
  


To extend a type, we use : xs:extension. The tag :xs:complexContent is used when there is a sequence or choice along with extension. Otherwise if there are no sub-elements, we should use xs:simpleContent.


  Mark
  Nixon
  Uni of Reading


XSD document are usually saved on a separate file with extension (xsd). An example is shown below for a file saved as : family.xsd:


  
    
      
        
        
      
    
  

The corresponding XML file should reference the XSD file as illustrated below:



 Hou
 Asma
 

Questions

1 What’s the difference between simpleContent and complexContent and complexType

2 How to define that an attribute if required or optional.

3 is it possible to integrate an XSD within the same XML document (Standalone=yes ?)

No Responses

Leave a Reply

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