1. Computing

Discuss in my forum

How to Break Down a Simple Element by Data Type

Stepping through an XML Schema

By

Setting up elements of an XML schema goes beyond just defining a class, simple or complex. Schemas let the author address a number of rules that validate the elements in the corresponding XML document. Elements are containers, much like a file in a cabinet. When creating a filing system, you might restrict the pattern for each drawer or only store particular document formats in one container. Elements work much like a specialized filing system. By creating values and data types, you control the content of the element. When stepping through an XML schema, a closer look at how to break down a simple element will help clarify the process.

Defining the Simple Element Data Type

When you set up a customized filing system, you want everyone to know what goes where. This means labeling the content. In the world of XML, the labeling process is a data type. An XML schema has a number of inherent data types that are common to simple elements.

XML element: <Jane> Welcome to Monday</Jane>
Corresponding Schema definition:
<xs:element name= "Jane" type= "xs:string"/>

This data type is a string, a line of basic text. Other forms of data types include decimal, integer, Boolean, date and time. If the text in element <Jane> were a number, this would be an integer.

Defining Default or Fixed Values

Along with defining the data type, schemas allow you to specify what the text in an element will be. If the text in an element is always the same, the value is fixed. For example, Jane's birthday never changes. 

XML Element: <JaneBday>1970-01-27</JaneBday>
Corresponding Schema definition:
<xs:element name= "JaneBday" type= "xs:date" fixed= "1970-01-27" />

Under the element 'JaneBday' the text must read '1970-01-27.'

Default identifies what the value would be when there is no text. Jane's favorite color is red, but that could change. 

XML Element: <JaneColor>Red</JaneColor>
Corresponding Schema definition:
<xs:element name= "JaneColor" type= "xs:string" default= "Red" />

If the XML document does not define Jane's favorite color, the processor fills in 'red.'

Setting up Facets

A restriction creates a definition that validates the text. Jane's age will never be under zero or over 150 and it will always be an integer. If you want the processor to check the XML document and verify the text falls within the parameters set, you create a facet.

XML Element: <JaneAge>42</JaneAge>
Corresponding Schema definition
<xs:element name= "JaneAge"> - element name
<xs:simpleType> - class
        <xs:restriction base= "xs:integer"> - data type
            <xs:minInclusive value= "0" /> - never less than zero
             <xs:maxInclusive value= "150" /> or more than 150
        </xs:restriction> - closing tags   
    </xs:simpleType>
</xs:element>

This is just one example of the flexibility of facets. Facets have predefined constraint names. For instance, if 'enumeration' was used instead of 'minInclusive' that would establish an acceptable list for a data string. Facets might require some further explanation, but for this discussion, it is important to know they exist and allow you to establish restrictions on text in elements.

Let's Review Some Important Terms

Simple Element
A simple element contains only text. Simple elements do not have attributes or nested elements.

Data Type
A data type defines the element based on the format of the text and supplies validation criteria for the processor.

Fixed
Fixed establishes a set value for the text of an element. When a fixed value is set, the processor always use that value in the XML document under that element name.

Default
Default establishes what the text value will read if the XML document lists no value under an element name. If the XML document defines no text under the element, the processor substitutes the default value.

Facet
Facet is a system for restricting the value of data in an element. A facet establishes criteria for text under a simple element in order to validate it.

If you need to catch up on Stepping Through an XML Schema, check out:
How to Write Your First XML Schema

How to Pick an Element Type - Complex or Simple?

XML Schema Tutorial

©2014 About.com. All rights reserved.