Stronger tool for XML data model specification than DTD
Offers opportunity how to:
Stronger tool for XML data model specification than DTD
Offers opportunity how to:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
.../...
</xs:schema>
<xs:element name="element_name">
... type definition - placed either right here (so called "local") or referenced one (so called "global")
</xs:element>
<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>
Approximately correspond to C "union" concept.
Result is a simple type.
Base type and values enumeration can be merged.
Example:
<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.
Example
<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>
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 bound to NS with URL http://www.w3.org/2001/XMLSchema
Either <xs:choice> or <xs:all> can be used instead of<xs:sequence>.
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>
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.
Example:
<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>
Example:
<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.
Example:
<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:
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, ...)
Example
<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.
Example:
<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.
Example:
<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">