Describe the use of XML schema in Java EE Web services.

[Note]

The W3C XML Schema Definition Language is a way of describing and constraining the content of XML documents.

The XML Schema specification consists of three parts. One part defines a set of simple datatypes, which can be associated with XML element types and attributes; this allows XML software to do a better job of managing dates, numbers, and other special forms of information. The second part of the specification proposes methods for describing the structure and constraining the contents of XML documents, and defines the rules governing schema validation of documents. The third part is a primer that explains what schemas are, how they differ from DTDs, and how one builds a schema.

XML Schema introduces new levels of flexibility that may accelerate the adoption of XML for significant industrial use. For example, a schema author can build a schema that borrows from a previous schema, but overrides it where new unique features are needed. XML Schema allows the author to determine which parts of a document may be validated, or identify parts of a document where a schema may apply. XML Schema also provides a way for users of e-commerce systems to choose which XML Schema they use to validate elements in a given namespace, thus providing better assurance in e-commerce transactions and greater security against unauthorized changes to validation rules. Further, as XML Schema are XML documents themselves, they may be managed by XML authoring tools, or through XSLT.

Let's start from "Hello World!" example:


<?xml version="1.0" encoding="UTF-8" ?>

<greeting>
  Hello World!
</greeting>

					

<?xml version="1.0" encoding="UTF-8" ?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="greeting" type="xsd:string"/>
</xsd:schema>

					

Here is a simple data structure for a purchase order. The order example contains following elements: company name, product identifier, and price.


<?xml version="1.0" encoding="UTF-8" ?>

<order_request>
  <company_name>IBA JV</company_name>
  <product_id>C-0YST</product_id>
  <product_price>500.00</product_price>
</order_request>

					
This example represents pricing data as a floating-point number and company name and product identifier as strings. Agreement among different programs about how to handle data is essential. XML solves data typing issues through the use of XML Schemas. The following example shows how each element in the order request can be designated a specific data type:

<?xml version="1.0" encoding="UTF-8" ?>

<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
  <xsd:element name="order_request">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="company_name" type="xsd:string"/>
        <xsd:element name="product_id" type="xsd:string"/>
        <xsd:element name="product_price" type="xsd:double"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

					

Another example of XML document for more complex purchase order:


<?xml version="1.0" encoding="UTF-8" ?>

<purchase_order order_date="2004-04-07">
  <shipping_address country="US">
    <name>Mikalai Zaikin</name>
    <street>28th Street</street>
    <city>Boulder</city>
    <state>CO</state>
    <zip>80301</zip>
  </shipping_address>
  <items>
    <item part_number="008291">                      
      <product_name>PRESARIO S5140</product_name>
      <quantity>1</quantity>
      <price>898.00</price>
    </item>
    <item part_number="005376">                      
      <product_name>COMPAQ FP7317</product_name>
      <quantity>1</quantity>
      <price>398.00</price>
    </item>    
  </items>
</purchase_order>

					
The XML Schema (one of many possible) for this document will be:

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  
  <xsd:element name="item">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="product_name" type="xsd:string" />        
        <xsd:element name="quantity">
          <xsd:simpleType>
            <xsd:restriction base="xsd:positiveInteger">
              <xsd:maxExclusive value="100"/>
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:element>
        <xsd:element name="price" type="xsd:decimal" />
      </xsd:sequence>
      <xsd:attribute name="part_number" type="xsd:string" use="required" />
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="items">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" minOccurs="1" ref="item" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="purchase_order">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="shipping_address" type="us_address" />
        <xsd:element ref="items" />
      </xsd:sequence>
      <xsd:attribute name="order_date" type="xsd:date" use="required" />
    </xsd:complexType>
  </xsd:element>
   
  <xsd:complexType name="us_address">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" />
      <xsd:element name="street" type="xsd:string" />
      <xsd:element name="city" type="xsd:string" />
      <xsd:element name="state" type="xsd:string" />
      <xsd:element name="zip" type="xsd:string" />
    </xsd:sequence>
    <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US" />
  </xsd:complexType>

</xsd:schema>

					

NOTE: In SOAP 1.2 multireference values may be encoded in-place rather than as "top-level" elements (as in SOAP 1.1):

SOAP 1.1


<e:Books>
  <e:Book>
    <title>SCDJWS 1.4 Study Guide</title>
    <author href="#mz" />
  </e:Book>
  <e:Book>
    <title>SCWCD 1.4 Study Guide</title>
    <author href="#mz" />
  </e:Book>
</e:Books>

<author id="henryford">
  <name>Mikalai Zaikin</name>
</author>
					
					
SOAP 1.2

<e:Books>
  <e:Book>
    <title>SCDJWS 5.0 Study Guide</title>
    <author id="mz" >
      <name>Mikalai Zaikin</name>
    </author>
  </e:Book>
  <e:Book>
    <title>SCWCD 5.0 Study Guide</title>
    <author ref="mz" />
  </e:Book>
</e:Books>
					
					

The following code:

public class PersonName {
    public String firstName;
    public String lastName;
}

public class Person {
    public PersonName name;
    public float age;
    public short height;

    public static boolean compare(Person person1, Person person2);
}				
					
The SOAP 1.2 message for Person.compare(...) call may look like this:
					
<soap:Envelope xmlns:soap='http://www.w3.org/2002/12/soap-envelope'
       soap:encodingStyle='http://www.w3.org/2002/12/soap-encoding'>
    <soap:Body xmlns:ns1='http://example'>
    
        <ns1:compare>
            <person id="mz">
              <name>
                <fistName>Mikalai</firstName>
                <lastName>Zaikin</lastName>
              </name>
              <age>29</age>
              <height>1.78</height>
            <person>            
            <person ref="mz" />
        </ns1:compare>
        
    </soap:Body>
</soap:Envelope>					

					

Default value of minOccurs is 1.

Default value of maxOccurs is 1.

XML Schema defines four main elements:

  1. xsd:element declares an element and assigns it a type.

  2. xsd:attribute declares an attribute and assigns it a type.

  3. xsd:complexType defines a new complex type.

  4. xsd:simpleType defines a new simple type.

Attribute values are always simple types. Attributes are unordered.

It does not matter whether complex type is defined before or after the element declaration as long as it is present in the schema document.

You can derive new simple types from existing types. An xsd:simpleType element defines the subtype. The name attribute of xsd:simpleType assigns a name to the new type, by which it can be referred to in xsd:element type attributes. An xsd:restriction child element derives by restricting the legal values of the base type. An xsd:list child element derives a type as a white space separated list of base type instances. An xsd:union child element derives by combining legal values from multiple base types.

You can derive new simple types types from existing types by restricting the type to a subset of its normal values. An xsd:simpleType element defines the restricted type. The name attribute of xsd:simpleType assigns a name to the new type. An xsd:restriction child element specifies what type is being restricted via its base attribute. Facet children of xsd:restriction specify the constraints on the type. For example, this xsd:simpleType element defines a myYear as any year from 1974 on:


<xsd:simpleType name="myYear">
  <xsd:restriction base="xsd:gYear">
    <xsd:minInclusive value="1974"/>
  </xsd:restriction>
</xsd:simpleType>

					
Then you declare the year element like this:

<xsd:element name="year" type="myYear" />
 
					

Facets include: length, minLength, maxLength, pattern, enumeration, whiteSpace, maxInclusive, maxExclusive, minInclusive, minExclusive, totalDigits, fractionDigits. Not all facets apply to all types.

For example, new string type must contain between 1 and 255 characters:


<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:simpleType name="Str255">
    <xsd:restriction base="xsd:string">
       <xsd:minLength value="1"/>
       <xsd:maxLength value="255"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>
 
					

For example, the new year type must be between 1974 and 2100:


<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:simpleType name="myYear">
    <xsd:restriction base="xsd:gYear">
      <xsd:minInclusive value="1974"/>
      <xsd:maxInclusive value="2100"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>
 
					

The enumeration facet lists all allowed values. Applies to all simple types except boolean. For example, the computer brand name must be one of the following : IBA JV, IBM, COMPAQ, DELL, HP.


<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:simpleType name="computerBrandName">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="IBA JV"/>
      <xsd:enumeration value="IBM"/>
      <xsd:enumeration value="COMPAQ"/>
      <xsd:enumeration value="DELL"/>
      <xsd:enumeration value="HP"/>
    </xsd:restriction>
  </xsd:simpleType> 
</xsd:schema>

					

Each element in the xsd:all group must occur zero or once; that is minOccurs and maxOccurs must each be 0 or 1. The xsd:all group must be the top level element of its type. The xsd:all group may contain only individual element declarations; no choices or sequences. Example:


<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="personName">
    <xsd:all>
      <xsd:element name="firstName"  type="xsd:string"/>
      <xsd:element name="lastName" type="xsd:string"/> 
    </xsd:all>     
  </xsd:complexType>
</xsd:schema>
 
					

xsd:choice requires exactly one of a group of specified elements to appear. The choice can have minOccurs and maxOccurs attributes that adjust this from zero to any given number. Example:


<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="computer">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>    
        <xsd:choice minOccurs="1" maxOccurs="1">
          <xsd:element name="desktop" type="xsd:string"/>
          <xsd:element name="notebook" type="xsd:string"/>
          <xsd:element name="handheld"   type="xsd:string"/>
        </xsd:choice>    
      </xsd:sequence>
    </xsd:complexType>
</xsd:schema>
 
					

xsd:sequence requires each child element it specifies to appear in the specified order. The sequence can have minOccurs and maxOccurs attributes that repeat each sequence zero to any given number of times.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     SCDJWS 5.0 Quiz