XML Schemas are more powerful than DTD.
Uses XML syntax
Unlike DTD, XML schemas supports Namespace
How to include Schema in XML File
To include a schema, you must first define xsi namespace. xsi namespace contain value "http://www.w3.org/2001/XMLSchema-instance". So the definition is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
The XML <Schema> Namespace
There are three choces, the xs namespace is recommended, the first is also very common:
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
or
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Target Namespace
Elements defined by this schema come from this specific namespace
elementFormDefault="qualified"
indicates that any elements used by the XML instance document which were declared in this schema mustbe namespace qualified.
<element> Declarations
name="name of the element"
type="global type"
ref="global element declaration"
form="qualified or unqualified"
minOccurs="non negative number"
maxOccurs="non negative number or 'unbounded'"
default="default value"
fixed="fixed value">
may contain global
Global and Local declarations
Global declarations are declarations that appear as direct children of the <schema> element.
Local declaration are NOT direct children of the <schema> element
Simple Types
Element
A simple element is an XML element that contains only text. It cannot contain any other elements or attributes.
Simple Element Format:
<xs:element name="xxx" type="yyy"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
<xs:attribute name="lang" type="xs:string"/>
The type must be under the namespace "http://www.w3.org/2001/XMLSchema", which is the default namespace
- string
- decimal
- integer
- boolean
- date
- time
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Complex Types
- empty elements
- elements that contain only other elements
- elements that contain only text
- elements that contain both other elements and text
declaration example:
declare inside the element
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<sequence> tag means the child element must be in the exact order as they are declared.
declare outside of the element:
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
declare by including the other complex type:
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<complexContent> means we are going to extend or restrict the content model.
Mixed Type
<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
xsd:
<xs:element name="letter" type="lettertype"/>
<xs:complexType name="lettertype" mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
Indicators
- All
- Choice
- Sequence
- maxOccurs
- minOccurs
- Group name
- attributeGroup name
<any> and <anyAttribute>
The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.
<any>:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<anyAttribute>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
Element Substitution
you can use either of them, <name> or <navn>
<span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:element name="name" type="xs:string"/></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:element name="navn" substitutionGroup="name"/></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:complexType name="custinfo"></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "> <xs:sequence></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "> <xs:element ref="name"/></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "> </xs:sequence></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "></xs:complexType></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:element name="customer" type="custinfo"/></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:element name="kunde" substitutionGroup="customer"/></span>
In this example, you can't use <navn>
block="substitution" prevent you from using substitution
<xs:element name="name" type="xs:string" block="substitution"/>
<xs:element name="navn" substitutionGroup="name"/>
<xs:complexType name="custinfo">
<xs:sequence>
<xs:element ref="name"/>
</xs:sequence>
</xs:complexType>
<xs:element name="customer" type="custinfo" block="substitution"/>
<xs:element name="kunde" substitutionGroup="customer"/>
Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be declared as global elements, otherwise it will not work!