| René Nyffenegger's collection of things on the web | |
|
René Nyffenegger on Oracle - Most wanted - Feedback
|
Schema | ||
|
In the XML context, a Schema is a substitute for a DTD, written in XML. It defines the
elements that can appear in an XML document and their possible attributes.
Here's a little comparison between DTDs and Schemas.
Simple setupThe Schema File
In its simplest form, a schema looks like the following framgent:
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- Schema definitions go here --> </xsd:schema> The XML File<?xml version="1.0" encoding="ISO-8859-1" ?> <rootelem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://c:/path/to/schema-file.xsd"> > <!-- furher XML here --> </rootelem> Simple Type vs Complex type
With Schema, an XML Element is assigned to a type which can either be simple or complex.
Simple Type
A simple type can contain only a basic type (comparable to a variable in a programming language). It cannot contain
other elements, and in cannot have attributes. If a type is not a simple type, it is automatically a complex type.
See here for an example
Complex Type
A complex type is an element that can include an attribute value and or has a content model that contains other elements.
There are four types of complex elements:
Declaration of a root elementThe Schema File<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name='root-elem'> </xsd:element> </xsd:schema> The XML File
<?xml version="1.0" encoding="ISO-8859-1"?>
<root-elem
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file://c:/path/to/schema-file.xsd">
</root-elem>
Nesting elements
As an element that contains another element is a complex type, the nesting element must be declared as such:
Schema
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root-elem" >
<xsd:complexType name="somecomplextype">
<xsd:sequence>
<xsd:element name="first" />
<xsd:element name="second" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<root-elem
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file://c:/path/to/schema-file.xsd">
<first>data of first</first>
<second>data of second</second>
</root-elem>
Attributes<attribute name="some_number" type="integer" default="0" />
Here's a working example:
The Schema file:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="elem_1">
<xsd:complexType>
<xsd:attribute name="elem_1_attr" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="elem_2">
<xsd:complexType>
<xsd:attribute name="elem_2_attr" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The XML file:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="elem_1">
<xsd:complexType>
<xsd:attribute name="elem_1_attr" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="elem_2">
<xsd:complexType>
<xsd:attribute name="elem_2_attr" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Groups of attributes
The Schema file:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- First: define an attribute group, name it attr_group -->
<xsd:attributeGroup name="attr_group">
<xsd:attribute name="attr_one" type="xsd:string" />
<xsd:attribute name="attr_two" type="xsd:string" />
</xsd:attributeGroup>
<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="elem_1">
<xsd:complexType>
<!-- Then: reference the group using ref by the groups name -->
<xsd:attributeGroup ref="attr_group" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The XML file:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<foo xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "file://ag.xsd" >
<elem_1 attr_one="bar" attr_two="baz" />
</foo>
DatatypesBuilt-in datatypes
There are totally 44 built-in datatypes. New datatypes can be built or
derived
Derived datatypes
12 facets can be used to derive new datatypes from built-in datatypes by restricting possible values:
The Schema file:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root-elem">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>
The XML File:
<?xml version="1.0" encoding="ISO-8859-1"?>
<root-elem
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file://restriction.xsd">42</root-elem>
If we had some figure greater than 99 or below zero, it wouldn't work for the restriction based.
Tags (Elements) possible in SchemasallannotationanyanyAttributeappInfoattribute
Declares an attribute.
attributeGroupchoicecomplexContentcomplexType
Defines a new complex type.
Such a definition usually contains
A complex type is defined like so:
<xsd:complexType name='T_person'>
<xsd:all>
<xsd:element name='first_name' type='xsd:string' />
<xsd:element name='last_name' type='xsd:string' />
<xsd:element name='birth_day' type='xsd:date' />
</xsd:all>
<xsd:attribute name='employee_id' type='xsd:positiveInteger' />
</xsd:complexType>
It can then be referenced:
<xsd:element name='person' type='T_person' /> documentationelement
Declares an element.
Attributes:
enumerationextensionfieldgroup
Attributes for particle component:
importincludekeykeyreflengthlistmaxInclusivemaxLengthminInclusiveminLengthpatternredefinerestriction
Restriction allows to create a new type based on another (already existing) type.
<xsd:simpleType name='T_length_between_5_and_9'> <xsd:restriction base='xsd:string'> <xsd:minLength value='5' /> <xsd:maxLength value='9' /> </xsd:restriction> </xsd:simpleType>
Now that the type has been defined, an element can be created from it:
<xsd:element name='something' type='T_length_between_5_and_9' /> schemaselectorsequencesimpleContentsimpleTypeunionuniqueMisc links
See also validating XML against a schema (with MSXML).
maxOccurs can have the value "unbounded"
|