What Is XML Schema
1. general description
2. how to use
save the schema file as *.xsd.
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:namespaceSchemaLocation="shiporder.xsd"
3. element classification
Content module in schema
empty: no children elements nor text nodes.
simple: only text nodes are accepted.
complex: only subelements are expected.
mixed: both text nodes and subelements can be present
Type module in schema
simple type: simple content and no attribute
complex type: all other cases
4. simple data type
W3C XML Schema provides an extensive set of predefined datatypes.
Data type |
|
description |
||||
xs:string |
|
|
No white space replacement |
|||
xs:normal izedString |
|
No white space collapsing |
||||
xs:token |
|
White space replacement and collapsing. |
||||
xs:language |
accept all the language codes standardized by RFC 1766 |
|||||
xs:NMTOKEN |
a single token (a set of characters without spaces) composed of characters allowed in XML name. |
|||||
xs:Name |
|
similar to xs:NMTOKEN with the additional restriction that the values must start with a letter or the characters ":" or "-". |
||||
|
xs:NCName |
|
without any colons (":") |
|||
|
xs:ID |
must not be any duplicate values in a document. |
||||
|
xs:IDREF |
it must match an ID defined in the same document. |
||||
|
xs:ENTITY |
must match an unparsed entity defined in a DTD. |
||||
xs:QName |
supports the use of namespace-prefixed names. |
|||||
xs:anyURI |
compensate for the differences of format between XML and URIs |
|||||
xs:NOTATION |
It cannot be used directly in a schema, used through user-defined derived datatypes |
|||||
xs:hexBinary |
code binary content as a character string by translating the value of each binary octet into two hexadecimal digits. |
|||||
xs:base64binary |
maps groups of 6 bits into an array of 64 printable characters |
Data type description |
description |
||||||
xs:decimal |
|
represents the decimal numbers. |
|||||
xs:integer |
|
don't have any fractional digits in its lexical or value spaces. |
|||||
|
xs:nonPositiveInteger |
|
0 and negative integer |
||||
xs:negativeInteger |
|
Negative integer |
|||||
xs:nonNegativeInteger |
|
0 and positive integer |
|||||
|
xs:positiveInteger |
|
Positive interger |
||||
xs:unsignedlong |
|
|
|
None signed 64 digits integer |
|||
xs:unsignedInt |
|
|
None signed 32 digits integer |
||||
xs:unsignedShort
|
|
None signed 16 digits integer |
|||||
xs:unsignedByte
|
None signed 8 digits integer |
||||||
xs:long |
|
64 digits integer |
|||||
xs:int |
|
32 digits integer |
|||||
xs:byte |
|
8 digits integer |
|||||
xs:short |
|
16 digits integer |
|||||
xs:double |
|
|
|||||
xs:float |
|
|
|
|
|
|
|
xs:boolean |
|
|
|
|
|
|
True or false |
Data type |
description |
xs:dateTime |
CCYY-MM-DDThh:mm:ss |
xs:date |
Gregorian calendar date |
xs:gYearMonth |
Gregorian calendar month |
xs:gYear |
Gregorian calendar year. |
xs:time |
represents a point in time that recurs every day, accepts an optional time zone definition. |
xs:gDay |
Gregorian calendar day ---DD |
xs:gMonth |
--MM |
xs:gMonthDay |
Gregorian calendar month --MM-DD |
xs:duration |
A period of time PnYnMnDTnHnMnS |
List type |
description |
xs:NMTOKENS |
whitespace-separated list of xs:NMTOKEN. |
xs:IDREFS |
whitespace-separated list of xs:IDREF. |
xs:ENTITIES |
whitespace-separated list of xs:ENTITY. |
5. user defined simpletype
derivation by restriction
A derivation by restriction is done using a xs:restriction element and each facet is defined using a specific element embedded in the xs:restriction element. The datatype on which the restriction is applied is called the base datatype, which can be referenced though a <base> attribute or defined in the xs:restriction element.
<xs:simpleType name="myInteger">
<xs:restriction base="xs:integer">
<xs:minInclusive value="-2"/>
<xs:maxExclusive value="5"/>
</xs:restriction>
</xs:simpleType>
xs:pattern: works on the lexical space; all the other facets constrain the value space.
xs:enumeration: allows definition of a list of possible values.
xs:length: defines a fixed length measured in number of characters (general case) or bytes (xs:hexBinary and xs:base64Binary)
xs:maxLength: defines a maximum length measured in number of characters (general case) or bytes (xs:hexBinary and xs:base64Binary)
xs:minLength: defines a minimum length measured in number of characters (general case) or bytes (hexBinary and base64Binary)
xs:whitespace: defines the way to handle whitespaces. The values of an xs:whiteSpace facet are "preserve" (whitespaces are kept unchanged), "replace" (all the instances of any whitespace are replaced with a space), and "collapse" (leading and trailing whitespaces are removed and all the other sequences of contiguous whitespaces are replaced by a single space).
xs:maxExclusive: defines a maximum value that cannot be reached.
xs:maxInclusive: defines a maximum value that can be reached.
xs:minExclusive: defines a minimum value that cannot be reached
xs:minInclusive: defines a minimum value that can be reached
xs:totalDigits: defines the maximum number of decimal digits.
xs:fractionDigits: specifies the maximum number of decimal digits in the fractional part (after the dot).
derivation by list
use a xs:list element, allows a definition by reference to existing types or embeds a type definition, two definition cannot be mixed.
These facets are xs:length, xs:maxLength, xs:minLength, xs:enumeration and xs:whiteSpace.
<xs:simpleType name="integerList">
<xs:list itemType="xs:integer"/>
</xs:simpleType>
<xs:simpleType name="myIntegerList">
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
derivation by union
using a xs:union element, allowing a definition by reference to existing types or by embedding type definition, Both styles can be mixed
The only two facets that can be applied to a union datatype are xs:pattern and xs:enumeration
<xs:simpleType name="integerOrData">
<xs:union memberTypes="xs:integer xs:date"/>
</xs:simpleType>
<xs:simpleType name="myIntegerUnion">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:integer"/>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="undefined"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
Complex data type is composed of simple type data type.
6. create a schema
<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>George Bush</orderperson>
<shipto>
<name>John Adams</name>
<address>Oxford Street</address>
<city>London</city>
<country>UK</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
Schema for simpleType element
<xs:element name=”” type=””/>
<xs:attribute name=”” type=””/>
Schema for complexType element
define complexType directly
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
ref to complex data type
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Empty content
<xs:element name=””>
<xs:complexType>
<xs:attribute name=”” type=””/>
</xs:complexType>
</xs:element>
Simple content
<xs:element name=””>
<xs:complexType>
<xs:simpleContent>
<xs:restriction base=””>
<xs:attribute name=”” type=””/>
</xs:restribution>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Complex content
<xs:element name=””>
<xs:complexType>
<xs:sequence>
<xs:element name=”” type=””/>
</xs:sequence>
<xs:attribute name=”” type=””/>
</xs:complexType>
</xs:element>
Mixed content
<xs:element name=””>
<xs:complexType mixed=”true”>
<xs:sequence>
<xs:element name=”” type=””/>
</xs:sequence>
<xs:attribute name=”” type=””/>
</xs:complexType>
</xs:element>
Global definition
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- 简易元素的定义 -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<!-- 属性的定义 -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- 复合元素的定义 -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Local definition
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Named type definition.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
</xs:schema>
7. others
Indicator
Order:
* All
* Choice
* Sequence
Occurrence:
* maxOccurs
* minOccurs
Group:
* Group
* attributeGroup
<any> & <anyAttribute>
Element Substitution
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs: any type=”xs:string”/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=”item” type=”xs:string”/>
<shipperto>
<name>xxxxx</name>
<address>xxxx</address>
<city>xxxx</city>
<item>cccc<./item>
</shipperto>