
XML Schema
• One of the challenges of WSDL is describing the data being transferred to and from a web service.
• In the simple example we used with the stock quote the types passed to and from the service were very
simple.
- The client passed in a string that represented a stock symbol.
- The server returned a floating point number (double) to the user.
• Even these simple types can cause systems to have interoperability problems.
- Remember that the client and server may not be written in the same language, or on the same platform
or operating system.
- Different languages have different definitions of basic data types.
• The real definition of the data is not part of the language of implementation; it becomes part of the
language of description.
- If you are implementing in Java, the Java data types must map correctly to something in the WSDL
document.
- The WSDL document is how the service is communicated to clients, so the WSDL’s definition of the
data becomes the key definition to which the implementations must match.
- The client must be able to match that same description.
• The most effective way to describe data passed via XML is to use XML Schema.
- Other ways to define payloads exist (DTD’s), but they are not allowed in web services (WS-I).
- XML Schema is defined by the W3C and is a recommended technology.
- Support for XML Schema is wide. Most programming languages have libraries that support XML
Schema.
• XML Schema is an XML based language for defining “grammar” of an XML message.
- Using XML schema you can limit the contents of an XML document.
- When a document is associated to a schema it can be considered valid or not-valid.
- Valid documents conform to XML Schema.
• To understand how schema works let’s take a quick look at a fairly basic example.
- In this example we want to define a Person to be passed via XML.
- The below is a snippet of the XML we want to pass.
<Person>
<FirstName>Chuck</FirstName>
<LastName>Ciecil</LastName>
<HomeAddress>
<Line1>123 Main St.</Line1>
<Line2>Apartment 203</Line2>
<City>Milwaukee</City>
<State>WI</State>
<Zip>54899</Zip>
</HomeAddress>
</Person>
• The challenge is to make sure everyone passes this data exactly the same way.
- XML Schema will allow us to define what elements can be in what elements.
- XML Schema will allow us to define what attributes can be used.
- XML Schema also allows us to control what the contents of any element may be.
• Looking at an XML Schema document that defines the above person we get the following.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="uri:test"
xmlns="uri:test">
<xsd:element name="HomeAddress">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Line1" type="xsd:string"/>
<xsd:element name="Line2" type="xsd:string"/>
<xsd:element name="City" type="xsd:string"/>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="Zip" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FirstName" type="xsd:string"/>
<xsd:element name="LastName" type="xsd:string"/>
<xsd:element ref="HomeAddress"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
• This schema lays out two global element types, Person and HomeAddress.
• The schema lays out what elements are expected as children of Person.
• The types used are XML Schema types not specific to any programming language.
- The below <City> element will need to conform to the XML Schema definition of a string.
<xsd:element name="City" type="xsd:string"/>
- The below <Zip> element is limited to schema’s definition of an int.
<xsd:element name="Zip" type="xsd:int"/>
• More complicated mappings could have been used.
- The State element could have been limited to valid postal abbreviations by defining an enumeration.
- The HomeAddress element could have used a predefined type of Address type, resulting in more reuse
of the address type.
• Keep in mind the important reasons that XML Schema is used in web services.
- XML Schema is a specification managed by the W3C and is in recommendation status.
- XML Schema is a well-defined, widely accepted technology that is also used in Web Services.
- Nearly all languages have support for Schema validation of XML.
- XML Schema allows very complex definitions of XML payloads.
- XML Schema can communicate a great deal of information without tying you to one particular platform
or programming language.
XML Schema
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services