<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
.../...
</xs:schema>
(C) 2018 Masaryk University -- Tomáš Pitner, Luděk Bártek, Adam Rambousek
Using W3C XML Schema Tutorial brief
XML Schema Tutorial at W3Schools
More comprehensive complete tutorial at xfront.com
How to add XML Schema support to Netbeans IDE available at Geertjan’s Blog
Try this XML Schema online validator or
Similar but more general Validome validator (not only XML schema) alternativelly available in web archive.
Stronger tool for XML data model specification than DTD, it allows:
Separate type (e.g. element type) from its occurrence (i.e. element with particular name)
More primitive data types.
Allows to use namespaces.
Allows to specify content model (elements) more accurate way.
Allows new type inheritance.
Allows modular schema design and schema reuse.
XML Schema has an XML syntax.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
.../...
</xs:schema>
<xs:element name="element_name">
<!-- here comes the type definition --
placed either right here (so called "local")
or as a referenced one (so called "global") -->
</xs:element>
short type reference:
<xs:element name="element_name" type="element_type"/>
Does not contain any child elements. Can be used like either element or attribute type.
Possible to define using an existing type restriction
<xs:simpleType name="TypeName">
<xs:restriction base="BaseTypeName"> ... </xs:restriction>
</xs:simpleType>
Content length restriction
<xs:simpleType name="nameType">
<xs:restriction base="xs:string"> <xs:maxLength value="32"/>
</xs:restriction> </xs:simpleType>
Content restriction using a regular expression
<xs:simpleType name="isbnType">
<xs:restriction base="xs:string"> <xs:pattern value="[0-9]{10}"/> </xs:restriction>
</xs:simpleType>
union
Approximately correspond to C "union" concept.
Result is a simple type.
Base type and values enumeration can be merged.
<xs:simpleType name="isbnType">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{10}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="TBD"/>
<xs:enumeration value="NA"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
Type can be defined as a values list separated by white-spaces.
The number of elements list limitation can used as a next derivation type.
<xs:simpleType name="isbnTypes">
<xs:list itemType="isbnType"/>
</xs:simpleType>
<xs:simpleType name="isbnTypes10">
<xs:restriction base="isbnTypes">
<xs:minLength value="1"/>
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="TypeName">
<xs:sequence>
<xs:element ...> ...
<xs:attribute ...>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:choice>
and <xs:all>
can be used instead of sequence.
The group element can be used to define complex type.
Group of elements:
<xs:group name="GroupName">
<xs:sequence>
<xs:element ... /> ...
</xs:sequence>
</xs:group>
<xs:choice>
and <xs:all>
can be used instead of sequence.
Attribute group:
<xs:attributeGroup name="AttributesGroupName">
<xs:attribute ... use="required"/>
...
</xs:attributeGroup>
The mandatory occurrence may be specified (use="required"
).
Example of elements/attributes groups use:
<xs:complexType name="bookType">
<xs:sequence>
<xs:group ref="mainBookElements"/>
<xs:element name="character" type="characterType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attributeGroup ref="bookAttributes"/>
</xs:complexType>
sequence
Defines occurrence of elements in the predefined order.
<xs:element name="element_name">
<xs:complexType>
<xs:sequence>
.../...
</xs:sequence>
.../...
</xs:complexType>
</xs:element>
sequence is a content model that allows occurrence of the defined sequence of child elements.
xs
prefix is (as usually) bound to the NS with URL http://www.w3.org/2001/XMLSchema
Either <xs:choice>
or <xs:all>
can be used instead of <xs:sequence>
.
choice
Defines the occurrence of only one of the specified child elements or groups of elements.
<xs:element name="element_name">
<xs:complexType>
<xs:choice>
.../...
</xs:choice>
.../...
</xs:complexType>
</xs:element>
all
Defines occurrence of child elements without definition of their order.
May appear on the definition top level only.
The cardinality of child elements can be one at most.
<xs:complexType name="bookType">
<xs:all>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="character"type="characterType" minOccurs="0" maxOccurs="1"/>
</xs:all>
<xs:attribute name="isbn" type="isbnType" use="required"/>
</xs:complexType>
<xs:element name="book">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="isbn" type="isbnType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
The text content (textual child nodes) can not be validated.
The child elements can be validated.
<xs:element name="book">
<xs:complexType mixed="true">
<xs:all>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
</xs:all>
<xs:attribute name="isbn" type="xs:string"/>
</xs:complexType>
</xs:element>
Possibility to specify integrity limitations:
value is unique — xs:unique
value is a key — xs:key
value is a key reference — xs:keyref
Annotation is a human-readable note (comment) of a schema or its part.
It may contain the processing information, see example xs:appinfo
as
well.
Next content is not specified (limited), see example (bind
, class
, …)
<xs:annotation>
<xs:documentation xml:lang="en">Top level element.</xs:documentation>
<xs:documentation xml:lang="fr">Element racine.</xs:documentation>
<xs:appinfo source="http://example.com/foo/">
<bind xmlns="http://example.com/bar/">
<class name="Book"/>
</bind>
</xs:appinfo>
</xs:annotation>
Direct:
<xs:include schemaLocation="character.xsd"/>
With redefinition:
<xs:redefine schemaLocation="character12.xsd">
<xs:simpleType name="nameType">
<xs:restriction base="xs:string">
<xs:maxLength value="40"/>
</xs:restriction>
</xs:simpleType>
</xs:redefine>
abstract
Type can not be instantiated.
Can be used for inheritance derivation only.
final
Type can not be extended/derived by inheritance.
<xs:schema targetNamespace="http://example.org/ns/books/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:bk="http://example.org/ns/books/" elementFormDefault="qualified"
attributeFormDefault="unqualified">
.../...
</xs:schema>
XML Schema allows to use some elements that are not known prior to its use.
<xs:complexType name="descType" mixed="true">
<xs:sequence>
<xs:any namespace="http://www.w3.org/1999/xhtml"
processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
Use xs:anyAttribute
for attributes.
<book isbn="0836217462"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:library.xsd">
<book isbn="0836217462" xmlns="http://example.org/ns/books/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="file:library.xsd">