Generically, a document that describes what a correct document may contain
Specifically, a W3C Recommendation for an XML-document syntax that describes the permissible contents of XML documents
Created by W3C XML Schema Working Group based on many different submissions
No known patent, trademark, or other IP restrictions
XML Schema Part 1: Structures: http://www.w3.org/TR/xmlschema-1/
XML Schema Part 2: Datatypes: http://www.w3.org/TR/xmlschema-2/
Unusual, non-XML like syntax
No data typing, especially for element content
Limited extensibility
Only marginally compatible with namespaces
Cannot use mixed content and enforce order and number of child elements
Cannot enforce number of child elements without also enforcing order.
(i.e. no &
operator from SGML)
DTDs | Schemas |
---|---|
<!ELEMENT> declaration | xsd:element element |
<!ATTLIST> declaration | xsd:attribute element |
<!NOTATION> declaration | |
<!ENTITY> declaration | |
Data types |
Last call working draft from April 7, 2000
Candidate Recommendation October 24, 2000
<?xml version="1.0"?> <GREETING> Hello XML! </GREETING>
<xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <xsd:element name="GREETING" type="xsd:string"/> </xsd:schema>
xsi:noNamespaceSchemaLocation
attribute on
root element
xsi
prefix is mapped to http://www.w3.org/1999/XMLSchema-instance URI
For example,
<?xml version="1.0"?> <GREETING xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xsi:noNamespaceSchemaLocation="greeting.xsd"> Hello XML! </GREETING>
Other means of connecting schemas to documents are allowed
D:\schemas\examples>java sax.SAX2Count -v greeting2.xml greeting2.xml: 701 ms (1 elems, 1 attrs, 0 spaces, 12 chars)
<?xml version="1.0"?> <GREETING xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xsi:noNamespaceSchemaLocation="greeting.xsd"> <P>Hello XML!</P> </GREETING>
D:\speaking\Software Development 2000 East\schemas\examples>java sax.SAX2Count -v greeting3.xml [Error] greeting3.xml:4:6: Element type "P" must be declared. [Error] greeting3.xml:5:13: Datatype error: In element 'GREETING' : Can not have element children within a simple type content. greeting3.xml: 781 ms (2 elems, 1 attrs, 0 spaces, 14 chars)
The namespace URIs have changed.
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:element name="GREETING" type="xsd:string"/> </xsd:schema>
xsi:noNamespaceSchemaLocation
attribute on
root element
xsi
prefix is mapped to http://www.w3.org/2000/10/XMLSchema-instance URI
For example,
<?xml version="1.0"?> <GREETING xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="greeting.xsd"> Hello XML! </GREETING>
<?xml version="1.0"?> <GREETING xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="greeting.xsd"> <P>Hello XML!</P> </GREETING>
<?xml version="1.0"?> <SONG xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="song.xsd"> <TITLE>Hot Cop</TITLE> <COMPOSER>Jacques Morali</COMPOSER> <COMPOSER>Henri Belolo</COMPOSER> <COMPOSER>Victor Willis</COMPOSER> <PRODUCER>Jacques Morali</PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <LENGTH>6:20</LENGTH> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
Complex types can have child elements and attributes
Simple types cannot have children or attributes
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:element name="SONG" type="songType"/> <xsd:complexType name="songType"> <xsd:element name="TITLE" type="xsd:string"/> <xsd:element name="COMPOSER" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="LENGTH" type="xsd:timeDuration"/> <xsd:element name="YEAR" type="xsd:string"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> </xsd:complexType> </xsd:schema>
xsd:element
declares an element and assigns it a type
xsd:attribute
declares an attribute and assigns it a type
xsd:complexType
defines a new type
D:\speaking\Software Development 2000 East\schemas\examples>java sax.SAX2Count -v hotcop.xml
[Error] hotcop.xml:10:25: Datatype error: java.text.ParseException: Illegal or
misplaced separator.
Here's the problem:
<?xml version="1.0"?>
<SONG xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="song.xsd">
<TITLE>Hot Cop</TITLE>
<COMPOSER>Jacques Morali</COMPOSER>
<COMPOSER>Henri Belolo</COMPOSER>
<COMPOSER>Victor Willis</COMPOSER>
<PRODUCER>Jacques Morali</PRODUCER>
<PUBLISHER>PolyGram Records</PUBLISHER>
<LENGTH>6:20</LENGTH>
<YEAR>1978</YEAR>
<ARTIST>Village People</ARTIST>
</SONG>
This is not in the schema time duration format! which is ISO 8601 "PnYn MnDTnH nMnS, where nY represents the number of years, nM the number of months, nD the number of days, 'T' is the date/time separator, nH the number of hours, nM the number of minutes and nS the number of seconds. The number of seconds can include decimal digits to arbitrary precision. An optional preceding minus sign ('-') is allowed, to indicate a negative duration."
<?xml version="1.0"?> <SONG xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="song.xsd"> <TITLE>Hot Cop</TITLE> <COMPOSER>Jacques Morali</COMPOSER> <COMPOSER>Henri Belolo</COMPOSER> <COMPOSER>Victor Willis</COMPOSER> <PRODUCER>Jacques Morali</PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <LENGTH>P0YT6M20S</LENGTH> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
Xerces doesn't get this one right yet!
Boolean
String
URIs
Numeric types
Time types
XML types
No money types. However, these can be derived
XML Schema Built-In Simple Types | ||
---|---|---|
Name | Type | Examples |
float | IEEE 754 32-bit floating point number | -INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN |
double | IEEE 754 64-bit floating point number | -INF, 1.401E-90, -1E4, -0, 0, 12.78E-2, 12, INF, NaN, 3.4E42 |
decimal | arbitrary precision, decimal numbers | -2.7E400, 5.7E-444, -3.1415292, 0, 7.8, 90200.76, 3.4E1024 |
binary | a binary number made up of zeroes and ones | 10000100111 |
integer | an arbitrarily large or small integer | -500000000000000000000000, -9223372036854775809, -126789, -1, 0, 1, 5, 23, 42, 126789, 9223372036854775808, 456734987324983264987362495809587095720978 |
nonPositiveInteger | an integer less than or equal to zero | 0, -1, -2, -3, -4, -5, ... |
negativeInteger | an integer strictly less than zero | -1, -2, -3, -4, -5, ... |
long | an eight-byte two's complement integer such as Java's
long type |
-9223372036854775808, -12678967543233, -1, 9223372036854775807 |
int | an integer that can be represented as a four-byte,
two's complement number such as Java's int type |
-2147483648, -1, 0, 1, 5, 23, 42, 2147483647 |
short | an integer that can be represented as a two-byte,
two's complement number such as Java's short type |
-32768, -1, 0, 1, 5, 23, 42, 32767 |
byte | an integer that can be represented as a one-byte,
two's complement number such as Java's byte type |
-128, -1, 0, 1, 5, 23, 42, 127 |
nonNegativeInteger | an integer greater than or equal to zero | 0, 1, 2, 3, 4, 5, ... |
unsignedLong | an eight-byte unsigned integer | 0, 1, 2, 3, 4, 5, ...18446744073709551614, 18446744073709551615 |
unsignedInt | a four-byte unsigned integer | 0, 1, 2, 3, 4, 5, ...4294967294, 4294967295 |
unsignedShort | a two-byte unsigned integer | 0, 1, 2, 3, 4, 5, ...65534, 65535 |
unsignedByte | a one-byte unsigned integer | 0, 1, 2, 3, 4, 5, ...254, 255 |
positiveInteger | an integer strictly greater than zero | 1, 2, 3, 4, 5, 6, ... |
XML Schema Built-In Simple Types | ||
---|---|---|
Name | Type | Examples |
timeInstant | a particular moment in Co-Ordinated Universal Time; up to an arbitrarily small fraction of a second | 1999-05-31T13:20:00.000-05:00 |
month | A given month in a given year | 2000-10 |
year | a given year | 2000 |
century | a specified century | 19 |
recurringDate | a date in no particular year, or rather in every year | --10-31 |
recurringDay | a day in no particular month, or rather in every mnonth | ----31 |
timeDuration | a length of time, without fixed endpoints, to an arbitrary fraction of a second | P2000Y10M31DT09H32M7.4312S |
date | a specific day in history | 2000-10-31 |
time | a specific time of day, that recurs every day | 14:30:00.000, 09:30:00.000-05:00 |
XML Schema Built-In Simple Types | ||
---|---|---|
Name | Type | Examples |
ID | XML 1.0 ID attribute type | any XML name that's unique among ID type attributes |
IDREF | XML 1.0 IDREF attribute type | any XML name that's used as an ID type attribute elsewhere in the document |
ENTITY | XML 1.0 ENTITY attribute type | any XML name that's declared as an unparsed entity in the DTD |
NOTATION | XML 1.0 NOTATION attribute type | any XML name that's declared as a notation name in the DTD |
language | valid values for xml:lang as defined in XML 1.0 | en-GB, en-US, fr |
IDREFS | XML 1.0 IDREFS attribute type | a white space separated list of IDREF names |
ENTITIES | XML 1.0 ENTITIES attribute type | a white space separated list of ENTITY names |
NMTOKEN | XML 1.0 NMTOKEN attribute type | 12 are you ready |
NMTOKENS | XML 1.0 NMTOKENS attribute type | a white space separated list of name tokens |
Name | An XML 1.0 Name | set, title, rdf, math, math123, href |
QName | a prefixed name | song:title |
NCName | a local name without any colons | title |
XML Schema Built-In Simple Types | ||
---|---|---|
Name | Type | Examples |
string | Parsed Character Data; #PCDATA | Hot Cop |
boolean | C++'s bool type | true, false, 1, 0 |
uriReference | relative or absolute URI | http://www.w3.org/TR/2000/WD-xmlschema-2-20000407/#timeDuration, /javafaq/reports/JCE1.2.1.html |
<?xml version="1.0"?> <SONG xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="attribute_song.xsd"> <TITLE>Hot Cop</TITLE> <PHOTO ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER>Jacques Morali</COMPOSER> <COMPOSER>Henri Belolo</COMPOSER> <COMPOSER>Victor Willis</COMPOSER> <PRODUCER>Jacques Morali</PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:element name="SONG" type="SongType"/> <!-- An empty element --> <xsd:complexType name="PhotoType"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="SongType"> <xsd:element name="TITLE" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="COMPOSER" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="YEAR" type="xsd:year" minOccurs="1" maxOccurs="1"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> </xsd:complexType> </xsd:schema>
<?xml version="1.0"?> <SONG xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nested_song.xsd"> <TITLE>Hot Cop</TITLE> <PHOTO ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER> <NAME> <GIVEN>Jacques</GIVEN> <FAMILY>Morali</FAMILY> </NAME> </COMPOSER> <COMPOSER> <NAME> <GIVEN>Henri</GIVEN> <FAMILY>Belolo</FAMILY> </NAME> </COMPOSER> <COMPOSER> <NAME> <GIVEN>Victor</GIVEN> <FAMILY>Willis</FAMILY> </NAME> </COMPOSER> <PRODUCER> <NAME> <GIVEN>Jacques</GIVEN> <FAMILY>Morali</FAMILY> </NAME> </PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="ComposerType"> <xsd:element name="NAME"> <xsd:complexType> <xsd:element name="GIVEN" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="FAMILY" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:complexType> </xsd:element> </xsd:complexType> <xsd:complexType name="ProducerType"> <xsd:element name="NAME"> <xsd:complexType> <xsd:element name="GIVEN" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="FAMILY" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:complexType> </xsd:element> </xsd:complexType> <xsd:complexType name="PhotoType"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="SongType"> <xsd:element name="TITLE" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="COMPOSER" type="ComposerType" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="ProducerType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="YEAR" type="xsd:year" minOccurs="1" maxOccurs="1"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> </xsd:complexType> </xsd:schema>
PRODUCER
and COMPOSER
are
really the same type.
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="PersonType"> <xsd:element name="NAME"> <xsd:complexType> <xsd:element name="GIVEN" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="FAMILY" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:complexType> </xsd:element> </xsd:complexType> <xsd:complexType name="SongType"> <xsd:element name="TITLE" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="COMPOSER" type="PersonType" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="PersonType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="YEAR" type="xsd:year" minOccurs="1" maxOccurs="1"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> </xsd:complexType> <xsd:complexType name="PhotoType"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </xsd:schema>
Schemas let you enforce order and appearance of elements in mixed content.
<?xml version="1.0"?> <SONG xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mixed_song.xsd"> <TITLE>Hot Cop</TITLE> <PHOTO ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER> <NAME>Mr. <GIVEN>Jacques</GIVEN> <FAMILY>Morali</FAMILY> Esq.</NAME> </COMPOSER> <COMPOSER> <NAME>Mr. <GIVEN>Henri</GIVEN> L. <FAMILY>Belolo</FAMILY>, M.D.</NAME> </COMPOSER> <COMPOSER> <NAME>Mr. <GIVEN>Victor</GIVEN> C. <FAMILY>Willis</FAMILY></NAME> </COMPOSER> <PRODUCER> <NAME>Mr. <GIVEN>Jacques</GIVEN> S. <FAMILY>Morali</FAMILY></NAME> </PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="PersonType"> <xsd:element name="NAME"> <xsd:complexType content="mixed"> <xsd:element name="GIVEN" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="FAMILY" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:complexType> </xsd:element> </xsd:complexType> <xsd:complexType name="SongType" content="elementOnly"> <xsd:element name="TITLE" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="COMPOSER" type="PersonType" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="PersonType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="YEAR" type="xsd:year" minOccurs="1" maxOccurs="1"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> </xsd:complexType> <xsd:complexType name="PhotoType"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </xsd:schema>
<?xml version="1.0"?> <SONG xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="unordered_song.xsd"> <TITLE>Hot Cop</TITLE> <PHOTO ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER> <NAME><FAMILY>Morali</FAMILY> <GIVEN>Jacques</GIVEN></NAME> </COMPOSER> <COMPOSER> <NAME><GIVEN>Henri</GIVEN> <FAMILY>Belolo</FAMILY></NAME> </COMPOSER> <COMPOSER> <NAME><FAMILY>Willis</FAMILY> <GIVEN>Victor</GIVEN></NAME> </COMPOSER> <PRODUCER> <NAME><GIVEN>Jacques</GIVEN> <FAMILY>Morali</FAMILY></NAME> </PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
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 choice or sequences
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="PersonType"> <xsd:element name="NAME"> <xsd:complexType> <xsd:all> <xsd:element name="GIVEN" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="FAMILY" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:all> </xsd:complexType> </xsd:element> </xsd:complexType> <xsd:complexType name="SongType"> <xsd:element name="TITLE" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="COMPOSER" type="PersonType" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="PersonType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="YEAR" type="xsd:year" minOccurs="1" maxOccurs="1"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> </xsd:complexType> <!-- An empty element --> <xsd:complexType name="PhotoType" content="empty"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </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.
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.
<?xml version="1.0"?> <SONG xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="derived_song.xsd"> <TITLE>Hot Cop</TITLE> <PHOTO ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER> <NAME><FAMILY>Morali</FAMILY> <GIVEN>Jacques</GIVEN></NAME> </COMPOSER> <COMPOSER> <NAME><GIVEN>Henri</GIVEN> <FAMILY>Belolo</FAMILY></NAME> </COMPOSER> <COMPOSER> <NAME><FAMILY>Willis</FAMILY> <GIVEN>Victor</GIVEN></NAME> </COMPOSER> <PRODUCER> <NAME><GIVEN>Jacques</GIVEN> <FAMILY>Morali</FAMILY></NAME> </PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> <PRICE>$1.35</PRICE> </SONG>
Suppose you want a money type to specify that the PRICE
element content must look like $1.35 or ¥11000
Derive this from the xsd:string
type by restriction
Use a regular expression to specify the pattern
More or less Perl-like with some Unicode extensions
The money regular expression:
\p{Sc}\p{Nd}+(\.\p{Nd}\p{Nd})?
\p{Sc}
\p{Nd}
\p{Nd}+
\.
(\.\p{Nd}\p{Nd})
(\.\p{Nd}\p{Nd})?
The base
attribute specifies the type it's derived from
The name
attribute specifies the name it will be referred to as
The pattern
child element imposes the restriction
<xsd:simpleType base="xsd:string" name="money">
<xsd:pattern value="\p{Sc}\p{Nd}+(\.\p{Nd}\p{Nd})?"/>
</xsd:simpleType>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:element name="SONG" type="SongType"/> <xsd:simpleType base="xsd:string" name="money"> <xsd:pattern value="\p{Sc}\p{Nd}+(\.\p{Nd}\p{Nd})?"/> <!-- Regular Expression: \p{Sc} Any Unicode currency indicator; e.g. $, ¥, £, &#A4, etc. \p{Nd} A Unicode decimal digit character \p{Nd}+ One or more Unicode decimal digit characters \. The period character (\.\p{Nd}\p{Nd}) (\.\p{Nd}\p{Nd})? Zero or one strings of the form .35 This works for any decimalized currency. --> </xsd:simpleType> <xsd:complexType name="SongType"> <xsd:element name="TITLE" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="COMPOSER" type="PersonType" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="PersonType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="YEAR" type="xsd:year" minOccurs="1" maxOccurs="1"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRICE" type="money" minOccurs="1" maxOccurs="1"/> </xsd:complexType> <xsd:complexType name="PersonType"> <xsd:element name="NAME"> <xsd:complexType> <xsd:all> <xsd:element name="GIVEN" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="FAMILY" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:all> </xsd:complexType> </xsd:element> </xsd:complexType> <xsd:complexType name="PhotoType"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </xsd:schema>
<?xml version="1.0"?> <GREETING xmlns="http://ibiblio.org/xml/schemas/greeting/" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:schemaLocation="http://ibiblio.org/xml/schemas/greeting/ greeting_defaultNS.xsd"> Hello XML! </GREETING>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" targetNamespace="http://ibiblio.org/xml/schemas/greeting/" > <xsd:element name="GREETING" type="xsd:string"/> </xsd:schema>
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <SONG xmlns="http://ibiblio.org/xml/namespace/song" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:schemaLocation = "http://ibiblio.org/xml/namespace/song namespace_song.xsd" > <TITLE>Hot Cop</TITLE> <PHOTO ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER>Jacques Morali</COMPOSER> <COMPOSER>Henri Belolo</COMPOSER> <COMPOSER>Victor Willis</COMPOSER> <PRODUCER>Jacques Morali</PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" xmlns="http://ibiblio.org/xml/namespace/song" targetNamespace="http://ibiblio.org/xml/namespace/song" elementFormDefault="qualified" attributeFormDefault="unqualified" > <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="SongType"> <xsd:element name="TITLE" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="COMPOSER" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="YEAR" type="xsd:year" minOccurs="1" maxOccurs="1"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> </xsd:complexType> <xsd:complexType name="PhotoType"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </xsd:schema>
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <SONG xmlns="http://ibiblio.org/xml/namespace/song" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:schemaLocation = "http://ibiblio.org/xml/namespace/song namespace_song.xsd http://www.w3.org/1999/xlink xlink.xsd" > <TITLE>Hot Cop</TITLE> <PHOTO xlink:type="simple" xlink:href="hotcop.jpg" ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER>Jacques Morali</COMPOSER> <COMPOSER>Henri Belolo</COMPOSER> <COMPOSER>Victor Willis</COMPOSER> <PRODUCER>Jacques Morali</PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink" targetNamespace="http://www.w3.org/1999/xlink" attributeFormDefault="qualified" > <xsd:attributeGroup name="XLinkAttributes"> <xsd:attribute name="xlink:simple" type="xsd:string"/> <xsd:attribute name="xlink:href" type="xsd:uriReference"/> </xsd:attributeGroup> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" xmlns="http://ibiblio.org/xml/namespace/song" xmlns:xlink="http://www.w3.org/1999/xlink" targetNamespace="http://ibiblio.org/xml/namespace/song" elementFormDefault="qualified" attributeFormDefault="unqualified" > <xsd:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/> <xsd:complexType name="PhotoType"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attributeGroup ref="XLinkAttributes"/> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="SongType"> <xsd:element name="TITLE" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0" maxOccurs="1"/> <xsd:element name="COMPOSER" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0" maxOccurs="1"/> <xsd:element name="YEAR" type="xsd:year" minOccurs="1" maxOccurs="1"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> </xsd:complexType> </xsd:schema>
The top-level xsd:annotation
element describes the schema
Its xsd:documentation
child element describes the schema
for human readers
Its xsd:appInfo
child element describes the schema
for computer programs; e.g. stylesheet instructions
<xsd:annotation>
<xsd:documentation>
Song schema for XML and Java Example at Software Development 2000 East
Copyright 2000 Elliotte Rusty Harold.
</xsd:documentation>
</xsd:annotation>
Cannot declare entities
Parent models
Extra-document validation
Rick Jelliffe's Schematron
Murato Makoto's RELAX
DTDs
Rick Jelliffe's Schematron:
The Schematron differs in basic concept from other schema languages in that it not based on grammars but on finding tree patterns in the parsed document. This approach allows many kinds of structures to be represented which are inconvenient and difficult in grammar-based schema languages.
XSLT/XPath Based
Murato Makoto
JIS standard
Uses W3C Schema data types
This presentation: http://www.ibiblio.org/xml/slides/sd2000east/schemas/
W3C Schema Primer: http://www.w3.org/TR/xmlschema-0/
XML Schema Part 1: Structures: http://www.w3.org/TR/xmlschema-1/
XML Schema Part 2: Datatypes: http://www.w3.org/TR/xmlschema-2/