Академический Документы
Профессиональный Документы
Культура Документы
Neeraj Singh
October 2009
XML Validation
Introduction to XML Schema
Examples / Demo
Slide 2
XML Validation
Slide 4
Validation
Slide 5
Schema Languages
There is more than one language in which you can express such
validation conditions. Generically, these are called schema
languages, and the documents that list the constraints are called
schemas.
Different schema languages have different strengths and
weaknesses.
The document type definition (DTD) is the only schema language
built into most XML parsers and endorsed as a standard part of XML.
The W3C XML Schema Language (schemas for short, though it’s
hardly the only schema language) addresses several limitations of
DTDs.
Many other schema languages have been invented that can easily
be integrated with your systems.
Slide 6
XML Schema
Slide 8
Schema definition
Slide 9
Schema Linking when document root element is from null namespace
Let's start with our first document. It must have only "root"
element and this element can contain text only. The element is
from null namespace. Valid document –
<root xmlns="">aaa</root>
If you want to validate this document with XML Schema, you have
to associate some Schema document with it. If the root element is
from null namespace, you will use "noNamespaceSchemaLocation"
attribute.
<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns=""
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > test
</root>
Slide 10
Schema Linking when document root element from some particular
namespace
Now, let's have the same document as in previous example, but the
"root" element must be from some concrete namespace, let's say
"http://foo". Valid document
<root xmlns="http://foo" >aaa</root>
If the root element is from some particular namespace, you
associate the Schema using "schemaLocation" attribute. The first
part of this attribute is the target namespace, the second one the
URL of the Schema file.
<f:root xsi:schemaLocation="http://foo correct_0.xsd"
xmlns:f="http://foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" > test </f:root>
Slide 11
Example
s / Demo
01_FirstXMLSchema.xsd
Writing your first XML Schema and a valid XML file based on this. This
will also demonstrate how to link a XML file with a XML schema.
02_FirstNameSpace.xsd
This example demonstrate the use of namespace. If you have a xml
document that belongs to certain namespace, how to connect to a XML
Schema.
Slide 12
Schema elements
Slide 13
Schema elements: Simple type Contd…
You can also specify default or fixed values for an element. You do
this with either the default or fixed attribute and specify a value
for the attribute. Note: Specifying a fixed or default attribute is
optional.
An example of a simple type element is:
<xs:element name="Author" type="xs:string" default="Whizlabs"/>
All attributes are simple types, so they are defined in the same
way that simple elements are defined. For example:
<xs:attribute name="title" type="xs:string" />
Slide 14
All complex types
Schema data types
Slide 15
Schema elements: Complex types
In this example, the order indicator is xs:sequence, and the occurrence indicator is maxOccurs in the Author element name.
Slide 16
Schema elements: Complex types (Mixed content)
Slide 17
Example
s / Demo
07_ComplexType01.xsd
Your first complex type. Element can contain a mixture of elements.
Now, we want the element "root" to contain elements "aaa", "bbb", and
"ccc" in any order. We will use the "all" element. It also demonstrate the
use of All.
11_EmptyElementUsingAnyType.xsd
Empty element. We want to have the root element to be named "AAA",
from null namespace and empty. The empty element is defined as a
"complexType" with a "complexContent" which is a restriction of
"anyType", but without any elements.
Slide 18
Occurrence indicators
Slide 19
Order indicators
Slide 20
Example: Occurrence and order indicators
Slide 21
Restriction
Slide 22
Example - To restrict the length of the text node
An example that shows how to restrict the length of the text node
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="tokenWithLangAndNote">
<xs:maxLength value="255"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:attribute name="note" type="xs:token"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Slide 23
Example – Remove an attribute from the element
To remove the note attribute from the element title, we declare note to
be prohibited in the list of attributes in the restriction:
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="tokenWithLangAndNote">
<xs:maxLength value="255"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:attribute name="note" use="prohibited"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Slide 24
Facets
Slide 25
Facets Contd…
Slide 27
Multiple Restriction using ‘Union’
The union has been applied on the two embedded simple types to allow values from
both data types, our new data type will now accept the values from an enumeration
with two possible values (TBD and NA).
<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>
Slide 28
Example
s / Demo
03_RestrictSimpleType01.xsd
This example restricts a simple type. Here we will require the value of
the element "root" to be integer and less than 25.
04_RestrictUsingUnion01.xsd
We want the element "root" to be from the range 0-100 or 300-400
(including the border values). We will make a union from two intervals.
06_RestrictUnionEnum02.xsd
Element can contain a string from an enumerated set. Now, we want the
element "root" to have a value "N/A" or "#REF!".
14_RestrictionOfSequence.xsd
The Schema declares type "AAA", which can contain up to two sequences
of "x" and "y" elements. Then we declare the type "BBB", which is a
restriction of the type "AAA" and contain only one x-y sequence.
Slide 29
Extension
The extension element defines complex types that might derive from other
complex or simple types.
If the base type is a simple type, then the complex type can only add attributes.
If the base type is a complex type, then it is possible to add attributes and
elements.
To derive from a complex type, you have to use the complexContent
element in conjunction with the base attribute of the extension element.
Extensions are particularly useful when you need to reuse complex element
definitions in other complex element definitions.
For example, it is possible to define a Name element that contains two child
elements (First and Last) and then reuse it in other complex element definitions.
Slide 30
An example of extensions
<!--Base element definition --> <!-- Student element that reuses it -->
<xs:complexType name="Name"> <xs:complexType name="Student">
<xs:sequence>
<xs:complexContent>
<xs:element name="First"/>
<xs:extension base="Name">
<xs:element name="Last"/>
</xs:sequence> <xs:sequence>
Slide 31
Example
s / Demo
12_ExtensionOfSequence.xsd
Extension of a sequence. When we extend the complexType, which
contains a sequence A with a sequence B, then the sequence B will be
appended to sequence A.
Slide 32
Groups
W3C XML Schema also allows the definition of groups W3C XML Schema also allows the
of elements and attributes. definition of groups of elements
These groups are not datatypes but containers holding a and attributes.
set of elements or attributes that can be used to describe
complex types. <xs:complexType name="bookType">
Slide 33
Example
s / Demo
08_AttributeGroup01.xsd
Defining a group of attributes. Let's say we want to define a group of
common attributes, which will be reused. The root element is named
"root", it must contain the "aaa" and "bbb" elements, and these elements
must have attributes "x" and "y".
12_SequenceChoiceGroup.xsd
Element which contains two "patterns" (sequences), in any order. We
want to have the root element to be named "AAA", from null namespace
and contains two patterns in any order. The first pattern is a sequence of
"BBB" and "CCC" elements, the second one is a sequence of "XXX" and
"YYY" element. The element "choice" allows one of the cases: either the
sequence "myFirstSequence"-"mySecondSequence" or
"mySecondSequence"-"myFirstSequence".
Slide 34
List Datatypes
09_ListDataType01.xsd
Attribute contains a list of values. Now, we want the "root" element to
have attribute "xyz", which contains a list of three integers. We will
define a general list (element "list") of integers and then restrict it
(element "restriction") to have exact length (element "length") of three
items.
10_ListDataType02.xsd
Element contains a list of values. Now, we want the "root" element to
contain a list of three integers. We will define a general list (element
"list") of integers and then restrict it (element "restriction") to have exact
length (element "length") of three items.
Slide 36
Example
s / Demo
More Examples
15_CustomSimpleType.xsd
Definition of a custom simpleType - temperature must be greater than
-273.15. The element "T" must contain number greater than -273.15. We
will define our custom type for temperature named "Temperature" and
will require the element "T" to be of that type.
16_PatternElement.xsd
String must contain e-mail address. The element "A" must contain an
email address. We will define our custom type, which will at least
approximately check the validity of the address. We will use the
"pattern" element, to restrict the string using regular expressions.
Slide 38
Summary
W3C XML Schema has become the de facto standard for defining
the structure of an XML document and for checking the validity of
XML documents. Using schema, it is possible to define:
Elements (simple and complex)
Attributes
Facets for XML elements
The structure of a document (order indicators)
The allowable number of elements (occurrence indicators) in an XML
document
Slide 39
References
ibm.com/developerWorks
IBM XML certification success, Part 1:
W3schools.com
www.Xml.com
XML Schema by OReilly
http://www.zvon.org/xxl/XMLSchemaTutorial
Examples used in the presentation are attached here
XML-Schema-Project.zip
Slide 40
Questions
Slide 41
Thank you