XSD
Definition
XSD
= XML Schema Definition
more expressive than DTDs
same syntax as XML
example
<person> <fullname> Mantas Å imkus </fullname> <tel> 18441 </tel> </person>
<!ELEMENT person (fullname, tel)> <!ELEMENT fullname (#PCDATA)> <!ELEMENT tel (#PCDATA)>
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!--xsd as namespace-prefix--> <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="fullname" type="xsd:string"/> <xsd:element name="tel" type="xsd:positiveInteger"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Declaring the XSD
Referencing the XSD in the XML declaration
xsi:schemaLocation
list of pairs: (namespace URI + schema URL)
schema validates elements and attributes in namespace
<?xml version="1.0"?> <person xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com person.xsd"><name> Alice </name> </person>
xsi:noNamespaceSchemaLocation
schema only validates elements that are not in any namespace
<?xml version="1.0"?> <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="person.xsd"><name> Alice </name> </person>
Root element
xsd:schema
â
only once in root of every XSD-schema document
other elements are in its namespace
usually used with prefixes
xsd:
or
xs:
Global elements
xsd:element
â
children of root element
can be the root in an âinstanceâ-XSD-document.
Simple Types
Simple elements
Element types
<xsd:element
name="elementName"
type="elementType"
>
possible element types:
xsd:boolean
true, false, 0, 1
xsd:string
can also be empty
xsd:date
YYYY-MM-DD
+(or -)
HH:MM
xsd:integer
arbitrary size
xsd:int
4-byte integer
xsd:positiveInteger
⥠1
xsd:nonNegativeInteger
⥠0
xsd:negativeInteger
< 0
xsd:nonPositiveInteger
†0
xsd:time
xsd:decimal

Fixed values
<xsd:element ....
fixed="value"
/>
no other value can be specified
Default values
<xsd:element ...
default="value"
/>
assigned when no other value is specified
Facets / Restrictions of simple elements
<
xsd:restriction
base="xsd:...">
Extends the simple types by restricting them.
Integers
<xsd:minExclusive value="..."/>
<xsd:maxExclusive value="..."/>
<xsd:minInclusive value="..."/>
<xsd:maxInclusive value="..."/>
Enumerations
<xsd:enumeration value="..."/>
Patterns in strings
<xsd:pattern value="..."/>
use regular expressions as value
[0-9]
[a-zA-Z]
([a-z])*
([a-z])+
([a-z]){3}
day | night
Whitespace restrictions in strings
<xsd:whiteSpace value="..."/>
whitespace characters = line feeds, tabs, spaces, carriage returns
possible values:
preserve
keep characters
replace
replace with space
collapse
only keep a single space from characters
Length
<xsd:length value="..."/>
<xsd:minLength value="..."/>
<xsd:maxLength value="..."/>
Facets / restrictions as anonymous types
<xsd:element name="age">
<xsd:simpleType> <!-- not named -->
<xsd:restriction base="xsd:integer">
<xsd:minExclusive value="0"/>
<xsd:maxInclusive value="110"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element><xsd:element name="duration">
<xsd:simpleType> <!-- not named -->
<xsd:restriction base="xsd:integer">
<xsd:minExclusive value="0"/>
<xsd:maxInclusive value="110"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
Facets / restrictions as named types
<xsd:element name="age" type="intervalType"/>
<xsd:element name="duration" type="intervalType"/><xsd:simpleType name="intervalType"> <!-- named -->
<xsd:restriction base="xsd:integer">
<xsd:minExclusive value="0"/>
<xsd:maxInclusive value="110"/>
</xsd:restriction>
</xsd:simpleType>
Complex Types
can be anonymous or named like simple types (see above)
Complex Elements
can contain other elements and attributes, nothing or just text
Empty
<person id="E832740"/>
<xsd:element name="person" type="personType"/><xsd:complexType name="personType">
<xsd:attribute name="id" type="xsd:ID"/> <!-- attribute, nothing else -->
</xsd:complexType>
Text only
<person id="E832740"> Mantas Å imkus </person>
<xsd:element name="person" type="personType"/><xsd:complexType name="personType">
<xsd:simpleContent> <!-- wrap to extend string and add an attribute-->
<xsd:extension base="xsd:string">
<xsd:attribute name="id" type="xsd:ID"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Element only
<person>
<firstname> Mantas </firstname>
<lastname> Å imkus </lastname>
</person>
<xsd:element name="person" type="personType"/><xsd:complexType name="personType">
<xsd:sequence>
<xsd:element name="firstname" type="xsd:string"/>
<xsd:element name="lastname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
Mix of text and children
<definition>
Woah I can combine text with <term> elements </term> in here!
</definition>
<xsd:element name="definition" type="definitionType"/><xsd:complexType name="definitionType" mixed="true">
<xsd:sequence>
<xsd:element name="term" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
Attributes
Only available for complex types
Attribute types
<xsd:attribute
name="attributeName" type="attributeType"
/>
the attribute-type itself is of a simple type
Default value
<xsd:attribute ...
default="value"
/>
Fixed value
<xsd:attribute ...
fixed="value"
/>
Optional
<xsd:attribute ...
use="optional"
/>
is the default setting
Required
<xsd:attribute ...
use="required"
/>
Order
Order Indicators
Defining the order for children
<xsd:complexType ...>
<xsd:all>
<xsd:element .../>
<xsd:element .../>
</xsd:all>
</xsd:complexType>
all
any order, but children must appear exactly once
minOccurs can be set to
choice
exactly one child from list allowed to appear
sequence
in given order
Occurrence
Occurrence Indicators
<xsd:element name="person">
<xsd:complexType name="personType">
<xsd:sequence>
<xsd:element name="firstname" maxOccurs="2" type="xsd:string"/> <!-- child -->
<xsd:element name="lastname" maxOccurs="2" type="xsd:string"/> <!-- child -->
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="person">
<xsd:complexType name="personType">
<xsd:sequence maxOccurs="2"> <!-- parent -->
<xsd:element name="firstname" type="xsd:string"/>
<xsd:element name="lastname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Warning: the code snippets above have different meaning!
minOccurs
min. number of times the element can be a child
maxOccurs
max. number of times the element can be a child â can be set to
unbounded
Extending other types
Inheritance
of other existing complex types
ie. here any element with the type âpersonTypeâ can only contain a text:
<xsd:element name="person" type="personType"/><xsd:complexType name="personType">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="id" type="xsd:ID"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
and a student inherits the type from person:
<xsd:element name="student" type="studentType"/><xsd:complexType name="studentType">
<xsd:complexContent>
<xsd:extension base="personType">
<xsd:attribute name="subject" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
Keys and References
xsd:key
unique field exists in all selected elements
xsd:unique
if field exists for selected element, then its unique
Example
<company>
<employees>
<employee emp_id="e1"> <!-- key -->
...
</employee>
...
</employees>
<managers>
<manager mgr_id="e1"> <!-- foreign key -->
...
</manager>
...
</managers>
</company>
<xsd:element name="company" type="companyType">
<xsd:key name="empKey">
<xsd:selector xpath="employees/employee"/>
<xsd:field xpath="@emp_id"/>
</xsd:key>
<xsd:keyref name="empRef" refer="empKey">
<xsd:selector xpath="managers/manager"/>
<xsd:field xpath="@mgr_id"/>
</xsd:keyref>
</xsd:element><xsd:complexType name="companyType">
...
</xsd:complexType>
If not all employees had ids, but all managers did -
then we would use
unique
instead of
key
for each employee.
Same example with DTD
DTD solution
<employee emp_id="e1"> E </employee> <project proj_id="p1"> P </project><manager mgr_id="e1"> M </manager> <!-- manages employee with ID 'e1' -->
<!ATTLIST employee emp_id ID #REQUIRED> <!ATTLIST project proj_id ID #REQUIRED> <!ATTLIST manager mgr_id IDREF #REQUIRED><!ELEMENT employee (#PCDATA)> <!ELEMENT project (#PCDATA)> <!ELEMENT manager (#PCDATA)>
XPath is restricted in XSD
-
Only allowes axes
child
,descendant-or-self
at the beginning of a path with special syntax
- Selects Elements (not attributes)