XSD Example

A schema can be written in different ways.

An XML Document:

employee.xml:




  Jonas Javis
  
    IT
    
22 B Road
Banglore India
Software Developer Well Experienced 10 10 Team Manager Leader 5 9

Explanation:

In the above XML document, the root element, “employee” contains a required attribute called “employeeid”. There are three different child elements: “name”, “department” and “experience”, of the “employee” element. The “experience” element contains a “title”, an optional “note” element, a “year”, and a “grade” element. It appears twice. To tell the XML parser that this document should be validated against a schema, we included the line: xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” and to specify where the schema resides, the line: xsi:noNamespaceSchemaLocation=”employee.xsd” is included.

Create an XML Schema:

To create a schema for the XML document above, we will start by opening a new file named “employee.xsd” and will simply follow the structure in the XML document to define each element as we find it. Before including the xs:schema element to define a schema, the standard XML declaration should be included.

Example:



...

Explanation:

In the above schema, the standard namespace (xs) is used. The URI associated with this namespace is the Schema language definition. It has the standard value of http://www.w3.org/2001/XMLSchema.

Now, we will define the “employee” element as a complex type, because of the presence of an attribute and other elements. Here, we will use the xs:sequence element to surround the child elements of the “employee” element, in order to define an ordered sequence of sub-elements.

Example:


  
    
     ...
    
  

The “name” element does not contain any attributes or other elements and will thus be defined as a simple type. The namespace prefix associated with XML Schema that indicates a predefined schema data type is the one with which the type (xs:string) is prefixed.

Example:


The elements “department” and “experience” are of the complex type. Let’s define the “department” element first.

Example:


  
    
      
      
      
      
    
  

To define the number of possible occurrences for an element, we can use the maxOccurs and minOccurs attributes. The maximum number of occurrences for an element is specified by maxOccurs while the minimum number of occurrences for an element is specified by minOccurs, both having a default value of 1. To define the “experience” element to appear multiple times inside an “employee” element, we will set the maxOccurs attribute of the “experience” element to “unbounded”. Thus, there can be as many occurrences of the “experience” element as the author wishes. Also, the “note” element here is specified to be optional by setting the minOccurs attribute to zero.

Example:


  
    
      
      
      
      
    
  

Now to declare the attribute of the “employee” element, we will specify use=”required”, since it is a required attribute. We must always declare the attribute at last.

Example:


employee.xsd:





  
    
      
      
        
          
            
            
            
            
          
        
      
      
        
          
            
            
            
            
          
        
      
    
    
  



Divide the Schema:

When documents are complex, the above design method can be difficult to read and maintain, however, it is simple. We will define all elements and attributes first and then refer to them using the ref attribute in the next design method.

New Design: employee.xsd:




















  
    
      
      
      
      
    
  



  
    
      
      
      
      
    
  



  
    
      
      
      
    
    
  



Using Named Types:

We will define the classes or types, to enable us to reuse the element definitions in this design method, by naming the simpleTypes and complexTypes elements and then pointing to them through the type attribute of the element.

Third Design: employee.xsd:





  



  



  



  
    
  



  
    
    
    
    
  



  
    
    
    
    
  



  
    
    
    
  
  





Explanation:

In the above example, we are indicating that the data type is derived from a W3C XML Schema namespace datatype, by using the restriction element. The <xs:restriction base=”xs:string”> fragment implies that the value of the element or attribute must be a string value. Usually, to apply restrictions to elements, the restriction element is used. The below lines indicates that the value of the element or attribute must be a string. Also, the value of the element or attribute must be exactly eight characters in a row, and those characters must be a number from 0 to 9.


  
    
  

Please follow and like us:
Content Protection by DMCA.com