Documente Academic
Documente Profesional
Documente Cultură
Part 3: XML
XML has become the basis for all new generation data interchange
formats for the www.
Why XML?
Features of XML
XML was created to structure, store and transport data.
XML does not do anything, just contains information wrapped in tags.
Rules file
Specifies the documents 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)
Syntax of a DTD
Different keywords
ELEMENT - used to define tags.
ATTLIST - used to define tag attributes.
ENTITY - used to define entities.
Syntax of a DTD
Part A : Declaring Elements (contd.)
employees
person
name
gender
parent age
Syntax of a DTD
Part A: Declaring Elements (contd.)
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)>
<person>
<name></name>
<parent> </parent>
<parent> </parent>
Elements
<age> </age>
<spouse> </spouse>
<sibling> </sibling>
<sibling> </sibling>
..
</person>
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 >
Syntax of a DTD
Part B: Declaring Attributes (contd.)
Syntax of a DTD
Part B: Declaring Attributes (contd.)
Examples
</person>
Syntax of a DTD
Part C: Declaring Entities
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>
Syntax of a DTD
Part C: Declaring Entities (contd.)
E.g.
<!ENTITY footnote C:\..\..\footnote.txt>
Syntax of a DTD
Part A : Declaring Elements
employees
person
person
person person
OPTIONAL
External file
<!DOCTYPE root-element SYSTEM file-name>
e.g. <!DOCTYPE employees SYSTEM ../../emp.dtd >
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.
Limitations of DTDs
Limitations of DTDs
Limitations of DTDs
Limitations of DTDs
Main differences
XML schema is an XML document itself, so can be processed by a
XML parser.
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).
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>
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.
Defining Namespaces
A XML docs namespace uniquely identifies every element and attribute
used in that document.
Multiple namespaces can be used in a single document.
Defining Namespaces
A XML docs namespace uniquely identifies every element and attribute
used in that document.
Multiple namespaces can be used in a single document.
Default namespace is that used by XHTML docs. Here tags can appear
without prefixes.
<html xmlns = http://www.w3.org/1999/xhtml>
<bank xmlns:sbi=http://www.statebankofindia.com>
<sbi:branch>
<sbi:branchname>NITK</sbi:branchname>
<sbi:city>Surathkal</sbi:city>
</sbi:branch>
</bank>
Schema Fundamentals
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.
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=W3Cs namespace URI
targetNamespace = your websites namespace URI
xmlns = default namespace URI for your site
elementFormDefault = qualified>
.
.
.
</xs:schema>
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)
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>
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>
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
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>
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.