3 xml namespaces and xml schema
-
Upload
gauravashq -
Category
Education
-
view
799 -
download
1
description
Transcript of 3 xml namespaces and xml schema
XML Namespaces
XML Schema | Atul Kahate 3
Need for Namespaces XML allows document authors to create
custom elements At times, this leads to ambiguity/collisions Example:
<subject>Geometry</subject> <subject>Cardiology</subject>
The first subject is the one we study in college, the other one in medicine – but there is nothing to differentiate between them
XML Schema | Atul Kahate 4
Using Namespaces We can resolve this problem by
using namespaces as follows: <highschool:subject>Geometry</highschool:subject> <medicine:subject>Cardiology</medicine:subject>
Both highschool and medicine are namespace prefixes
We can provide any namespace prefix we want, except for the reserve words
XML Schema | Atul Kahate 5
Namespace Example<?xml version = "1.0"?>
<text:directoryxmlns:text = "www.one.com"xmlns:image = "www.two.com">
<text:file filename = "book.xml"><text:description>A book list</text:description>
</text:file>
<image:file filename = "book.xml"><image:description>A funny picture</image:description><image:size width = "200" height = 100" />
</image:file></text:directory>
XML Schema | Atul Kahate 6
Using Default Namespace<?xml version = "1.0"?>
<directoryxmlns = "www.one.com"xmlns:image = "www.two.com">
<file filename = "book.xml"><description>A book list</description>
</file>
<image:file filename = "book.xml"><image:description>A funny picture</image:description><image:size width = "200" height = 100" />
</image:file></directory>
Now, directory is an element that has a default namespace (i.e. no prefix attached).
Well-formed and Valid XML
XML Schema | Atul Kahate 8
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 Schemas
XML Schema | Atul Kahate 10
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
DTD Versus Schema: An Example
XML Schema | Atul Kahate 12
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 13
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 14
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 15
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 16
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 17
Exercise – 2 Modify the above example to split
the name into first and last name
Schema Basics and Syntaxes
XML Schema | Atul Kahate 19
The <schema> Element Root element of every XML schema
<?xml version="1.0"?><xsd:schema>......</xsd:schema>
Usually contains attributes, as shown next
XML Schema | Atul Kahate 20
Attributes in the <schema> Element<?xml version="1.0"?><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.test.com"xmlns="http://www.xyz.com"elementFormDefault="qualified">......</xsd: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 xsd:
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 21
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 22
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 <xsd:element name="xxx" type="yyy"/>
Examples<lastname>Tanenbaum</lastname> <age>60</age> <dateborn>1945-10-08</dateborn>
Corresponding schema declarations<xsd:element name="lastname" type="xsd:string"/><xsd:element name="age" type="xsd:integer"/> <xsd:element name="dateborn" type="xsd:date"/>
XML Schema | Atul Kahate 23
Schema Data Types and Default or Constant Values Basic data types
xsd:string xsd:decimal xsd:integer xsd:boolean xsd:date xsd:time
Declaring default values <xsd:element name="color" type="xsd:string"
default="red"/> Declaring constant values
<xsd:element name="color" type="xsd:string" fixed="red"/>
XML Schema | Atul Kahate 24
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
<xsd:attribute name="xxx" type="yyy"/> Example
<lastname lang="EN">Joshi</lastname> Corresponding schema declaration
<xsd:attribute name="lang" type="xsd:string"/>
XML Schema | Atul Kahate 25
More on Attributes Default values
<xsd:attribute name="lang" type="xsd:string" default="EN"/>
Fixed values <xsd:attribute name="lang" type="xsd:string"
fixed="EN"/> Mandatory attributes
<xsd:attribute name="lang" type="xsd:string" use="required"/>
Optional attributes <xsd:attribute name="lang" type="xsd:string"
use="optional"/>
Schema Example – See the difference between “ref” and “type”
XML Schema | Atul Kahate 27
XML <?xml version="1.0" encoding="utf-8" ?> <Employee lastUpdated="2007-04-21" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="emp_first.xsd"> <!-- <Employee lastUpdated="2007-04-21"> --> <Name>Manish Potdar</Name> <Contact> <EmpAddress> <Street>12 East Street</Street> <City>Pune</City> <Pin>411001</Pin> <State>Maharashtra</State> </EmpAddress> <HomePhone>02025530834</HomePhone> <Email>[email protected]</Email> </Contact> </Employee>
XML Schema | Atul Kahate 28
Schema <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:element name="Employee"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name"/> <xsd:element name="Contact"> <xsd:complexType> <xsd:sequence> <xsd:element
name = "EmpAddress" type="Address"/> <xsd:element
name="HomePhone"/> <xsd:element
name="Email"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name="lastUpdated"/> </xsd:complexType> </xsd:element> <xsd:complexType name="Address"> <xsd:sequence> <xsd:element name="Street"/> <xsd:element name="City"/> <xsd:element name="Pin"/> <xsd:element name="State"/> </xsd:sequence>
</xsd:complexType>
</xsd:schema>
XML Schema | Atul Kahate 29
Modified XML (emp_first.xml)<?xml version="1.0" encoding="utf-8" ?><Employee lastUpdated="2007-04-21"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="emp_first.xsd">
<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>[email protected]</Email> </Contact></Employee>
XML Schema | Atul Kahate 30
Modified Schema (emp_first.xsd)<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Employee"><xsd:complexType><xsd:sequence><xsd:element name="Name"/><xsd:element name="Contact"><xsd:complexType><xsd:sequence><xsd:element ref="Address"/><xsd:element name="HomePhone"/><xsd:element name="Email"/></xsd:sequence></xsd:complexType></xsd:element></xsd:sequence><xsd:attribute name="lastUpdated"/></xsd:complexType>
</xsd:element><xsd:element name="Address">
<xsd:complexType><xsd:sequence><xsd:element name="Street"/><xsd:element name="City"/><xsd:element name="Pin"/><xsd:element name="State"/></xsd:sequence></xsd:complexType>
</xsd:element>
</xsd:schema>
XML Schema | Atul Kahate 31
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.
XML Schema | Atul Kahate 32
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 33
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 35
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 37
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 38
Declaring Elements Example<xsd:element name =
“dateReceived”/>
Or, with data type<xsd:element name =
“dateReceived” type = “xsd:date”/>
XML Schema | Atul Kahate 39
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 40
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 41
Simple Types Contain information that cannot be split
further Example XML
<myElement>I am simple</myElement> Schema
<xsd:element name="myElement" type="xsd:string"/>
XML Schema | Atul Kahate 42
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 43
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 44
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 45
Combining Named and Unnamed Declarations <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--> <xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsd:element name="ContactDetails"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Contact" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Contact"> <xsd:complexType> <xsd:sequence> <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:element> <xsd:element name="billingAddress" type="Address"/> <xsd:element name="mailingAddress" type="Address"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="Address"> <xsd:sequence> <xsd:element name="Street1"/> <xsd:element name="Street1"/> <xsd:element name="City"/> <xsd:element name="Pin"/> <xsd:element name="State"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
XML Schema | Atul Kahate 46
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)--> <xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsd:element name="ContactDetails"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Contact" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Contact"> <xsd:complexType> <xsd:sequence> <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:element> <xsd:element name="billingAddress" type="Address"/> <xsd:element name="mailingAddress" type="Address"/> <xsd:element name="CreditAddressReference"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="Address"> <xsd:attribute name="residentFrom" type="xsd:date"/> <xsd:attribute name="residentTo" type="xsd:date"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="Address"> <xsd:sequence> <xsd:element name="Street1"/> <xsd:element name="Street1"/> <xsd:element name="City"/> <xsd:element name="Pin"/> <xsd:element name="State"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
XML Schema | Atul Kahate 47
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 49
Secondary Components Can be classified into
Model group definitions Attribute groups Notation declarations Identity constraints
XML Schema | Atul Kahate 50
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 51
group example<?xml version="1.0"?><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:group name="custGroup"><xsd:sequence>
<xsd:element name="customer" type="xsd:string"/><xsd:element name="orderdetails"
type="xsd:string"/><xsd:element name="billto" type="xsd:string"/><xsd:element name="shipto" type="xsd:string"/>
</xsd:sequence></xsd:group><xsd:element name="order" type="ordertype"/><xsd:complexType name="ordertype">
<xsd:group ref="custGroup"/><xsd:attribute name="status" type="xsd:string"/>
</xsd:complexType></xsd:schema>
XML Schema | Atul Kahate 52
Choice Example – Using group Syntax <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--> <xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsd:element name="FlightDetails"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name"/> <xsd:group ref="MealOptions"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:group name="MealOptions"> <xsd:choice> <xsd:element name="Vegetarian"/> <xsd:element name="Non-Vegetarian"/> <xsd:element name="Salad"/> </xsd:choice> </xsd:group> </xsd:schema>
XML Schema | Atul Kahate 53
Choice Example – Using Unnamed Syntax
<xsd:element name="person"> <xsd:complexType> <xsd:choice> <xsd:element name="employee"
type="employee"/> <xsd:element name="member"
type="member"/> </xsd:choice> </xsd:complexType></xsd:element>
XML Schema | Atul Kahate 54
All Example – Using group Syntax <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2
(http://www.altova.com)--> <xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsd:group name="person"> <xsd:all> <xsd:element name="firstname" type="xsd:string"/> <xsd:element name="lastname" type="xsd:string"/> </xsd:all> </xsd:group> <xsd:element name="test"> <xsd:complexType> <xsd:group ref ="person" /> </xsd:complexType> </xsd:element> </xsd:schema>
XML Schema | Atul Kahate 55
All Example – Using Unnamed Syntax
<xsd:element name="person"> <xsd:complexType> <xsd:all> <xsd:element name="firstname" type="xsd:string"/> <xsd:element name="lastname" type="xsd:string"/> </xsd:all> </xsd:complexType></xsd:element>
firstname and lastname are mandatory sub-elements – must occur exactly once
XML Schema | Atul Kahate 56
All – Another Example<xsd:group name = “CreditCardDetails”>
<xsd:all><xsd:element name = “CardType” /><xsd:element name = “CardHolder” /><xsd:element name = “CardNumber” /><xsd:element name = “CardExpiry” />
</xsd:all></xsd:group>
XML Schema | Atul Kahate 57
Sequence Example using group Syntax <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2
(http://www.altova.com)--> <xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsd:group name="person"> <xsd:sequence> <xsd:element name="firstname" type="xsd:string"/> <xsd:element name="lastname" type="xsd:string"/> </xsd:sequence> </xsd:group> <xsd:element name="test"> <xsd:complexType> <xsd:group ref ="person" /> </xsd:complexType> </xsd:element> </xsd:schema>
XML Schema | Atul Kahate 58
Sequence Example Using Unnamed Syntax Another element that we can use inside a
group is sequence Example
<xsd:group name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="firstname" type="xsd:string"/> <xsd:element name="lastname" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:element>
XML Schema | Atul Kahate 59
Other Details We can use minOccurs, maxOccurs
attributes on group, choice, sequence, and all elements.
XML Schema | Atul Kahate 60
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 61
Attribute Groups - Example <?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--> <xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsd:element name="product"> <xsd:complexType> <xsd:attributeGroup ref="productDetails"/> </xsd:complexType> </xsd:element> <xsd:attributeGroup name="productDetails"> <xsd:attribute name="productID" use="required" type="xsd:string"/> <xsd:attribute name="productName" use="required" type="xsd:string"/> <xsd:attribute name="productDescription" use="required" type="xsd:string"/> <xsd:attribute name="unit" use="required" type="xsd:positiveInteger"/> <xsd:attribute name="price" use="required" type="xsd:decimal"/> <xsd:attribute name="stock" use="required" type="xsd:integer"/> </xsd:attributeGroup> </xsd:schema>
XML Schema | Atul Kahate 62
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)--><xsd:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"><xsd:element name="product" default="book" />
</xsd:schema>
XML Schema | Atul Kahate 63
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 64
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)--><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:element name="product" fixed="book" /></xsd:schema>
Now, the value of the element in the XML document must either be book, or it should be empty
XML Schema | Atul Kahate 65
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 66
Nillable Element Example – 1 Schema<?xml version="1.0" encoding="UTF-8"?><!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:element name="product"><xsd:complexType>
<xsd:sequence><xsd:element name="productID"/><xsd:element name="productName"
nillable="true"/></xsd:sequence>
</xsd:complexType></xsd:element>
</xsd:schema>
XML Schema | Atul Kahate 67
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 68
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 69
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 71
Schema Facets (Restrictions) - 1 Restriction on values
<xsd:element name="age"><xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="100"/> </xsd:restriction></xsd:simpleType></xsd:element>
XML Schema | Atul Kahate 72
Schema Facets (Restrictions) - 2 Restriction on a set of values<xsd:element name="car“ />
<xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value=“Santro/> <xsd:enumeration value=“Maruti"/> <xsd:enumeration value=“Indica/> </xsd:restriction></xsd:simpleType>
</xsd:element>
XML Schema | Atul Kahate 73
Schema Facets (Restrictions) - 3 Restrictions on a series of values
<xsd:element name="letter"><xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="[a-z]"/> </xsd:restriction></xsd:simpleType></xsd:element> Allows one lowercase alphabet
<xsd:pattern value="[A-Z][A-Z][A-Z]"/> Allows three uppercase alphabets
<xsd:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> Allows three uppercase or lowercase alphabets
<xsd:pattern value="[xyz]"/> Allows one of the lowercase alphabets x, y, or z
<xsd:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> Allows five digits in sequence
XML Schema | Atul Kahate 74
Schema Facets (Restrictions) - 4 Other restrictions on a series of values
<xsd:element name="letter"><xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="([a-z])*"/> </xsd:restriction></xsd:simpleType></xsd:element> Allows 0 or more lowercase alphabets <xsd:pattern value="([a-z][A-Z])+"/>
One or more lowercase alphabets followed by an uppercase alphabet <xsd:pattern value=“worker|manager"/>
Allow either worker or manager <xsd:pattern value="[a-zA-Z0-9]{8}"/>
Allow exactly 8 characters, either alphabets or numbers <xsd:length value="8"/>
Allow exactly 8 characters <xsd:minLength value="5"/> <xsd:maxLength value="8"/>
Allow a minimum of 5 and maximum of 8 characters
Schema: Case Study
XML Schema | Atul Kahate 76
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 77
Start with the Schema Template<?xml version="1.0"?><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
...
</xsd:schema>
XML Schema | Atul Kahate 78
Create the shiporder Element Outline<xsd:element name="shiporder"> <xsd:complexType> <xsd:sequence> ... ... </xsd:sequence> ... </xsd:complexType></xsd:element>
XML Schema | Atul Kahate 79
Define the orderperson Element
<xsd:element name="orderperson" type="xsd:string"/>
XML Schema | Atul Kahate 80
Define the shipto and item Elements<xsd:element name="shipto"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="address" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="country" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:element>
<xsd:element name="item" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element name="note" type="xsd:string" minOccurs="0"/> <xsd:element name="quantity" type="xsd:positiveInteger"/> <xsd:element name="price" type="xsd:decimal"/> </xsd:sequence> </xsd:complexType></xsd:element>
XML Schema | Atul Kahate 81
Declare the attribute for the shiporder Element
<xsd:attribute name="orderid" type="xsd:string" use="required"/>
XML Schema | Atul Kahate 82
The Complete Schema<?xml version="1.0" encoding="ISO-8859-1" ?><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:element name="shiporder"> <xsd:complexType> <xsd:sequence> <xsd:element name="orderperson" type="xsd:string"/> <xsd:element name="shipto"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="address" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="country" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="item" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element name="note" type="xsd:string" minOccurs="0"/> <xsd:element name="quantity" type="xsd:positiveInteger"/> <xsd:element name="price" type="xsd:decimal"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name="orderid" type="xsd:string" use="required"/> </xsd:complexType></xsd:element>
</xsd:schema>
XML Schema | Atul Kahate 83
Making it more readable<?xml version="1.0" encoding="ISO-8859-1" ?><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements --><xsd:element name="orderperson" type="xsd:string"/><xsd:element name="name" type="xsd:string"/><xsd:element name="address" type="xsd:string"/><xsd:element name="city" type="xsd:string"/><xsd:element name="country" type="xsd:string"/><xsd:element name="title" type="xsd:string"/><xsd:element name="note" type="xsd:string"/><xsd:element name="quantity" type="xsd:positiveInteger"/><xsd:element name="price" type="xsd:decimal"/>
<!-- definition of attributes --><xsd:attribute name="orderid" type="xsd:string"/>
<!-- definition of complex elements --><xsd:element name="shipto"> <xsd:complexType> <xsd:sequence> <xsd:element ref="name"/> <xsd:element ref="address"/> <xsd:element ref="city"/> <xsd:element ref="country"/> </xsd:sequence> </xsd:complexType></xsd:element><xsd:element name="item"> <xsd:complexType> <xsd:sequence> <xsd:element ref="title"/> <xsd:element ref="note" minOccurs="0"/> <xsd:element ref="quantity"/> <xsd:element ref="price"/> </xsd:sequence> </xsd:complexType></xsd:element>
<xsd:element name="shiporder"> <xsd:complexType> <xsd:sequence> <xsd:element ref="orderperson"/> <xsd:element ref="shipto"/> <xsd:element ref="item" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attribute ref="orderid" use="required"/> </xsd:complexType></xsd:element>
</xsd:schema>
XML Schema | Atul Kahate 84
Adding more features<?xml version="1.0" encoding="ISO-8859-1" ?><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="stringtype"> <xsd:restriction base="xsd:string"/></xsd:simpleType>
<xsd:simpleType name="inttype"> <xsd:restriction base="xsd:positiveInteger"/></xsd:simpleType>
<xsd:simpleType name="dectype"> <xsd:restriction base="xsd:decimal"/></xsd:simpleType>
<xsd:simpleType name="orderidtype"> <xsd:restriction base="xsd:string"> <xsd:pattern value="[0-9]{6}"/> </xsd:restriction></xsd:simpleType>
<xsd:complexType name="shiptotype"> <xsd:sequence> <xsd:element name="name" type="stringtype"/> <xsd:element name="address" type="stringtype"/> <xsd:element name="city" type="stringtype"/> <xsd:element name="country" type="stringtype"/> </xsd:sequence></xsd:complexType>
<xsd:complexType name="itemtype"> <xsd:sequence> <xsd:element name="title" type="stringtype"/> <xsd:element name="note" type="stringtype" minOccurs="0"/> <xsd:element name="quantity" type="inttype"/> <xsd:element name="price" type="dectype"/> </xsd:sequence></xsd:complexType>
<xsd:complexType name="shipordertype"> <xsd:sequence> <xsd:element name="orderperson" type="stringtype"/> <xsd:element name="shipto" type="shiptotype"/> <xsd:element name="item" maxOccurs="unbounded" type="itemtype"/> </xsd:sequence> <xsd:attribute name="orderid" type="orderidtype" use="required"/></xsd:complexType>
<xsd:element name="shiporder" type="shipordertype"/>
</xsd:schema>
XML Schema | Atul Kahate 85
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 86
Solution – 1<?xml version="1.0" encoding="UTF-8"?><!--W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com)--><xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Order"><xsd:complexType>
<xsd:sequence><xsd:element name="Products" type="productsType"/><xsd:element name="Contact" type="contactType"/><xsd:element name="Payment" type="paymentType"/></xsd:sequence>
</xsd:complexType></xsd:element><xsd:complexType name="productsType">
<xsd:sequence><xsd:element name="Product" minOccurs="1" maxOccurs="unbounded" type="productType"/>
</xsd:sequence></xsd:complexType><xsd:complexType name="productType">
<xsd:sequence><xsd:element name="Product_ID" type="xsd:string"/><xsd:element name="Product_Name" type="xsd:string"/><xsd:element name="Product_Units" type="xsd:positiveInteger"/><xsd:element name="Product_Price" type="xsd:int"/>
</xsd:sequence></xsd:complexType><xsd:complexType name="contactType">
<xsd:sequence><xsd:element name="Name" type="xsd:string"/><xsd:element name="Address" type="xsd:string"/><xsd:element name="City" type="xsd:string"/><xsd:element name="Pin" type="xsd:int"/>
</xsd:sequence></xsd:complexType><xsd:complexType name="paymentType">
<xsd:sequence><xsd:choice><xsd:element name="CreditCard" type="creditCardType"/><xsd:element name="Cheque" type="chequeType"/></xsd:choice><xsd:element name="Amount" type="xsd:positiveInteger"/>
</xsd:sequence></xsd:complexType><xsd:complexType name="creditCardType">
<xsd:sequence><xsd:element name="CardType" type="cardTypeRestriction"/><xsd:element name="CardNumber" type="xsd:int"/><xsd:element name="ExpiryDate" type="xsd:date"/>
</xsd:sequence></xsd:complexType><xsd:simpleType name="cardTypeRestriction">
<xsd:restriction base="xsd:string"><xsd:enumeration value="VISA"/><xsd:enumeration value="MASTER"/><xsd:enumeration value="AMEX"/>
</xsd:restriction></xsd:simpleType><xsd:complexType name="chequeType">
<xsd:sequence><xsd:element name="ChequeNumber" type="xsd:positiveInteger"/><xsd:element name="IssuingBank" type="xsd:string"/><xsd:element name="IssuingBranch" type="xsd:string"/><xsd:element name="Dated" type="xsd:date"/>
</xsd:sequence></xsd:complexType>
</xsd:schema>
XML Schema | Atul Kahate 87
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)
XML Schemas: Case Study
XML Schema | Atul Kahate 89
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 90
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 91
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 92
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 93
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 94
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 95
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 96
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>
Thank you!
Any Questions?