IT702 – AWT (Jul – Dec 2016) 31-08-2016

Part 3: XML

Web Data Standards

XML and related technologies

Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 30-Aug-16

XML - eXtensible Markup Language

 Developed from SGML

 XML is used for data storage/processing and data exchange.
 XML is a meta-markup language
 HTML is a markup language, XML is used to define markup languages.
 e.g. XHTML, RSS, SOAP, SVG etc.

 Markup languages defined in XML are known as applications.
 For e.g, MIME type of XML based documents is application/xhtml+xml ….

 XML has become the basis for all new generation data interchange
formats for the www.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 1
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Why XML?

 can be used to describe and identify information accurately and
unambiguously
 computers can be programmed to ‘understand’ your information (well,
at least manipulate as if they could understand it).
 provides a standardised way of describing, controlling, or
allowing/disallowing particular types of document structure.
 provides a robust and durable format for information storage and
transmission.
 provides a common syntax for messaging systems for the exchange of
information between applications.
 lets you separate form (appearance) from content.

Dept of IT, NITK Surathkal 30-Aug-16

Features of XML
 XML was created to structure, store and transport data.
 XML does not do anything, just contains information wrapped in tags.

 XML is just plain text.
 Only XML aware applications can handle the XML tags specially.

 XML does not have pre-defined tags.

 XML preserves whitespace.

 XML is a complement, not a replacement for HTML !

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 2
IT702 – AWT (Jul – Dec 2016) 31-08-2016

The Syntax of XML
 Levels of syntax
 Well-formed : conform to basic XML rules.
 XML tags are case sensitive (lowercase only)
 One root tag that contains all the other tags in a document.
 Each begin tag has a matching closing tag.
 All elements must be properly nested.
 Attributes must have a value assigned, the value must be quoted.
 Attributes are specified by name=value pairs inside the starting tag of an
element.
 The characters <, >, & etc can only appear with their special meaning.
 Otherwise use entity references (e.g. &lt; &amp; etc.)

 Valid: documents are well-formed and also conform to a schema
which defines details of the allowed content.

Dept of IT, NITK Surathkal 30-Aug-16

XML – Necessary Accompaniments.

 Each XML document uses two auxiliary files –

 Rules file –
 Specifies the document’s tag set and the structural rules to which it must
conform.
 Options: DTD or an XML Schema

 Style file -
 Contains a style-sheet to describe how the content of the document is to be
displayed or printed.
 Options: Cascading Style Sheets or XSLT (XML Stylesheet Tranformations)

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 3
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Document Type Definitions (DTD)

 A set of structural rules called declarations which –
 Define tags, attributes, entities
 Specify the order and nesting of tags
 Specify which attributes can be used with which tags.
 Two ways of specifying a DTD -
 Internal DTD
 embedded in the XML document whose syntax rules it describes.
 External DTD
 Stored as a separate file.

 Important: A DTD is not an XML document!

Dept of IT, NITK Surathkal 30-Aug-16

Syntax of a DTD

 Each declaration has the form –

 <!KEYWORD element-name content-description>

 Different keywords
 ELEMENT - used to define tags.
 ATTLIST - used to define tag attributes.
 ENTITY - used to define entities.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 4
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Syntax of a DTD
Part A : Declaring Elements (contd.)

 Step 1: Declaring the parent element for each record
 each element declaration in a DTD specifies the structure of one category of
elements.
Syn: <!ELEMENT element_name(list of child elements)>
e.g. <!ELEMENT person(name,parent,age,gender ..)>

employees

person

name
gender
parent age

Dept of IT, NITK Surathkal 30-Aug-16

Syntax of a DTD
Part A: Declaring Elements (contd.)

 Step 2: Specifying Occurrences
 in many cases, it is necessary to specify the number of times a child
element may appear.
 This can be done by adding a quantifier to the child element’s
specification.
 Types: +
*
?

E.g. <!ELEMENT person(name, parent+, age, spouse?,
sibling*)>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 5
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Syntax of a DTD
Part A: Declaring Elements (contd.)
 Step 3: Specifying leaf-node datatypes
 Datatype of the content stored within the element tag.
 Most common data type –
 PCDATA (parseable character data – a string of any printable character except ‘<‘,
‘>’ and ‘&’ )
 Other types – EMPTY – if element has no content.
ANY - element may contain literally any content.
Syn: <!ELEMENT element-name(#PCDATA)>

e.g. <!ELEMENT person(name,parent+,age,spouse?,sibling*)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT parent(#PCDATA)>
<!ELEMENT age(#PCDATA)> .. .

Dept of IT, NITK Surathkal 30-Aug-16

DTD (with elements only)

<!ELEMENT person(name, parent+, age, spouse?, sibling*)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT parent(#PCDATA)>
<!ELEMENT age(#PCDATA)>
.
.
.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 6
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XML document corresponding to DTD (with elements only)

<?xml version=“1.0” encoding=“utf-8”?>

<person>
<name>………</name>
<parent> ……………</parent>
<parent> ……………</parent>
Elements
<age> ……………</age>
<spouse> ……………</spouse>
<sibling> ……………</sibling>
<sibling> ……………</sibling>
..
</person>

Dept of IT, NITK Surathkal 30-Aug-16

Syntax of a DTD
Part B: Declaring Attributes
 Attribute declaration contains –
 Name of the element to which that attribute belongs
 Name of the attribute
 Value of the attribute.
Syn: <!ATTLIST element-name attr_name attr_type
default_value >
OPTIONAL
 If an element has multiple attributes –
<!ATTLIST element-name
attr_name1 attr_type default_value_for_1
attr_name2 attr_type default_value_for_2
…………
attr_namen attr_type default_value_for_n >

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 7
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Syntax of a DTD
Part B: Declaring Attributes (contd.)

 Most commonly used attribute type is CDATA

 Possible default values for attributes –
Value Description
A value - Any quoted given value.
#FIXED - The quoted value which every element will have and
which cannot be changed.
#REQUIRED -No default value is specified, every instance of the
element must specify a value.
#IMPLIED -No default value is given ( the browser chooses the
default value)
- The element value may or may not be specified.

Dept of IT, NITK Surathkal 30-Aug-16

Syntax of a DTD
Part B: Declaring Attributes (contd.)

 Examples

<!ATTLIST person dept CDATA #REQUIRED>

 If an element has multiple attributes –

<!ATTLIST person dept CDATA #REQUIRED
company CDATA #FIXED “NITK”
room_no CDATA #IMPLIED
emp_role CDATA “teaching” >

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 8
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XML document corresponding to DTD
(with elements and attributes)
<?xml version=“1.0” encoding=“utf-8”?>
<person dept=“Accounts” gender=“male” emp_role=“non-
teaching“ company=“nitk”>
<name>………</name>
<parent> ……………</parent>
Attributes
<parent> ……………</parent>
<age> ……………</age>
<spouse> ……………</spouse>
Elements
<sibling> ……………</sibling>
.
.

</person>

Dept of IT, NITK Surathkal 30-Aug-16

Syntax of a DTD
Part C: Declaring Entities

 Entities can be defined so that they can be referenced anywhere in the
content of an XML document.
Syn: <!ENTITY entity_name “entity_value”>

E.g.
<!ENTITY nitk
“National Institute of Technology Karnataka,
Srinivas Nagar P.O, Surathkal, Mangalore”>
- This entity declaration in the DTD allows us to specify the complete
name with just a reference &nitk; in the corresponding XML
document.
<address>&nitk;</address>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 9
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Syntax of a DTD
Part C: Declaring Entities (contd.)

 If entity is longer than a few words (e.g. a section of a technical article,
footnotes etc.) its text can be defined outside the DTD and used as
follows-

Syn: <!ENTITY entity_name SYSTEM “file_location”>

- Keyword SYSTEM indicates that entity defined is in a separate file.

E.g.
<!ENTITY footnote “C:\..\..\footnote.txt”>

Dept of IT, NITK Surathkal 30-Aug-16

XML document corresponding to DTD
(with elements, attributes and entities)

<?xml version=“1.0” encoding=“utf-8”?>
<person dept=“Accounts” gender=“male” emp_role=“non-
teaching“>
<name>………</name>
<parent> ……………</parent> Attributes
<parent> ……………</parent>
Elements <age> ……………</age>
<spouse> ……………</spouse>
<sibling> ……………</sibling>
<address>&nitk;</address>
.
.
Entity
</person>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 10
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Syntax of a DTD
Part A : Declaring Elements

Step 4: Declaring the document root element:
 each element declaration in a DTD specifies the structure of one category of
elements.
Syn: <!ELEMENT element_name(list of child elements)>

e.g. <!ELEMENT employees(person+)>

employees

person
person
person person

Dept of IT, NITK Surathkal 30-Aug-16

XML document corresponding to DTD
(with root elements, record elements, attributes and entities)
<?xml version=“1.0” encoding=“utf-8”?>
<employees>
<person dept=“Accounts” gender=“male” emp_role=“non-
teaching“>
<name>………</name>
<parent> ……………</parent>
<parent> ……………</parent>
<age> ……………</age>
<spouse> ……………</spouse>
<sibling> ……………</sibling>
<address>&nitk;</address>
</person>
<person> …. </person>
</employees>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 11
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Internal and External DTDs
 A document type declaration can either contain declarations directly or can
refer to another file.
 Internal
 <!DOCTYPE root-element [declarations]>
e.g. <!DOCTYPE employees[!-–DTD for emp file--]>

OPTIONAL
 External file
 <!DOCTYPE root-element SYSTEM “file-name”>
e.g. <!DOCTYPE employees SYSTEM “../../emp.dtd ”>

Dept of IT, NITK Surathkal 30-Aug-16

Complete DTD for example discussed
<!DOCTYPE employees
[ emp.dtd
<!ELEMENT employees (person+)>
<!ELEMENT person(name, parent+, age, spouse?, sibling*)>
)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT parent(#PCDATA)>
<!ELEMENT age(#PCDATA)>
…..
<!ATTLIST person dept CDATA #REQUIRED>
<!ATTLIST person company CDATA #FIXED “NITK”>
<!ATTLIST person gender CDATA #IMPLIED>
<!ATTLIST person emp_role CDATA “teaching”>
….
<!ENTITY nitk “National Institute of Technology
Karnataka, Srinivas Nagar P.O, Surathkal, Mangalore”>
]
>
Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 12
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Complete XML document corresponding to the DTD
<?xml version=“1.0” encoding=“utf-8”?>
<!DOCTYPE employees SYSTEM “../../emp.dtd”> emp.xml
<employees>
<person dept=“Accounts” emp_role=“non-teaching“ ..>
<name>………</name>
<parent> ……………</parent>
. . .
<address>&nitk;</address>
</person>
<person dept=“IT” gender=“male” emp_role=“teaching“>
<name>………</name>
. . .
</person>
. . .
</employees>
Dept of IT, NITK Surathkal 30-Aug-16

Exercise
 Create a DTD for an online booklet for a used car sale consisting of
ads for different vehicles to be sold. Each ad gives the make (e.g.
Honda, Tata), model, year, color, engine-type, no-of doors, price,
seller and location of the seller. Declare entities for the names of
popular car makes.

 Create an XML file for above DTD with at least 5 instances of the car
element. Process the doc using the DTD and produce a display of the
raw XML document.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 13
IT702 – AWT (Jul – Dec 2016) 31-08-2016

eXtensible Markup Language

XML Namespaces and Schemas

Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 30-Aug-16

Limitations of DTDs

 DTDs have several deficits -
 They do not use XML syntax. It is sometimes difficult to deal with two
different syntactic forms ( one to define the document, the other to
define its structure.)

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 14
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Limitations of DTDs

 DTDs have several deficits -
 They do not use XML syntax. It is sometimes difficult to deal with two
different syntactic forms ( one to define the document, the other to
define its structure.)

 There are only 10 data types, none of which are numeric.

Dept of IT, NITK Surathkal 30-Aug-16

Limitations of DTDs

 DTDs have several deficits -
 They do not use XML syntax. It is sometimes difficult to deal with two
different syntactic forms ( one to define the document, the other to
define its structure.)

 There are only 10 data types, none of which are numeric.

 Attribute types cannot be strictly specified, can only be specified as text.
(CDATA)
 E.g.: if content of a tag represents time, DTD only specifies it as text

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 15
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Limitations of DTDs

 DTDs have several deficits -
 They do not use XML syntax. It is sometimes difficult to deal with two
different syntactic forms ( one to define the document, the other to
define its structure.)

 There are only 10 data types, none of which are numeric.

 Attribute types cannot be strictly specified, can only be specified as text.
(CDATA)
 E.g.: if content of a tag represents time, DTD only specifies it as text

 They do not support namespaces.

Dept of IT, NITK Surathkal 30-Aug-16

W3C Standard for XML Structure - Schemas

 XML Schema was designed by W3C as an alternative to DTDs.

 Main differences –
 XML schema is an XML document itself, so can be processed by a
XML parser.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 16
IT702 – AWT (Jul – Dec 2016) 31-08-2016

W3C Standard for XML Structure - Schemas

 XML Schema was designed by W3C as an alternative to DTDs.

 Main differences –
 XML schema is an XML document itself, so can be processed by a
XML parser.
 Provides 44 different data types to describe the content of a specific
element (tag).

Dept of IT, NITK Surathkal 30-Aug-16

W3C Standard for XML Structure - Schemas

 XML Schema was designed by W3C as an alternative to DTDs.

 Main differences –
 XML schema is an XML document itself, so can be processed by a
XML parser.
 Provides 44 different data types to describe the content of a specific
element (tag).
 Also allows defining new types with constraints on existing data types.
 For e.g. a numeric data value can be constrained to have exactly 6 digits for a
element <pincode>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 17
IT702 – AWT (Jul – Dec 2016) 31-08-2016

W3C Standard for XML Structure - Schemas

 XML Schema was designed by W3C as an alternative to DTDs.

 Main differences –
 XML schema is an XML document itself, so can be processed by a
XML parser.
 Provides 44 different data types to describe the content of a specific
element (tag).
 Also allows defining new types with constraints on existing data types.
 For e.g. a numeric data value can be constrained to have exactly 6 digits for a
element <pincode>
 Concept of namespaces introduced.

Dept of IT, NITK Surathkal 30-Aug-16

Need for Namespaces
 Sometimes XML docs are constructed by using tag-sets that may be
defined and used by other docs.
 Example 1: (data exchange)
 XML data has to be exchanged between organizations.
 Same tag name may have different meaning in different organizations,
causing confusion on exchanged documents.

 Example 2: (formatting for display)
 Creating an XML catalog with user-defined tags that may also be found
in HTML. (<title>, <body>, <table>, <form> etc..)
 For example, we may want to use the HTML <table> tag to format the display
of data stored in this XML document.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 18
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Need for Namespaces (contd.)

 To deal with these problems, W3C introduced a standard called XML
Namespaces.

 Basic Idea - Specifying a unique string as an element name avoids
confusion.
 Improved Idea: use unique-domain-name : element-name

Dept of IT, NITK Surathkal 30-Aug-16

Defining Namespaces
 A XML doc’s namespace uniquely identifies every element and attribute
used in that document.
 Multiple namespaces can be used in a single document.

 Syntax: <element-name xmlns[:prefix]=“URI”>
 The prefix is used to qualify elements that belong to the namespace.
 Symbols [ ] indicate that prefix is optional.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 19
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Defining Namespaces
 A XML doc’s namespace uniquely identifies every element and attribute
used in that document.
 Multiple namespaces can be used in a single document.

 Syntax: <element-name xmlns[:prefix]=“URI”>
 The prefix is used to qualify elements that belong to the namespace.
 Symbols [ ] indicate that prefix is optional.

 Default namespace is that used by XHTML docs. Here tags can appear
without prefixes.
<html xmlns = “http://www.w3.org/1999/xhtml”>

Dept of IT, NITK Surathkal 30-Aug-16

Defining Namespaces (example.)

<bank xmlns:sbi=“http://www.statebankofindia.com”>

<sbi:branch>

<sbi:branchname>NITK</sbi:branchname>

<sbi:city>Surathkal</sbi:city>

</sbi:branch>

</bank>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 20
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Schema Fundamentals

 Documents that conform to a schema’s rules are considered instances
of that schema.
 Analogy:
 Schema --- Class in an OO Language.
 XML doc --- Object of the class “Schema”

 Purpose of a Schema:
 Defines the structure of its instance XML docs, including
 which elements & attributes may appear
 where and
 how often.
 the data type of every element and attribute of its instance XML doc.

Dept of IT, NITK Surathkal 30-Aug-16

Defining a Schema
 Each schema uses XSD (XML Schema Definition) Language namespace. (prefix
“xs” or “xsd”)
 <xs:schema> element is the root element of every XML schema.
 Syntax:
<?xml version=“1.0” encoding=“utf-8”?>
<xs:schema xmlns:xs=“W3C’s namespace URI”
targetNamespace = “your website’s namespace URI”
xmlns = “default namespace URI for your site”
elementFormDefault = “qualified”>
.
.
.
</xs:schema>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 21
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Defining a Schema (contd.)
Example (same as the one discussed for DTD) –

<?xml version=“1.0” encoding=“utf-8”?>
<xs:schema xmlns:xs=“http://www.w3.org/2001/XMLSchema”
targetNamespace = “http://www.nitk.ac.in/”
xmlns = “http://www.nitk.ac.in/”
elementFormDefault = “qualified”>
.
.
.
.
emp.xsd
</xs:schema>

Dept of IT, NITK Surathkal 30-Aug-16

Referencing a Schema in an XML document
<?xml version="1.0" encoding=“utf-8” ?>

<employees xmlns = "http://www.nitk.ac.in"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.nitk.ac.in/files emp.xsd“
>
Two separate attributes are required
<person>
<name>…..</name>
<parent>....</parent>
<age>….</age>

</person> emp.xml
.
.
</employees>
Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 22
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Overview of Data Types used in XML Schema

 Data types are of two kinds

 Simple data types
 Content is restricted to text. It cannot contain any other elements or
attributes.

 Complex data types
 Defined for elements that contain other elements, attributes and string
content.

Dept of IT, NITK Surathkal 30-Aug-16

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 23
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Built-in Simple Types <xs:element>
 Content : text only, cannot contain any other elements or have any attributes.
 Common built-in data types - xs:string, xs:decimal, xs:integer, xs:date, xs:time...
 Syntax:
<xs:element name=“value” type=“value” optional_arguments=““/>
Optional arguments - default, fixed values or more than one occurrences.

Examples :
<xs:element name=“emp_role” type=“xs:string”
default=“Teaching” />
<xs:element name=“country” type=“xs:string” fixed=“India” />
<xs:element name=“parents” type=“xs:string”
minOccurs=“1” maxOccurs=“2” />
(value can be “unbounded” wherever needed)

Dept of IT, NITK Surathkal 30-Aug-16

User defined Simple types
 Built-in data types with some constraints.
 These properties are called restrictions or facets.
 Uses <xs:simpleType> inside the built-in tag <xs:element>

 Syntax and Example:
<xs:element name=“ename”>
<xs:simpleType>
<xs:restriction base=“xs:string”>
<xs:maxlength value=“20”/>
<xs:minlength value=“3”/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 24
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Complex Types <xs:complexType>
 Used for 4 types of elements –
 Empty elements
e.g. <spouse value=“yes”/>
 Elements that contain other elements. (most commonly used)
e.g. <ename>
<fname> … </fname>
<mid>… </mid>
<lname> … </lname>
</ename>
 Elements with attributes that contain text.
e.g. <parent rel=“father”> …</parent>
 Elements with attributes that contain both text and other elements
e.g. <note lang=“english”>This person is a
<emptype>adjunct</emptype> faculty </note>
Dept of IT, NITK Surathkal 30-Aug-16

Complex Types (contd.)
Defining Complex types –

1. Ordered Group
<xs:sequence> element

 Example
<xs:element name=“ename”>
<xs:complexType>
<xs:sequence>
<xs:element name=“fname” type=“xs:string”/>
<xs:element name=“mid” type=“xs:string”/>
<xs:element name=“lname” type=“xs:string”/>
</xs:sequence>
</xs:complexType>
</xs:element>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 25
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Complex Types (contd.)
Defining Complex types –

2. Unordered Group
 <xs:all> element

 Example:
<xs:element name=“depts”>
<xs:complexType>
<xs:all>
<xs:element name=“IT” type=“xs:string”/>
<xs:element name=“CS” type=“xs:string”/>
<xs:element name=“EE” type=“xs:string”/>
</xs:all>
</xs:complexType>
</xs:element>

Dept of IT, NITK Surathkal 30-Aug-16

XML file
<?xml version="1.0" encoding=“utf-8” ?>
<employees xmlns = "http://www.nitk.ac.in"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.nitk.ac.in emp.xsd“ >
<person>
<name>…..</name>
<dept> …. </dept>
<parent>....</parent>
<parent> …</parent>
<spouse> … </spouse>
<age>….</age>
<gender>…</gender>
<address> … </address>
</person>
.

.

</employees>
Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 26
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XML Schema
<?xml version="1.0“ encoding=“utf-8” ?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
<
targetNamespace = "http://www.nitk.ac.in"
xmlns = "http://www.nitk.ac.in"
elementFormDefault = "qualified">
emp.xsd
.
.
.
.

</
</xs:schema>

Dept of IT, NITK Surathkal 30-Aug-16

XML Schema (contd.)
<?xml version="1.0“ encoding=“utf-8” ?>
<xs:schema xmlns:xs =
<
"http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.nitk.ac.in"
xmlns = "http://www.nitk.ac.in"
elementFormDefault = "qualified">
<
<xs:element name=“employees”>
emp.xsd
<xs:complexType>
.
.
.
</xs:complexType>
</
</xs:element>
</
</xs:schema>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 27
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XML Schema (contd.)
<?xml version="1.0“ encoding=“utf-8” ?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
<
targetNamespace = "http://www.nitk.ac.in"
xmlns = "http://www.nitk.ac.in"
elementFormDefault = "qualified">
<
<xs:element name=“employees”>
emp.xsd
<xs:complexType>
<xs:element name=“person">
<xs:complexType>
.
.
</xs:complexType>
</xs:element>
</xs:complexType>
</
</xs:element>
</
</xs:schema>

Dept of IT, NITK Surathkal 30-Aug-16

XML Schema (complete)
<?xml version="1.0“ encoding=“utf-8” ?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
<
targetNamespace = "http://www.nitk.ac.in"
xmlns = "http://www.nitk.ac.in"
elementFormDefault = "qualified">
<
<xs:element name=“employees”>
<xs:complexType>
emp.xsd
<xs:element name=“person">
<xs:complexType>
<xs:sequence>
<xs:element name=“name" type="xs:string"/>
<xs:element name=“parent" type="xs:string“ minoccurs =“1”
maxoccurs =“2”/>
<xs:element name=“age" type="xs:positiveInteger"/>
.
.
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:complexType>
</
</xs:element>
</ Dept of IT, NITK Surathkal
</xs:schema> 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 28
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Exercise

 Create a Schema for an online booklet for a used car sale consisting of
ads for different vehicles to be sold. Each ad gives the make (e.g.
Honda, Tata), model, year, color, engine-type, no-of doors, price,
seller and location of the seller.

 Create an XML file for above Schema with at least 3 instances of the
car element. Process the doc using the Schema and produce a display
of the raw XML document.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 29

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.