<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->
Section 5. XML Schema
Validation using schemas requires two documents: the schema document and the instance document.
<?xml version="1.0"?>
<memories
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='memory.xsd'>
<memory tapeid="idnum">
...
noNamespaceSchemaLocation attribute to determine the location.
Structure of a schema document
< xsd:schema xmlns:xsd =" http://www.w3.org/2001/XMLSchema ">
< xsd:element name =" memories ">
< xsd:complexType >
< xsd:sequence >
< xsd:element name =" memory " maxOccurs =" unbounded " type =" memoryType "/>
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
< xsd:complexType name =" memoryType ">
< xsd:sequence >
< xsd:element name =" media ">
< xsd:complexType >
< xsd:attribute name =" mediaid " type =" xsd:string " />
< xsd:attribute name =" status " type =" xsd:string " />
</ xsd:complexType >
</ xsd:element >
< xsd:element name =" subdate " type =" xsd:string "/>
< xsd:element name =" donor " type =" xsd:string "/>
< xsd:element name =" subject " type =" xsd:string "/>
< xsd:element name =" location " type =" locationType " />
</ xsd:sequence >
< xsd:attribute name =" tapeid " type =" xsd:string " />
< xsd:attribute name =" status " type =" xsd:string " />
</ xsd:complexType >
< xsd:complexType name =" locationType ">
< xsd:choice >
< xsd:element name =" description " type =" xsd:string " />
< xsd:element name =" place " type =" xsd:string " />
</ xsd:choice >
</ xsd:complexType >
</ xsd:schema >
Elements and built-in types
< xsd:element name =" subdate " type =" xsd:date "/>
The subdate element has been further constrained, yyyy-mm-dd, as defined in the W3C XML Schema Recommendation.
Forty-two simple types are defined as part of the recommendation, including
string, int, date, decimal, boolean, timeDuration, and uriReference.
Simple types: restricting values
In addition to the built-in simple types, you can create new simple types.
For example: idNumber:
< xsd:simpleType name =" idNumber ">
< xsd:restriction base =" xsd:integer ">
< xsd:minInclusive value =" 1 " />
< xsd:maxInclusive value =" 100000 " />
</ xsd:restriction >
</ xsd:simpleType >
Simple types: enumeration
< xsd:simpleType name =" mediaType ">
< xsd:restriction base =" xsd:string ">
< xsd:enumeration value =" 8mm " />
< xsd:enumeration value =" vhs " />
< xsd:enumeration value =" vhsc " />
< xsd:enumeration value =" digital " />
< xsd:enumeration value =" audio " />
</ xsd:restriction >
</ xsd:simpleType >
Complex types: attributes
< xsd:element name =" media ">
< xsd:complexType >
< xsd:attribute name =" mediaid " type =" xsd:integer " />
< xsd:attribute name =" status " type =" mediaType " />
</ xsd:complexType >
</ xsd:element >
In this case, the media element now has two attributes, including one that follows the enumerated mediaType
Complex types: elements
< xsd:element name =" memories ">
< xsd:complexType >
< xsd:sequence >
< xsd:element name =" memory " type =" memoryType "/>
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
This example shows only one child element, but you still need sequence element.The type memoryType combines many of the techniques seen so far:
< xsd:complexType name =" memoryType ">
< xsd:sequence >
< xsd:element name =" media ">
< xsd:complexType >
< xsd:attribute name =" mediaid " type =" xsd:integer " />
< xsd:attribute name =" status " type =" mediaType " />
</ xsd:complexType >
</ xsd:element >
< xsd:element name =" subdate " type =" xsd:date "/>
< xsd:element name =" donor " type =" xsd:string "/>
< xsd:element name =" subject " type =" xsd:string "/>
< xsd:element name =" location " type =" locationType " />
</ xsd:sequence >
< xsd:attribute name =" tapeid " type =" idNumber " />
< xsd:attribute name =" status " type =" xsd:string " />
</ xsd:complexType >
The memoryType type also includes a reference to the locationType , which allows the user to choose between potential children for an element.
Element choices
The sequence element shows all of the possible children of an element. The choice element can allow you choose only one element from a list of alternatives.
< xsd:complexType name =" locationType ">
< xsd:choice >
< xsd:element name =" description " type =" xsd:string " />
< xsd:element name =" place " type =" xsd:string " />
</ xsd:choice >
</ xsd:complexType >
Optional and repeated elements
At this point all elements and attributes added to the schema must appear exactly once. Using minOccurs and maxOccurs , you can control whether or not an item must appear, and whether or not it may repeat.
< xsd:element name =" subject " minOccurs =" 1 " maxOccurs =" 5 " type =" xsd:string "/>
subject element can appear as many as five times within a single element.
If you don’t want an upper limit.
< xsd:element name =" memories ">
< xsd:complexType >
< xsd:sequence >
< xsd:element name =" memory " minOccurs =" 0 " maxOccurs =" unbounded " type =" memoryType "/>
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >
Mixed content in schemas
Sometimes an element contains mixed content , as opposed to just elements or just text. For example, the subject element might contain HTML markup:
< subject >
A reading of Charles Dickens' < i > A Christmas Carol </ i > .
Absolutely < b > marvelous </ b > !
</ subject >
This content could not be described as xsd:string because it contains elements. I and b cannot as element because they contain text. Instead, the subject element must be defined as mixed:
< xsd:element name =" subject ">
< xsd:complexType mixed =" true ">
< xsd:sequence >
< xsd:element name =" i " minOccurs =" 0 " maxOccurs =" unbounded " type =" xsd:string " />
< xsd:element name =" b " minOccurs =" 0 " maxOccurs =" unbounded " type =" xsd:string " />
</ xsd:sequence >
</ xsd:complexType >
</ xsd:element >