XML Schema

Representing Data Types: XML Schema

  • A web service client or server written in Java must convert between its internal representation of data and character strings appropriate for representing that data in a SOAP document.
  • Issues
    • what string representation is appropriate
    • are there limits on the number of digits the string can contain
    • Can scientific notation (such as 4.34e4) be used
    • Are negative values only represented using standard mathematical notation (by a leading minus sign, as in -23.43), or can an accounting format (such as (23.43)) be used?
    • how can more general data structures, such as arrays and objects be passed
  • Web service clients and servers must agree on these and other formatting details for effective communication.

 


 

XML Schema
  • To address such issues, the W3C has developed an XML vocabulary known as XML Schema.
  • XML Schema defines a collection of standard data types.
    • Built-In Data Types
  • Each data type definition includes a specification of
    • the range of values that can be represented by the data type (for example, integers ranging from −32,768 to 32,767) and
    • how to represent those values as strings (for example, negative numbers are represented by a leading minus sign).
  • In addition, the XML Schema vocabulary defines elements that can be used to describe data structures.

 


 

Built-In Data Types

  • The data types defined by the XML Schema specification itself are called its built-in data types.
  • Example – xsd:decimal is a reference to the XML Schema built-in type named decimal

<latitude xsi:type=”xsd:decimal”>40.28</latitude>

  • The corresponding markup in theWSDLdocument is

<part name=”latitude” type=”xsd:decimal” />

  • The XML Schema definition of each built-in type provides two kinds of information:
    • The character strings used to represent a value of the given type, and
    • the set of values that may be represented by such strings.
  • XML Schema has a wide variety of data types that correspond to all of Java’s primitive types (except char) – boolean, byte, short, int, long, float, and double
  • It also supports XML DTD attribute-value data types (CDATA, ID, etc.)
  • It defines a variety of other data types for representing
    • arbitrary-precision integer and decimal values (integer and decimal)
    • dates and times (dateTime)
    • URI’s (anyURI)
    • XML qualified names (QName)
    • binary data
    • nonNegativeInteger
  • Within a WSDL document, XML Schema built-in types generally appear within types element and message elements to declare the data type for an operation parameter or return value.
  • Note – elements must be qualified with the prefix corresponding to the XML Schema document namespace (xsi or xsd)
  • Table 9.1 shows the mappings from the Java API classes to the XML Schema types used to represent their values within a SOAP document.


 

XML Schemas – Simple and Complex types

  • In addition to built-in types, XML Schema defines elements that can be used to define new data types.
  • There are two classes of XML Schema data types: simple and complex.
  • Simple type
    • used to represent individual values
    • values are represented in XML documents by character data
  • Complex type
    • used to represent structured data
    • Values are represented using markup.
  • Within a WSDL document, definitions for both simple and complex types are contained within the types element.
  • These user-defined types are then used in message elements to specify the types of operation parameters and return values that do not belong to one of the built-in types.

 


 

User-Defined Simple Types

  • In XML Schema it is possible to specify one or more restrictions on the values of a simple built-in type.
  • Example – restricts the string built-in data type to just four possible values:

<simpleType name=”memberType”>

<restriction base=”string”>

<enumeration value=”gold” />

<enumeration value=”member” />

</restriction>

</simpleType>

  • All built-in data types have various facets that can be used within a restriction to form a simple user-defined type.
  • These include
    • enumeration element, length, minLength, and maxLength, which apply to string-oriented data types such as string, QName, anyURI, and ID;
    • minInclusive, maxInclusive, minExclusive, and maxExclusive, which apply to numeric and time-oriented data types;
    • totalDigits and fractionDigits, which apply to most numeric data types.
  • Note – Multiple facets may be used to define a single restricted type.
  • For example,

<simpleType name=”priorityType”>

<restriction base=”int”>

<minExclusive value=”10″ />

<maxInclusive value=”100″ />

</restriction>

</simpleType>

 


 

User-Defined Complex Types

  • XML Schema element complexType is the used to define a type whose values are represented in an XML document using markup.
  • For example, defining an XML Schema userdefined complex type named ExchangeValues.

<anExchangeValue xsi:type=”ExchangeValues“>

<dollars>1.0</dollars>

<euros>0.746826</euros>

<yen>102.56</yen>

</anExchangeValue>

  • the element of type ExchangeValues contains three elements (dollars, euros, and yen) as content. The complexType markup is specified as

<complexType name=”ExchangeValues”>

<sequence>

<element name=”dollars” type=”double”/>

<element name=”euros” type=”double”/>

<element name=”yen” type=”double”/>

</sequence>

</complexType>

  • Attributes of complexType
    • Name
    • Type
    • minOccurs, maxOccurs (number of occurrence of element)
    • sequence (exact sequence of appearance)
    • all (any order/sequence of appearance)

 


 

XML Schema within Instance Documents

 

  • A primary purpose of the XML schema within a WSDL document is to specify the form of some of the content of SOAP documents passed between a web service client and server.
  • Portions of SOAP documents can therefore be viewed as XML schema instances.
  • XMLSchema has two namespaces:
    • instance namespace – http://www.w3.org/2001/XMLSchema-instance
      • namespace prefix – xsi
    • document namespace http://www.w3.org/2001/XMLSchema.
      • Namespace prefix – xsd
  • XML Schema allows to specify nil value for an attribute rather than leave the element out of the document. Example

<optArg xsi:nil=”true”></optArg>

  • The most often used attribute in the XML Schema instance namespace is type. Example

<latitude xsi:type=”xsd:decimal”>40.28</latitude>

<longitude xsi:type=”xsd:decimal”>-79.49</longitude>

  • The schemaLocation attribute can be used within an instance document to specify a URL for the XML schema that defines the instance vocabulary. Example

<web-app

xmlns=”http://java.sun.com/xml/ns/j2ee”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”

version=”2.4″>