4 xml namespaces and xml schema

Tags:

Post on 11-May-2015

1.023 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

NS, Schema - XML

TRANSCRIPT

XML Namespaces and XML Schema

Atul Kahate

akahate@gmail.com

XML Namespaces

XML Schema | Atul Kahate 3

Ambiguity in Tags One of the major goals of XML is to add

uniformity to tags Potential problem

Several people create their own tags When combined, this may lead to ambiguity

Example Company A and B both use a query tag When we combine their XML documents,

which one should the XML processor process?

XML Schema | Atul Kahate 4

XML Namespaces – 1 Solve the problem of ambiguity Allow the element and attribute names

to be distinguished when merging occurs

Provide a unique prefix to the beginning of every element and attribute name

Usually it is the Web address of the organization

XML Schema | Atul Kahate 5

XML Namespaces – 2 Technically, the prefix can be any URI

URI is more generic than Uniform Resource Locator (URL)

URI can be any unique name

When namespaces are used, the element and attribute names become two-part names

XML namespace and the actual element/attribute name

e.g. a title element may now become http://test.com:title

XML Schema | Atul Kahate 6

Namespace Declarations – 1 Declare a namespace in a top-level element

All the elements and attributes under that element inherit the namespace

Example<book xmlns = “http://www.test.com”>

<isbn>0-596-0058-8</isbn><title>My first book</title><author>Mr XYZ</author>

</book> All elements under the book element are a

part of the namespace

XML Schema | Atul Kahate 7

Namespace Declarations – 2 If a namespace is declared at an

element that is not the root element It applies only till the boundaries of that

element Allows different namespaces for

different elements in an XML document One per element or sub-element, etc

XML Schema | Atul Kahate 8

Namespace Declarations – 3 Example

<supercatalog xmlns = “http://www.main.com”><book xmlns = “http://www.first.com”>

<isbn>0-596-0058-8</isbn><title>My first book</title><author>Mr XYZ</author>

</book><book xmlns = “http://www.second.com”>

<isbn>0-719-8990-2</isbn><title>My second book</title><author>Mr PQR</author>

</book></supercatalog>

XML Schema | Atul Kahate 9

Namespace Shortcuts Makes namespace declarations and usage

more readable Example

<zbooks:book xmlns:zbooks = “http://www.test.com”><zbooks:isbn>0-596-0058-8</zbooks:isbn><zbooks:title>My first book</ zbooks: title><zbooks:author>Mr XYZ</ zbooks: author>

</zbooks:book> All elements starting with zbooks: belong to

the http://www.test.com namespace

XML Schema | Atul Kahate 10

Quick Recap on Namespaces - 1 Consider an XML file<employees>

<employee><id>9662</id><name>Ram</name><hireDate>2001-08-13</hireDate>

</employee><employee>

<id>10000</id><name>Parag</name><hireDate>2004-01-12</hireDate>

</employee></employees>

XML Schema | Atul Kahate 11

Quick Recap on Namespaces - 2 Now imagine that the payroll department wants to share this XML file and wants

to add payroll data Modified XML file is shown below<employees>

<employee><id>9662</id><name>Ram</name><hireDate>2001-08-13</hireDate><salary> 10000 </salary><taxes> 2000 </taxes>

</employee><employee>

<id>10000</id><name>Parag</name><hireDate>2004-01-12</hireDate><salary> 10000 </salary><taxes> 2000 </taxes>

</employee></employees>

XML Schema | Atul Kahate 12

Quick Recap on Namespaces - 3 How to handle this change?

Update the schema owned by the HR department? OR

Separate the data items owned by HR and Payroll and make them responsible for their data items?

Use namespaces Different “buckets” for data items owned

by HR and Payroll

XML Schema | Atul Kahate 13

Quick Recap on Namespaces - 4<HRData:employees>

<HRData:employee><HRData:id>9662</HRData:id><HRData:name>Ram</HRData:name><HRData:hireDate>2001-08-13</HRData:hireDate><payrollData:salary> 10000 </payrollData:salary><payrollData:taxes> 2000 </payrollData:taxes>

</HRData:employee><HRData:employee>

<HRData:id>10000</HRData:id><HRData:name>Parag</HRData:name><HRData:hireDate>2004-01-12</HRData:hireDate><payrollData:salary> 10000 </payrollData:salary><payrollData:taxes> 2000 </payrollData:taxes>

</HRData:employee></HRData:employees>

XML Schema | Atul Kahate 14

Quick Recap on Namespaces - 5 Using a short-hand notation<hr:employees xmlns:hr=“HRData” xmlns:py=“payrollData”>

<hr:employee><hr:id>9662</hr:id><hr:name>Ram</hr:name><hr:hireDate>2001-08-13</hr:hireDate><py:salary> 10000 </py:salary><py:taxes> 2000 </py:taxes>

</hr:employee><hr:employee>

<hr:id>10000</hr:id><hr:name>Parag</hr:name><hr:hireDate>2004-01-12</hr:hireDate><py:salary> 10000 </py:salary><py:taxes> 2000 </py:taxes>

</hr:employee></hr:employees> Syntax: xmlns:prefix=“Actual namespace” (where prefix is the

shorthand prefix)

XML Schema | Atul Kahate 15

Quick Recap on Namespaces - 6 Effectively, we will create two schemas,

one for HR and one for payroll Suppose now you want to send the

payroll data to the income tax department in XML format

What if another company also uses namespace shorthands such as hr and py? Need to make namespaces globally unique

XML Schema | Atul Kahate 16

Quick Recap on Namespaces - 7<hr:employees xmlns:hr="http://www.test.com/hr/"

xmlns:py="http://www.test.com/payroll/"><hr:employee>

<hr:id>9662</hr:id><hr:name>Ram</hr:name><hr:hireDate>2001-08-13</hr:hireDate><py:salary> 10000 </py:salary><py:taxes> 2000 </py:taxes>

</hr:employee><hr:employee>

<hr:id>10000</hr:id><hr:name>Parag</hr:name><hr:hireDate>2004-01-12</hr:hireDate><py:salary> 10000 </py:salary><py:taxes> 2000 </py:taxes>

</hr:employee></hr:employees>

XML Schema | Atul Kahate 17

Quick Recap on Namespaces - 8

If most elements in an XML file belong to one namespace, we can make it the default namespace No need to prefix elements belonging

to that namespace Defining default namespace

xmlns=“namespace”

XML Schema | Atul Kahate 18

Quick Recap on Namespaces - 9<hr:employees xmlns:hr="http://www.test.com/hr/"

xmlns:py="http://www.test.com/payroll/"><employee>

<id>9662</id><name>Ram</name><hireDate>2001-08-13</hireDate><py:salary> 10000 </py:salary><py:taxes> 2000 </py:taxes>

</employee><employee>

<id>10000</id><name>Parag</name><hireDate>2004-01-12</hireDate><py:salary> 10000 </py:salary><py:taxes> 2000 </py:taxes>

</employee></employees>

Well-formed and Valid XML

XML Schema | Atul Kahate 20

Well-formed and Valid Documents Well-formed documents

Adhere to all the basic rules of XML document design

Valid documents A well-formed document that also adheres

to its DTD rules

All well-formed documents need not be valid documents

XML Schema | Atul Kahate 21

Well-formed Documents – 1 Include a required XML declaration

Every XML document must begin with an XML declaration such as <?XML VERSION=“1.0”?>

Have a single root element

XML Schema | Atul Kahate 22

Well-formed Documents – 2 All elements, attributes, and entities

should use the correct syntax All non-empty elements must have start

and end tags Empty elements must have a slash at

the end before the greater than tag Every attribute value must be inside

quotation marks Nesting of elements must happen

correctly

XML Schema | Atul Kahate 23

Valid Documents Exhibits all characteristics of well-

formed documents Additionally

Adheres to the rules of its DTD Contain only elements defined by its

DTD Includes all the mandatory elements

and attributes

XML Schemas

XML Schema | Atul Kahate 25

XML Schemas Similar in concept to a DTD

Describes the data elements, attributes and their inter-relationships in an XML document

Schema syntax is much closer to the XML tag syntax

XML Schemas: Case Study

XML Schema | Atul Kahate 27

Case Study on Schema – Sample XML Document<?xml version=“1.0”><company ccode=“XYZ Corp Ltd”>

<chairman>Rajesh</chairman><department>

<manager>Hari</manager><assistant>Madhuri</assistant><role-description>Administration</role-description>

</department><department>

<manager>Vivek</manager><assistant>Bhavna</assistant><role-description>Training</role-description>

</department></company>

XML Schema | Atul Kahate 28

Case Study on Schema – Designing Schema

We need the following declaration in our schema file, after the <?xml …?> declaration:

<xsd:schema xmlns:xsd=“http://www.w3.org/2000/10/XMLSchema”>

XML Schema | Atul Kahate 29

Case Study –Schema Write out the schema in a manner similar

to the way the XML document is organized So, start with a <company> root tag

This tag has attributes and sub-elements Hence, it should be of type <complexType> The other type (<simpleType>) is reserved for

data types holding only values, but no attributes or sub-elements

To define the sub-elements, use the <sequence> tag

XML Schema | Atul Kahate 30

Case Study - Schema -> The structure so far

<xsd:element name=“company”><xsd:complexType>

<xsd:sequence> Notes:

The <sequence> element defines the order of the child elements

Other options are <choice> and <all>, discussed later

Let us now define the sub-elements of the company root element

XML Schema | Atul Kahate 31

Case Study - Schema -> Adding Sub-Elements

<xsd:element name=“chairman” type=“xsd:string”/>

Defines the first sub-element Now, define the department sub-element

and its sub-elements<xsd:element name=“department”

minOccurs=“0” maxOccurs=“unbounded”>

<xsd:sequence><xsd:complexType>

XML Schema | Atul Kahate 32

Case Study - Schema -> Adding Sub-Sub-Elements Now, define the sub-elements of the department tag

<xsd:complexType> <xsd:sequence>

<xsd:element name=“manager” type=“xsd:string”/> <xsd:element name=“assistant” type=“xsd:string” minOccurs=“0” maxOccurs=“unbounded”/> <xsd:element name=“role-description”

type=“xsd:string”/> </xsd:sequence> </xsd:complexType>

XML Schema | Atul Kahate 33

Case Study - Schema -> Closing the Root Tag Why have we not closed the root tag

<company>? Because, we want to define an attribute for

it<xsd:attribute name=“ccode”

type=“xsd:string”/></xsd:complexType></xsd:element></xsd:schema> Note: Attribute definitions must always

follow the element definitions.

XML Schema | Atul Kahate 34

Case Study - Schema -> The Complete Schema Code<?xml version="1.0"?><xsd:schema xmlns="http://www.w3.org/2000/10/XMLSchema"><xsd:element name="company">

<xsd:complexType><xsd:sequence>

<xsd:element name=“chairman” type=“xsd:string”/><xsd:element name=“department” minOccurs=“0”

maxOccurs=“unbounded”> <xsd:complexType> <xsd:sequence>

<xsd:element name=“manager” type=“xsd:string”/><xsd:element name=“assistant” type=“xsd:string”

minOccurs=“0” maxOccurs=“unbounded”/>

<xsd:element name=“role-description” type=“xsd:string”/>

</xsd:sequence> </xsd:complexType></xsd:element>

</xsd:sequence><xsd:attribute name=“ccode” type=“xsd:string”/>

</xsd:complexType></xsd:element></xsd:schema>

DTD Versus Schema: An Example

XML Schema | Atul Kahate 36

XML Example – Using DTD <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE BOOK SYSTEM "book_first.dtd"> <BOOK> <BOOK_NAME>Computer

Networks</BOOK_NAME> <AUTHOR>Andrew Tanenbaum</AUTHOR> <PUBLISHER>Pearson

Education</PUBLISHER> <CATEGORY>Internet

Technologies</CATEGORY> </BOOK>

XML Schema | Atul Kahate 37

DTD Example <!ELEMENT BOOK (BOOK_NAME,

AUTHOR, PUBLISHER, CATEGORY)> <!ELEMENT BOOK_NAME (#PCDATA)> <!ELEMENT AUTHOR (#PCDATA)> <!ELEMENT PUBLISHER (#PCDATA)> <!ELEMENT CATEGORY (#PCDATA)>

XML Schema | Atul Kahate 38

XML Document – Using Schema <?xml version="1.0" encoding="UTF-8"?> <BOOK

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="book_first.xsd"> <BOOK_NAME>Computer Networks</BOOK_NAME> <AUTHOR>Andrew Tanenbaum</AUTHOR> <PUBLISHER>Pearson Education</PUBLISHER> <CATEGORY>Internet Technologies</CATEGORY> </BOOK>

XML Schema | Atul Kahate 39

Corresponding Schema Let us write this as a schema now …

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="BOOK"><xsd:complexType>

<xsd:sequence><xsd:element name="BOOK_NAME"

type="xsd:string"/><xsd:element name="AUTHOR"

type="xsd:string"/><xsd:element name="PUBLISHER"

type="xsd:string"/><xsd:element name="CATEGORY"

type="xsd:string"/></xsd:sequence>

</xsd:complexType></xsd:element>

</xsd:schema>

XML Schema | Atul Kahate 40

Exercise – 1 We want to keep information about

an employee in the form of employee ID, name, department, salary, and date of joining. Create an XML document and write corresponding DTD and schema.

XML Schema | Atul Kahate 41

Exercise – 2 Modify the above example to split

the name into first and last name

Schema Basics and Syntaxes

XML Schema | Atul Kahate 43

The <schema> Element Root element of every XML schema

<?xml version="1.0"?><xs:schema>......</xs:schema>

Usually contains attributes, as shown next

XML Schema | Atul Kahate 44

Attributes in the <schema> Element<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.test.com"xmlns="http://www.xyz.com"elementFormDefault="qualified">......</xs:schema>

xmlns:xs="http://www.w3.org/2001/XMLSchema" indicates that the data types and elements used inside this schema come from this namespace and they should be prefixed with xs:

targetNamespace="http://www.test.com" indicates that the user-defined XML elements, attributes etc come from this schema

xmlns="http://www.xyz.com“ is the default namespace elementFormDefault="qualified" mandates usage of namespaces

for all elements in the current XML document

XML Schema | Atul Kahate 45

Referencing a Schema from an XML File<?xml version="1.0"?>

<note xmlns="http://www.xyz.com"xmlns:xsi="http://www.w3.org/2001/XMLSchema-

instance"xsi:schemaLocation="http://www.xyz.com note.xsd">

<to>Ram</to><from>Krishna</from><heading>Reminder</heading><body>Please send me your XML notes!</body></note>

Default namespace

Namespace and schema file name

XML Schema | Atul Kahate 46

Defining Simple Elements A simple element is an XML element that can contain

only text (strings, dates, Boolean values, etc). It cannot contain any other elements or attributes.

Syntax <xs:element name="xxx" type="yyy"/>

Examples<lastname>Tanenbaum</lastname> <age>60</age> <dateborn>1945-10-08</dateborn>

Corresponding schema declarations<xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/>

XML Schema | Atul Kahate 47

Schema Data Types and Default or Constant Values Basic data types

xs:string xs:decimal xs:integer xs:boolean xs:date xs:time

Declaring default values <xs:element name="color" type="xs:string"

default="red"/> Declaring constant values

<xs:element name="color" type="xs:string" fixed="red"/>

XML Schema | Atul Kahate 48

Declaring Attributes in Schemas Simple elements cannot have attributes. If an element has attributes, it is considered to be of

complex type. But the attribute itself is always declared as a simple

type. This means that an element with attributes always has a

complex type definition. Syntax

<xs:attribute name="xxx" type="yyy"/> Example

<lastname lang="EN">Joshi</lastname> Corresponding schema declaration

<xs:attribute name="lang" type="xs:string"/>

XML Schema | Atul Kahate 49

More on Attributes Default values

<xs:attribute name="lang" type="xs:string" default="EN"/>

Fixed values <xs:attribute name="lang" type="xs:string"

fixed="EN"/> Mandatory attributes

<xs:attribute name="lang" type="xs:string" use="required"/>

Optional attributes <xs:attribute name="lang" type="xs:string"

use="optional"/>

XML Schema | Atul Kahate 50

Exercise Maintain employee information in

the form of employee ID, name, department, salary, and date of joining. Keep maximum information in the form of attributes, rather than as elements. Create an XML document and write and schema.

Schema Example

XML Schema | Atul Kahate 52

Employee XML<?xml version="1.0" encoding="utf-8" ?><Employee lastUpdated="2007-04-21"> <Name>Manish Potdar</Name> <Contact> <Address> <Street>12 East Street</Street> <City>Pune</City> <Pin>411001</Pin> <State>Maharashtra</State> </Address> <HomePhone>02025530834</HomePhone> <Email>manish@yahoo.com</Email> </Contact></Employee>

XML Schema | Atul Kahate 53

Employee Schema <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Employee"> <xs:complexType> <xs:sequence> <xs:element name="Name"/> <xs:element name="Contact"> <xs:complexType> <xs:sequence> <xs:element ref="Address"/> <xs:element name="HomePhone"/> <xs:element name="Email"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="lastUpdated"/> </xs:complexType> </xs:element> <xs:element name="Address"> <xs:complexType> <xs:sequence> <xs:element name="Street"/> <xs:element name="City"/> <xs:element name="Pin"/> <xs:element name="State"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

XML Schema | Atul Kahate 54

Schema and Data Types Schemas provide a much wider range of data

types than DTDs Boolean, Date-related, String, Numeric

Add flexibility to element declarations Examples

<simpleType name=“humanAges”><minInclusive base=“double” value=“0.0” /><maxInclusive base=“double” value=“150.0” />

</simpleType>

<simpleType name=“comment”><maxLength base=“string” value=“1024” />

</simpleType>

XML Schema | Atul Kahate 55

Schema and DTD DTD can be converted into schema Equivalences need to be found Example, consider an element test

DTD Syntax Meaning minOccurs in schema

maxOccurs in schema

test? 0 or 1 occurrences

0 1

test Exactly 1 occurrence

1 1

test* 0 or more occurrences

0 Unbounded

test+ 1 or more occurrences

1 Unbounded

Schema Components

XML Schema | Atul Kahate 57

Schema Components Generic term for the blocks that make up

the abstract data model of the schema syntax

There are 12 components, split into three groups Primary components – Simple and complex

type definitions, Element and attribute declarations

Secondary components – Attribute groups, Identity constraints, Model group definitions

Helper components – Not named or accessed independently

Primary Components

XML Schema | Atul Kahate 59

Primary Components Element declarations Simple type definitions – Can hold

only values, not child elements, or attributes

Complex type definitions – Can have sub-elements and attributes

Attribute declarations Note: We declare attributes and

elements, but define types

XML Schema | Atul Kahate 60

Declaring Elements Example<xsd:element name =

“dateReceived”/>

Or, with data type<xsd:element name =

“dateReceived” type = “xsd:date”/>

XML Schema | Atul Kahate 61

Specifying Element Occurrences minOccurs and maxOccurs can be used Examples

<xsd:element name = “dateReceived” type = “xsd:date” minOccurs = “0” maxOccurs = “1”/>

<xsd:element name = “student” type = “xsd:string” minOccurs = “1” maxOccurs = “unbounded”/>

XML Schema | Atul Kahate 62

Simple and Complex Types Simple types

Allow atoms of data (element or attribute values) that cannot be divided further

Hence, only elements that do not have child elements or attributes are of simple type

Complex types Can contain other elements and

attributes

XML Schema | Atul Kahate 63

Simple Types Contain information that cannot be split

further Example XML

<myElement>I am simple</myElement> Schema

<xs:element name="myElement" type="xs:string"/>

XML Schema | Atul Kahate 64

Simple Data Types – Classification Two types

Built-in types Primitive types – Examples are string,

boolean, number, float, double, decimal, dateTime, time, date, etc

Derived types – Examples are integer, nonPositiveInteger, negativeInteger, long, int, short, byte, nonNegativeInteger, unsignedInt, positiveInteger, etc

User-derived types

XML Schema | Atul Kahate 65

Named Complex Types Allow elements to contain sub-elements and

attributes; thus, allow creation of content models

Schema<xsd:complexType name= "Address">

<xsd:sequence><xsd:element name = "Street1" /><xsd:element name = "Street2" /><xsd:element name = "City" /><xsd:element name = "Pin" /><xsd:element name = “State" />

</xsd:sequence></xsd:sequence>

XML<xsd:element name = “billingAddress” type = “Address” />

XML Schema | Atul Kahate 66

Anonymous Complex Types<xsd:element name = "Name">

<xsd:complexType><xsd:sequence>

<xsd:element name = "FirstName" /><xsd:element name = "MiddleInitial"

minOccurs = "0" maxOccurs = "unbounded" /><xsd:element name = "LastName" />

</xsd:sequence></xsd:complexType>

</xsd:name>

XML Schema | Atul Kahate 67

Combining Named and Unnamed Declarations <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="ContactDetails"> <xs:complexType> <xs:sequence> <xs:element ref="Contact" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Contact"> <xs:complexType> <xs:sequence> <xs:element name="Name"> <xs:complexType> <xs:sequence> <xs:element name="FirstName"/> <xs:element name="MiddleInitial" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="LastName"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="billingAddress" type="Address"/> <xs:element name="mailingAddress" type="Address"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="Address"> <xs:sequence> <xs:element name="Street1"/> <xs:element name="Street1"/> <xs:element name="City"/> <xs:element name="Pin"/> <xs:element name="State"/> </xs:sequence> </xs:complexType> </xs:schema>

XML Schema | Atul Kahate 68

Extending Complex Types Suppose we want to add two more properties to the Address complex type in a given situation <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="ContactDetails"> <xs:complexType> <xs:sequence> <xs:element ref="Contact" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Contact"> <xs:complexType> <xs:sequence> <xs:element name="Name"> <xs:complexType> <xs:sequence> <xs:element name="FirstName"/> <xs:element name="MiddleInitial" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="LastName"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="billingAddress" type="Address"/> <xs:element name="mailingAddress" type="Address"/> <xs:element name="CreditAddressReference"> <xs:complexType> <xs:complexContent> <xs:extension base="Address"> <xs:attribute name="residentFrom" type="xs:date"/> <xs:attribute name="residentTo" type="xs:date"/> </xs:extension> </xs:complexContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="Address"> <xs:sequence> <xs:element name="Street1"/> <xs:element name="Street1"/> <xs:element name="City"/> <xs:element name="Pin"/> <xs:element name="State"/> </xs:sequence> </xs:complexType> </xs:schema>

XML Schema | Atul Kahate 69

Declaring Attributes

<xsd:element name = “invoice”><xsd:complexType>

<xsd:attribute name = “paid” use = “optional” default = “true”></xsd:complexType>

</xsd:element>

Secondary Components

XML Schema | Atul Kahate 71

Secondary Components Can be classified into

Model group definitions Attribute groups Notation declarations Identity constraints

XML Schema | Atul Kahate 72

Model group definitions Allows creation of group of element definitions

and naming them using a name attribute, so that we can use these groups to build content models

We need to use the group element, which must be a top-level schema element, i.e. child of the schema element

Once a group is specified, we can also indicate whether only one of them should appear (choice) or all of them are needed (all)

XML Schema | Atul Kahate 73

Choice Example – Using group Syntax <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="FlightDetails"> <xs:complexType> <xs:sequence> <xs:element name="Name"/> <xs:group ref="MealOptions"/> </xs:sequence> </xs:complexType> </xs:element> <xs:group name="MealOptions"> <xs:choice> <xs:element name="Vegetarian"/> <xs:element name="Non-Vegetarian"/> <xs:element name="Salad"/> </xs:choice> </xs:group> </xs:schema>

XML Schema | Atul Kahate 74

Choice Example – Using Unnamed Syntax

<xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee"

type="employee"/> <xs:element name="member"

type="member"/> </xs:choice> </xs:complexType></xs:element>

XML Schema | Atul Kahate 75

All Example – Using group Syntax <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2

(http://www.altova.com)--> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:group name="person"> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:group> <xs:element name="test"> <xs:complexType> <xs:group ref ="person" /> </xs:complexType> </xs:element> </xs:schema>

XML Schema | Atul Kahate 76

All Example – Using Unnamed Syntax

<xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType></xs:element>

firstname and lastname are mandatory sub-elements – must occur exactly once

XML Schema | Atul Kahate 77

All – Another Example<xs:group name = “CreditCardDetails”>

<xs:all><xs:element name = “CardType” /><xs:element name = “CardHolder” /><xs:element name =

“CardNumber” /><xs:element name = “CardExpiry” />

</xs:all></xs:group>

XML Schema | Atul Kahate 78

Sequence Example using group Syntax <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2

(http://www.altova.com)--> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:group name="person"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:group> <xs:element name="test"> <xs:complexType> <xs:group ref ="person" /> </xs:complexType> </xs:element> </xs:schema>

XML Schema | Atul Kahate 79

Sequence Example Using Unnamed Syntax Another element that we can use inside

a group is sequence Example

<xs:group name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType></xs:element>

XML Schema | Atul Kahate 80

Other Details We can use minOccurs, maxOccurs

attributes on group, choice, sequence, and all elements.

XML Schema | Atul Kahate 81

Attribute Groups Just as we can group elements

together, we can create a group of attributes

Attribute groups allow us to define a set of attributes that would be used on a set of elements

Attribute groups can nest other attribute groups inside them

XML Schema | Atul Kahate 82

Attribute Groups - Example <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="product"> <xs:complexType> <xs:attributeGroup ref="productDetails"/> </xs:complexType> </xs:element> <xs:attributeGroup name="productDetails"> <xs:attribute name="productID" use="required" type="xs:string"/> <xs:attribute name="productName" use="required" type="xs:string"/> <xs:attribute name="productDescription" use="required"

type="xs:string"/> <xs:attribute name="unit" use="required" type="xs:positiveInteger"/> <xs:attribute name="price" use="required" type="xs:decimal"/> <xs:attribute name="stock" use="required" type="xs:integer"/> </xs:attributeGroup> </xs:schema>

XML Schema | Atul Kahate 83

Default or Fixed Element Content – 1 Schema<?xml version="1.0" encoding="UTF-8"?><!--W3C Schema generated by XMLSpy v2007 sp2

(http://www.altova.com)--><xs:schema

xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="product" default="book" />

</xs:schema>

XML Schema | Atul Kahate 84

Default or Fixed Element Content – 2 XML<?xml version="1.0" encoding="utf-8"?><product lastUpdated="2007-04-21"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Atul\Desktop\test.xsd">

pencil</product>

Or this<?xml version="1.0" encoding="utf-8"?><product lastUpdated="2007-04-21"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Atul\Desktop\test.xsd">

</product>

XML Schema | Atul Kahate 85

Default or Fixed Element Content – 2 Usage of Fixed in the Schema<?xml version="1.0" encoding="UTF-8"?><!--W3C Schema generated by XMLSpy v2007 sp2

(http://www.altova.com)--><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="product" fixed="book" /></xs:schema>

Now, the value of the element in the XML document must either be book, or it should be empty

XML Schema | Atul Kahate 86

Nillable Elements We can specify that an element

must occur or is optional by using the nillable attribute

However, we still need to include the nillable element in the XML document, but with a different syntax, as shown in the example

XML Schema | Atul Kahate 87

Nillable Element Example – 1 Schema<?xml version="1.0" encoding="UTF-8"?><!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="product"><xs:complexType>

<xs:sequence><xs:element name="productID"/><xs:element name="productName"

nillable="true"/></xs:sequence>

</xs:complexType></xs:element>

</xs:schema>

XML Schema | Atul Kahate 88

Nillable Element Example – 2 XML<?xml version="1.0" encoding="utf-8"?><product

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Atul\Desktop\test.xsd"><productID>test</productID><productName xsi:nil="true"/>

</product>

XML Schema | Atul Kahate 89

Exercise – 1 Create an XML document and write the

corresponding schema to represent the following information: Trimester number Student’s details

Roll number Name Total maximum marks across all subjects Total actual marks across all subjects Percentage of marks

XML Schema | Atul Kahate 90

Exercise – 2 Modify the above example to take

care of the following: For every student, maintain a list of

any 5 subjects, each with a maximum of 100 marks and actual marks obtained, along with the earlier information

Schema Facets

XML Schema | Atul Kahate 92

Schema Facets (Restrictions) - 1 Restriction on values

<xs:element name="age"><xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="100"/> </xs:restriction></xs:simpleType></xs:element>

XML Schema | Atul Kahate 93

Schema Facets (Restrictions) - 2 Restriction on a set of values<xs:element name="car“ type=“carType”/>

<xs:simpleType name=“carType”> <xs:restriction base="xs:string"> <xs:enumeration value=“Santro/> <xs:enumeration value=“Maruti"/> <xs:enumeration value=“Indica/> </xs:restriction></xs:simpleType>

</xs:element> Note: Text shown in blue is optional

XML Schema | Atul Kahate 94

Schema Facets (Restrictions) - 3 Restrictions on a series of values

<xs:element name="letter"><xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction></xs:simpleType></xs:element> Allows one lowercase alphabet

<xs:pattern value="[A-Z][A-Z][A-Z]"/> Allows three uppercase alphabets

<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> Allows three uppercase or lowercase alphabets

<xs:pattern value="[xyz]"/> Allows one of the lowercase alphabets x, y, or z

<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> Allows five digits in sequence

XML Schema | Atul Kahate 95

Schema Facets (Restrictions) - 4 Other restrictions on a series of values

<xs:element name="letter"><xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction></xs:simpleType></xs:element> Allows 0 or more lowercase alphabets <xs:pattern value="([a-z][A-Z])+"/>

One or more lowercase alphabets followed by an uppercase alphabet <xs:pattern value=“worker|manager"/>

Allow either worker or manager <xs:pattern value="[a-zA-Z0-9]{8}"/>

Allow exactly 8 characters, either alphabets or numbers <xs:length value="8"/>

Allow exactly 8 characters <xs:minLength value="5"/> <xs:maxLength value="8"/>

Allow a minimum of 5 and maximum of 8 characters

Schema: Case Study

XML Schema | Atul Kahate 97

XML Document<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>John Smith</orderperson> <shipto> <name>Ola Nordmann</name> <address>Langgt 23</address> <city>4000 Stavanger</city> <country>Norway</country> </shipto> <item> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item></shiporder>

Specifies that this document should be validated against a schema

Specifies the location of the schema (in this case, same as that of the XML file)

XML Schema | Atul Kahate 98

Start with the Schema Template<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

...

...

</xs:schema>

XML Schema | Atul Kahate 99

Create the shiporder Element Outline<xs:element name="shiporder"> <xs:complexType> <xs:sequence> ... ... </xs:sequence> ... </xs:complexType></xs:element>

XML Schema | Atul Kahate 100

Define the orderperson Element

<xs:element name="orderperson" type="xs:string"/>

XML Schema | Atul Kahate 101

Define the shipto and item Elements<xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType></xs:element>

<xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType></xs:element>

XML Schema | Atul Kahate 102

Declare the attribute for the shiporder Element

<xs:attribute name="orderid" type="xs:string" use="required"/>

XML Schema | Atul Kahate 103

The Complete Schema<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element name="orderperson" type="xs:string"/> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType></xs:element>

</xs:schema>

XML Schema | Atul Kahate 104

Making it more readable<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- definition of simple elements --><xs:element name="orderperson" type="xs:string"/><xs:element name="name" type="xs:string"/><xs:element name="address" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="country" type="xs:string"/><xs:element name="title" type="xs:string"/><xs:element name="note" type="xs:string"/><xs:element name="quantity" type="xs:positiveInteger"/><xs:element name="price" type="xs:decimal"/>

<!-- definition of attributes --><xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements --><xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="address"/> <xs:element ref="city"/> <xs:element ref="country"/> </xs:sequence> </xs:complexType></xs:element><xs:element name="item"> <xs:complexType> <xs:sequence> <xs:element ref="title"/> <xs:element ref="note" minOccurs="0"/> <xs:element ref="quantity"/> <xs:element ref="price"/> </xs:sequence> </xs:complexType></xs:element>

<xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element ref="orderperson"/> <xs:element ref="shipto"/> <xs:element ref="item" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="orderid" use="required"/> </xs:complexType></xs:element>

</xs:schema>

XML Schema | Atul Kahate 105

Adding more features<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/></xs:simpleType>

<xs:simpleType name="inttype"> <xs:restriction base="xs:positiveInteger"/></xs:simpleType>

<xs:simpleType name="dectype"> <xs:restriction base="xs:decimal"/></xs:simpleType>

<xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction></xs:simpleType>

<xs:complexType name="shiptotype"> <xs:sequence> <xs:element name="name" type="stringtype"/> <xs:element name="address" type="stringtype"/> <xs:element name="city" type="stringtype"/> <xs:element name="country" type="stringtype"/> </xs:sequence></xs:complexType>

<xs:complexType name="itemtype"> <xs:sequence> <xs:element name="title" type="stringtype"/> <xs:element name="note" type="stringtype" minOccurs="0"/> <xs:element name="quantity" type="inttype"/> <xs:element name="price" type="dectype"/> </xs:sequence></xs:complexType>

<xs:complexType name="shipordertype"> <xs:sequence> <xs:element name="orderperson" type="stringtype"/> <xs:element name="shipto" type="shiptotype"/> <xs:element name="item" maxOccurs="unbounded" type="itemtype"/> </xs:sequence> <xs:attribute name="orderid" type="orderidtype" use="required"/></xs:complexType>

<xs:element name="shiporder" type="shipordertype"/>

</xs:schema>

XML Schema | Atul Kahate 106

Exercise – 1 Create schema for the following XML<?xml version="1.0" encoding="utf-8"?><Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Atul\Desktop\test.xsd">

<Products><Product>

<Product_ID>100</Product_ID><Product_Name>Book</Product_Name><Product_Units>2</Product_Units><Product_Price>400</Product_Price>

</Product><Product>

<Product_ID>200</Product_ID><Product_Name>Pen</Product_Name><Product_Units>5</Product_Units><Product_Price>10</Product_Price>

</Product></Products><Contact>

<Name>Reshama Joshi</Name><Address>43 Tilak Road</Address><City>Pune</City><Pin>411001</Pin>

</Contact><Payment>

<CreditCard><CardType>VISA</CardType><CardNumber>901010111</CardNumber><ExpiryDate>2012-10-01</ExpiryDate>

</CreditCard><Amount>1000</Amount>

</Payment></Order>

XML Schema | Atul Kahate 107

Solution – 1<?xml version="1.0" encoding="UTF-8"?><!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Order"><xs:complexType>

<xs:sequence><xs:element name="Products" type="productsType"/><xs:element name="Contact" type="contactType"/><xs:element name="Payment" type="paymentType"/></xs:sequence>

</xs:complexType></xs:element><xs:complexType name="productsType">

<xs:sequence><xs:element name="Product" minOccurs="1" maxOccurs="unbounded" type="productType"/>

</xs:sequence></xs:complexType><xs:complexType name="productType">

<xs:sequence><xs:element name="Product_ID" type="xs:string"/><xs:element name="Product_Name" type="xs:string"/><xs:element name="Product_Units" type="xs:positiveInteger"/><xs:element name="Product_Price" type="xs:int"/>

</xs:sequence></xs:complexType><xs:complexType name="contactType">

<xs:sequence><xs:element name="Name" type="xs:string"/><xs:element name="Address" type="xs:string"/><xs:element name="City" type="xs:string"/><xs:element name="Pin" type="xs:int"/>

</xs:sequence></xs:complexType><xs:complexType name="paymentType">

<xs:sequence><xs:choice><xs:element name="CreditCard" type="creditCardType"/><xs:element name="Cheque" type="chequeType"/></xs:choice><xs:element name="Amount" type="xs:positiveInteger"/>

</xs:sequence></xs:complexType><xs:complexType name="creditCardType">

<xs:sequence><xs:element name="CardType" type="cardTypeRestriction"/><xs:element name="CardNumber" type="xs:int"/><xs:element name="ExpiryDate" type="xs:date"/>

</xs:sequence></xs:complexType><xs:simpleType name="cardTypeRestriction">

<xs:restriction base="xs:string"><xs:enumeration value="VISA"/><xs:enumeration value="MASTER"/><xs:enumeration value="AMEX"/>

</xs:restriction></xs:simpleType><xs:complexType name="chequeType">

<xs:sequence><xs:element name="ChequeNumber" type="xs:positiveInteger"/><xs:element name="IssuingBank" type="xs:string"/><xs:element name="IssuingBranch" type="xs:string"/><xs:element name="Dated" type="xs:date"/>

</xs:sequence></xs:complexType>

</xs:schema>

XML Schema | Atul Kahate 108

Exercise – 2 Keep information about the scores of a

cricket team in a match in the following form: Team name Opposition team name Innings (1 or 2)

Batsman’s details Batting position (1 to 11) Batsman name How out (either Caught, Bowled, LBW, or Not Out) Bowler’s name Runs scored (0 to 400)

Thank you!

Any Questions?

top related