You are on page 1of 17

W3C XML SCHEMA TRAINING

Introduction to XML (1hr 30 Min) Basic XML Rules Databases (Relational and XML) Data Storage and Retrieval Why XML is choice over RDBMS

Why Schema (1 Hr) Grammar for XML. Why Grammar is needed? Well-formed vs Valid XML. Introduction to DTD. Why Schema, when DTD is there? Validation of XML Using DTD and Schema XML Editors (Oxygen, Altova XML Spy, Xmetal, Stylus Studio, XML Editor) XML Parsers (SAX, MSXML, Xalan,LibXML, Xerces)

Formal Introduction Schema is a language recommended by W3C in 2001 Schema is an XML document Schema defines structure of XML instance document Element Name Attribute Name Order of Elements Number of Element Nesting of Element Data type of Element and Attribute Restrictions Default Values Schema Vs DTD Data Types Namespace Inline DTD XML Syntax

XML
<?xml version="1.0"?> <employee> <name>A</name> <dob>7March</dob> <city>Delhi</city> <salary>7000</salary> </employee>

DTD
<!ELEMENT employee (name, dob, city, salary)> <!ELEMENT name (#PCDATA)> <!ELEMENT dob (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT salary(#PCDATA)>

SCHEMA
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.myns.com" xmlns="http://www.myns.com" elementFormDefault="qualified> <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="dob" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="salary" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

Referencing DTD
<?xml version="1.0"?> <!DOCTYPE employee SYSTEM "dtd/emp.dtd"> <employee> <name>A</name> <dob>7March</dob> <city>Delhi</city> <salary>7000</salary> </employee>

Referencing Schema
<?xml version="1.0"?> <employee xmlns="http://www.myns.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.myns.com emp.xsd"> <name>A</name> <dob>7March</dob> <city>Delhi</city> <salary>7000</salary> </employee> This attribute has two values, separated by a space. The first value is the namespace to use. The second value is the location of the XML schema to use .

Starting Writing Schema


<schema> is the root element of every schema. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.myns.com" xmlns="http://www.mynscom" elementFormDefault="qualified> </xs:schema>

- xmlns:xs="http://www.w3.org/2001/XMLSchema"
indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs: - targetNamespace="http://www.myns.com" indicates that the elements defined by this schema (employee, name , city , dob , salary) come from the "http://www.myns.com" namespace. - xmlns="http://www.myns.com" indicates that the default namespace is "http://www.myns.com". If the default and target namespace are same then we can have qualified or unqualified elements belonging to same name space.

-elementFormDefault="qualified
indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

Simple Element
A simple element is an XML element that contains only text. It cannot contain any other elements or attributes.. Syntax <xs:element name="xxx" type="yyy"/> name is any name given to the element, type could be any built-in data type or any custom data-type. <xs:element name=name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name=dob" type="xs:date"/> A simple element can have a default or fixed value. <xs:element name=age" type="xs:integer default=18/> <xs:element name=city" type="xs:string fixed=Delhi/>

Attributes
Attributes are declared and simple types. Syntax <xs:attribute name="xxx" type="yyy"/> name is any name given to the attribute, type could be any built-in data type or any custom data-type. <xs:attribute name=country" type="xs:string"/> <city country=ind>Delhi</city> An attribute can have a default or fixed value. <xs:attribute name=country" type="xs:string default=Delhi/> <xs:attribute name=country" type="xs:stringfixed=Delhi/> An Attribute can be optional or mandatory (attributes are optional by default) <xs:attribute name=country" type="xs:string" use="required"/>

Complex Element
A complex element is an XML element that contains child elements/attributes. An empty element is also a complex element. A complex element can have any of the following structure empty element (with or without attribute) element with child element (with or without attribute) but no content element with child element (with or without attribute) and content as well (mixed content) EXAMPLE <employee id=e1/> and <hr/> <employee id=e1> <firstname>Neil</firstname> <lastname>Scott</lastname> </employee> <about emp-id=e1> this employee joined on <date>7-3-2011</date> as <designation>Analyst</designation> </about>

Complex Element

Empty Element
<xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name=hr"> <xs:complexType/> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> <xs:element name=img"> <xs:complexType> <xs:complexContent> <xs:attribute name=src" type=xs:string"/> </xs:complexContent> </xs:complexType> </xs:element>

Child Elements
<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>

Mixed Content
<xs:element name=about"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name=date" type="xs:date"/> <xs:element name=designation" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>

Element with Text and Attributes


<xs:element name=city"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> This type contains only simple content (text and attributes), therefore we add a simpleContent element around the content. When using simple content, we must define an extension OR a restriction within the simpleContent element,

Complex Element Linear Method


<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>

Modular Method
<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>

Advantage of Modular Method


We can reuse each type which serve as custom data type. Here personinfo act as custom data type and can be reused as data type <xs:element name="employee" type="personinfo"/> <xs:element name="student" type="personinfo"/> <xs:element name="member" 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>

Built-in Types

Extending Complex Custom 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>

Restriction
Also called Facets, are used to define constraint on the values of elements or attributes.

Restriction Type
Values

Example
<xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value=18"/> <xs:maxInclusive value=60"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:restriction base="xs:string"> <xs:enumeration value=Red"/> <xs:enumeration value=Blue"/> <xs:enumeration value=Green"/> </xs:restriction> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z][A-Z]"/> </xs:restriction> <xs:whiteSpace value="preserve"/> or replace *Processor will preserve space or remove spaces. <xs:length value="8"/> eg password <xs:minLength value="5"/> <xs:maxLength value="8"/>

Enumeration

Patterns* *Uses perl type regular expression White Space Length

Restriction
Restriction Type Example

Indicators
Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs

All

specifies that the child elements can appear in any order, and that each child element must occur only once

<xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/> </xs:sequence>

Choice

specifies that either one child element or another can occur

Sequence

specifies that the child elements must appear in a specific orde

maxOccurs minOccurs

Specifies number of time an element can appear. Default value is one, for unlimited use unbounded

Groups Elements Group


<xs:group name="personEgroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> <xs:element name="person> <xs:complexType> <xs:sequence> <xs:group ref="personEgroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>

Attribute Groups
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributeGroup>

<xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> </xs:complexType> </xs:element>

You might also like