Mukesh N Tekwani
Mumbai, India
mukeshtekwani@outlook.co
m
Disadvantages of HTML – Need for
XML
 HTML lacks syntax checking
 HTML lacks structure
 HTML is not suitable for data interchange
 HTML is not context aware – HTML does not
allow us to describe the information content or
the semantics of the document
 HTML is not object-oriented
 HTML is not re-usable
 HTML is not extensible
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
2
Introduction to XML
 XML – Extensible Markup Language
 Extensible – capable of being extended
 Markup – it is a way of adding information to the text
indicating the logical components of a document
 How is it different from HTML?
 HTML was designed to display data
 XML was designed to store, describe and transport
data
 XML is also a markup language like HTML
 XML tags are not predefined – we must design
our own tags.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
3
Differences between HTML and
XML
HTML XML
1. Designed to display data 1. Designed to store and transport
data between applications and
databases.
2. Focus is on how data looks 2. Focus is on what data is
3. It has pre-defined tags such as
<B>, <LI>, etc
3. No predefined tags; all tags must
be defined by the user. E.g., we can
create tags such as <TO>,
<FROM>, <BOOKNAME>, etc
4. HTML is used to display
information
4. XML is used to describe
information
5. Every tag may not have a closing
tag.
5. Every tag must have a closing
tag.
6. HTML is not case sensitive. 6. XML is case sensitive
7. HTML is for humans 7. XML is for computersNovember 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
4
Advantages of XML - 1
 XML simplifies data sharing
 Since XML data is stored in plain text format, data
can be easily shared among different hardware
and software platforms.
 XML separates data from HTML
 To display dynamic data in HTML, the code must
be rewritten each time the data changes. With
XML, data can be stored in separate files so that
whenever the data changes it is automatically
displayed correctly. We have to design the HTML
for layout only once.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
5
Advantages of XML - 2
 XML simplifies data transport
 With XML, data can be easily exchanged between
different platforms.
 XML makes data more available
 Since XML is independent of hardware, software and
application, XML can make your data more available
and useful.
 Different applications can access your data in HTML
pages
 XML provides a means to package almost any
type of information (binary, text, voice, video) for
delivery to a receiving end.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
6
Advantages of XML - 3
 Internationality
 HTML relies heavily on ASCII which makes using
foreign characters very difficult. XML uses
Unicode so that many European and Asian
languages are also handled easily
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
7
XML Document – Example 1
<?xml version="1.0" encoding="ISO-8859-1"?>
<class_list>
<student>
<name>Anamika</name>
<grade>A+</grade>
</student>
<student>
<name>Veena</name>
<grade>B+</grade>
</student>
</class_list>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
9
XML Document–Example 1 -
Explained
 The first line is the XML declaration.
 <?xml version="1.0" encoding="ISO-8859-1"?>
 It defines the XML version (1.0)
 It gives the encoding used (ISO-8859-1 = Latin-1/West
European character set)
 The XML declaration is actually a processing instruction
(PI) an it is identified by the ? At its start and end
 The next line describes the root element of the
document (like saying: "this document is a class_list“)
 The next 2 lines describe 2 child elements of the
root (student, name, and grade)
 And finally the last line defines the end of the root
element: </class_list>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
10
Logical Structure
 XML uses its start tags and end tags as
containers.
 The start tag, the content and the end tag form
an element
 Elements are the building blocks out of which
an XML document is assembled.
 An XML document has a tree-like structure
with the root element at the top and all the
other elements are contained within each
other.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
11
Tree structure
 XML documents form a tree structure.
 XML documents must contain a root element.
This element is "the parent" of all other elements.
 The elements in an XML document form a
document tree. The tree starts at the root and
branches to the lowest level of the tree.
 All elements can have sub elements (child
elements)
 <root>
<child>
<subchild>.....</subchild>
</child>
</root
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
12
XML – Example 2
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
13
XML – Example 2
<bookstore>
<book category = "COOKING">
<title lang = "en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category = "CHILDREN">
<title lang = "en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category = "WEB">
<title lang = "en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
14
Important Definitions
 XML Element
 An element is a start tag, content, and an end tag.
 E.g., <greeting>”Hello World</greeting>
 XML Attribute
 An attribute provides additional information about
elements
 E.g., <note priority = “high”>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
15
Important Definitions
 Child elements – XML elements may have
child elements
<employee id = “100”>
<name>
<first>Anita</first>
<initial>D</initial>
<last>Singh</last>
</name>
</employee>
Parent Element
Name
Children of parent
element
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
16
XML Element
 An XML element is everything from the
element's start tag to the element's end tag.
 An element can contain other elements,
simple text or a mixture of both.
 Elements can also have attributes.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
17
XML Syntax Rules
 All XML elements must have a closing tag
 XML tags are case sensitive.
 The tag <Book> is different from the tag <book>
 Opening and closing tags must be written with
the same case
<Message>This is incorrect</message>
<message>This is correct</message>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
18
XML Syntax Rules
 XML elements must be properly nested
 HTML permits this:
<B><I>This text is bold and italic</B></I>
But in XML this is invalid. All elements must be properly
nested within one another.
<B><I>This text is bold and italic</I></B>
 XML documents must have a root element. It is the
parent of all other elements.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
19
XML Syntax Rules
 XML Entity References
 Some characters have a special meaning in
XML. E.g., If you place a character like "<"
inside an XML element, it will generate an
error because the parser interprets it as the
start of a new element.
 <message>if salary < 1000 then </message>
 To avoid this error, replace the "<" character
with an entity reference:
 <message>if salary &lt; 1000 then</message>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
20
XML Syntax Rules
 XML Entity References
 There are 5 predefined entity references in
XML:
Entity Symbol Description
&lt; < Less than
&gt; > Greater than
&amp; & Ampersand
&apos; ‘ Apostrophe
&quot; “ Quotation mark
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
21
XML Syntax Rules
 Comments in XML (similar to HTML)
<!-- This is a comment -->
 White space is preserved in XML but not in HTML
 XML Naming Rules
 Names can contain letters, numbers, and other
characters
 Names cannot start with a number or punctuation
character
 Names cannot start with the letters xml (or XML, or
Xml, etc)
 Names cannot contain spaces
 Any name can be used, no words are reserved.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
22
XML Markup Delimiters
 Every XML element is made up of the
following parts:
Symbol Description
< Start tag open delimiter
</ End tag open delimiter
something element name
> tag close delimiter
/> empty tag close delimiter
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
23
Different Types of XML Markups
 Element Markup
 It is composed of 3 parts: start tag, the content,
and the end tag.
 Example: <name>Neetu</name>
 The start tag and the end tag can be treated as
wrappers
 The element name that appears in the start tag
must be exactly the same as the name that
appears in the end tag.
 Example: <Name>Neetu</name>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
24
Different Types of XML Markups
 Attribute Markup
 Attributes are used to attach information to the
information contained in an element.
 General syntax for attributes is:
 <elementname property = ‘value’>
Or
 <elementname property = “value”>
 Attribute value must be enclosed within quotation
marks
 Use either single quotes or double quotes but
don’t mix them. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
25
Attribute Markup
 If we specify the attributes for the same
element more than once, the specifications are
merged.
<?xml version = “1.0”?>
<myparas>
<para num = “first”>This is Para 1 </para>
<para num = ‘second’ color = “red”>This is Para
2</para>
<myparas>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
26
Attribute Markup
 When the XML processor encounters line 3, it
will record the fact that para element has the
num attribute
 When it encounters the 4th line it will record the
fact that para element has the color attribute
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
27
Reserved Attribute
 The xml:lang attribute is reserved to identify
the human language in which the element was
written
 The value of attribute is one of the following:
 en English
 fr French
 de German
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
28
XML Attributes
 Attribute provides additional information about the element
 Similar to attributes in HTML e.g., <IMG SRC=“sky.jpg”> In this SRC is the
attribute
 XML Attribute values must be quoted
 XML elements can have attributes in name/value pairs just like in HTML.
 In XML the attribute value must always be quoted.
<note date = 01/01/2010> <---------- This is invalid
<to>Priya</to>
<from>Deepali</from>
</note>
<note date = “01/01/2010”> --------- Now OK since enclosed in double quotes
<note date = ‘01/01/2010’> --------- This is also OK since enclosed in single
quotes
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
29
XML Attributes and Elements
 Consider the following example:
<person gender = "female">
<firstname>Geeta</firstname>
<lastname>Shah</lastname>
</person>
<person>
<gender>female</gender>
<firstname>Geeta</firstname>
<lastname>Shah</lastname>
</person>
Gender is an
attribute
Gender is an
element
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
30
Problems with XML Attributes
 Attributes cannot contain multiple values
whereas elements can
 Attributes cannot contain tree structures
 Attributes are not easily expandable (for future
changes)
 Attributes are difficult to read and maintain
 Use elements for data.
 Use attributes for information that is not
relevant to the data.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
31
Illustrating Problematic
Attributes
 Consider the following example:
<note day=“03" month="02" year="2010"
to="Tina" from=“Yasmin" heading="Reminder"
body=“Happy Birthday!">
</note>
 Better way:
<note>
<date>
<day>03</day>
<month>02</month>
<year>2010</year>
</date>
<to>Tina</to>
<from>Yasmin</from>
<heading>Reminder</heading>
<body>Happy Birthday!</body>
</note> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
32
When to use Attributes?
 XML Attributes can be used to assign ID references to
elements.
 Metadata – data about data – should be stored as attributes
 The ID can then be used to identify the XML element
<messages>
<note id="501">
<to>Tina</to>
<from>Yasmin</from>
<heading>Reminder</heading>
<body>Happy Birthday!</body>
</note>
<note id="502">
<to>Yasmin</to>
<from>Tina</from>
<heading>Re: Reminder</heading>
<body>Thank you, my dear</body>
</note>
</messages>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
33
What does Extensible mean in
XML?
 Consider the following XML example:
<note>
<to>Anita</to>
<from>Veena</from>
<body>You have an exam tomorrow</body>
</note>
Suppose we create an application that extracted the
<to>, <from> and <body> elements from the XML
document to produce the result:
MESSAGE
To: Anita
From:Veena
You have an exam tomorrow
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
34
What does Extensible mean in
XML?
 Now suppose the author of the XML document
added some extra information to it:
<note>
<date>2008-01-10</date>
<to>Anita</to>
<from>Veena</from>
<heading>Reminder</heading>
<body>You have an exam tomorrow</body>
</note>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
35
What does Extensible mean in
XML?
This application will not crash because it will still
find the <to>, <from> and <body> elements in
the XML document and produce the same
output.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
36
XML Validation
 What is a “well formed” XML document?
 XML with correct syntax is "Well Formed" XML.
 A "Well Formed" XML document has correct XML
syntax.
 XML documents must have a root element
 XML elements must have a closing tag
 XML tags are case sensitive
 XML elements must be properly nested
 XML attribute values must be quoted
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
37
Wellformed Document - Rule 1
 Elements are case-sensitive.
 If you define your language to use lowercase
elements, then all instances of those elements
must be in lowercase.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
38
Bad Examples…
<H1>Sample Heading</H1>
<h1>Sample Heading</H1>
<H1>Sample Heading</h1>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
39
Rule 2:
 All elements that contain text or other
elements must have both start and ending
tags.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
40
Rule 3:
 All empty elements (commonly known as
standalone tags) must have a slash (/) before
the end of the tag.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
41
Rule 4:
 All attribute values must be contained in
quotes, either single or double – no
exceptions!
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
42
Rule 5:
 Elements may not overlap.
 Elements must be nested properly within
other elements and can not start before a
sub-element and end within the sub-element.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
43
Rule 6:
 Isolated markup characters (characters
essential to creating markup documents) may
not appear in parsed content as is.
 Isolated markup characters must be
represented as a character entity and include
the following: <, [, ], >, ', " and &.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
44
Isolated Markup Characters
< &lt;
[ &#91;
] &#93;
> &gt;
' &apos;
" &quot;
& &amp;
November 9, 2019
Mukesh N Tekwani
(mukeshtekwani@outlook.com)
45
Bad Examples…
<h1>Jack &amp Jill</h1>
<equation>5 &lt 2</equation>
These examples are invalid since they are both
examples forgetting the semi-colon following
the character entity.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
46
Good Examples…
<h1>Jack &amp; Jill</h1>
<equation>5 &lt; 2</equation>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
47
Rule 7:
 Element (and attribute) names must start with
either a letter (uppercase or lowercase) or an
underscore.
 Element names may contain letters, numbers,
hyphens, periods and underscores.
BAD
EXAMPLES
<bad*characters>
<illegal space>
<99number-start>
GOOD
EXAMPLES
<example-one>
<_example2>
<Example.Three>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
48
XML Structure
 An XML document has a number of components
that can be used for representing information in a
hierarchical order
 These components are:
 Processing Instruction (PI)
 Tags
 Elements
 Attributes
 Entities
 Comments
 Content
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
49
Processing Instruction (PI)
 Every XML document begins with a processing
instruction (PI)
 The PI is also called as XML declaration
 The PI indicates the version of XML
 Typical PI statement:
<?xml version = “1.0” encoding = “UTI-8” ?>
 All XML parsers support the Unicode “UTF-8” and
“UTF-16” encodings. UTF-8 is used to create pages
written in English.
 UTF stands for Universal Character Set
Transformation Format
 This character set (UTF-8) uses 8 bits of information
to represent each character November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
50
Processing Instruction (PI)
 The xml declaration is case sensitive. It cannot
start with capital XML!
 If the XML declaration appears in a XML
document, it must be the first statement; even
comments and white spaces cannot appear
before it
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
51
Tags
 Tags are used to identify data
 XML does not have predefined tags
 XML tags begin with the < sign and end with
the > sign.
 All tags must occur in pairs; each pair consists
of a start tag and an end tag
 E.g., <book> is the start tag and </book> is the
end tag
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
52
Elements
 Element is the basic unit used to identify and
describe the XML data
 Elements are the basic building blocks of an
XML document
 Element begins with a start tag such as
<book> and ends with an end-tag such as
</book>
 Some elements may be empty i.e., they have
no content.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
53
Rules to Use Elements
 Elements must not overlap: a tag opened last must be
the first one to be closed (LIFO structure)
 E.g., the following is incorrect:
<book><author>Mukesh N Tekwani</book></author>
It is incorrect because the author tag was the last one to be opened
so it must be the first tag to be closed.
Correct:
<book><author>Mukesh N Tekwani</author></book>
 An XML document has exactly one root
 XML element names are case sensitive. So <book>
and <Book> are tow different elements. So we cannot
use something like this:
 <book>Introduction to XML <Book>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
54
Comments
 Comments begin with <!– and end with -->
 Comments can contain any data except the
literal string --
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
55
XML Validation
 A “well formed” XML document conforms to the
rules of a Document Type Definition (DTD)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tina</to>
<from>Yasmin</from>
<heading>Reminder</heading>
<body>Happy Birthday!</body>
</note>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
56
 DTD stands for Document Type Definition.
 A document type definition provides a list of
the elements, attributes, and entities contained
in a document, as well as their relationships to
one another.
 DTDs specify a set of rules for the structure of
a document. For example, a DTD may indicate
that a BOOK element has only one ISBN child,
exactly one TITLE child, and one or more
AUTHOR children, and it may or may not
contain a single SUBTITLE.
XML Document Type Definition
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
58
 A DTD can identify which elements are valid
for a particular document and which attributes
are valid to use with each of those elements.
 DTDs can be included in the file that contains
the document they describe, or they can be
linked from an external URL. This is called
Internal DTD.
 DTD can be stored in a separate file and is
called External DTD. External DTDs can be
shared by different documents and Web sites.
 DTD is used to validate an XML document.
XML Document Type Definition
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
59
DTD Elements
 Child Elements
 When defining child elements in DTDs, we can specify
how many times those elements can appear by
adding a modifier after the element name.
 If no modifier is added, the element must appear once
only
 Modifier Description
? Zero or one times.
+ One or more times.
* Zero or more times.
 It is not possible to specify a range of times that an
element may appear (e.g, 2-4 appearances).
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
60
DTD Elements and Attributes
 Attributes are declared using the <!ATTLIST >
declaration. The syntax is shown below.
 <!ATTLIST ElementName AttributeName
AttributeType State DefaultValue?
AttributeName AttributeType State
DefaultValue?>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
61
DTD Elements
 In a DTD elements are declared with an ELEMENT
declaration.
 Empty Elements
Empty elements are declared with the category keyword
EMPTY:
<!ELEMENT element-name EMPTY>
Example:
<!ELEMENT br EMPTY>
XML example:
<br />
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
62
DTD Elements
 Elements with Parsed Character Data
 Elements with parsed character data are
declared with #PCDATA inside parentheses:
<!ELEMENT element-name (#PCDATA)>
Example:
<!ELEMENT from (#PCDATA)>
PCDATA means any plain text values
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
63
DTD Elements
 Elements with any Contents
Elements declared with the category keyword
ANY, can contain any combination of parsable
data:
 <!ELEMENT element-name ANY>
Example:
<!ELEMENT note ANY>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
64
DTD Elements
 Elements with Children (sequences)
 Elements with one or more children are declared
with the name of the children elements inside
parentheses:
 <!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1, child2, ...)>
Example:
<!ELEMENT note (to, from, heading, body)>
 When children are declared in a sequence
separated by commas, the children must appear
in the same sequence in the document.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
65
External DTD
 If the DTD is declared in an external file, it
should be enclosed in a DOCTYPE definition
with the following syntax:
<!DOCTYPE root-element SYSTEM
"filename">
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
66
External DTD
 XML File:
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Raja</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
67
External DTD
 DTD file:
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
68
PCDATA
 PCDATA means Parsed Character Data.
 Parsed data means any plain text data
 Character data is the text found between the start
tag and the end tag of an XML element.
 PCDATA is text that WILL be parsed by a parser.
The text will be examined by the parser for entities
and markup.
 Tags inside the text will be treated as markup and
entities will be expanded.
 But, parsed character data should not contain any
&, <, or > characters; these must be represented
by the &amp; &lt; and &gt; entities, respectively.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
69
PCDATA
 Consider the declaration: <!ELEMENT YEAR
(#PCDATA)> This declaration says that a YEAR
may contain only parsed character data, i.e. text
that is not marked up. It may not contain children
of its own.
 So the following are all valid expressions:
<YEAR>2010</YEAR>.
<YEAR>2010 – The Decade of IT</YEAR>
 XML will not attempt to validate the contents of
PCDATA; it only checks that the text does not
contain markup. So the following is invalid:
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
70
Invalid
because
<YEAR>
cannot have
children of its
own – in this
case
<MONTH>
PCDATA
 Invalid: (why invalid?)
<YEAR>
<MONTH>January</MONTH>
<MONTH>February</MONTH>
<MONTH>March</MONTH>
<MONTH>April</MONTH>
<MONTH>May</MONTH>
<MONTH>June</MONTH>
<MONTH>July</MONTH>
<MONTH>August</MONTH>
<MONTH>September</MONTH>
<MONTH>October</MONTH>
<MONTH>November</MONTH>
<MONTH>December</MONTH>
</YEAR> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
71
CDATA
 CDATA means character data.
 CDATA is text that will NOT be parsed by a
parser.
 Tags inside the text will NOT be treated as
markup and entities will not be expanded.
 A CDATA section to indicate that a block of text
is to be presented as is with no translation.
CDATA sections begin with <![CDATA[ and end
with ]]>.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
72
CDATA
 Characters like "<" and "&" are illegal in XML
elements.
 "<" will generate an error because the parser
interprets it as the start of a new element.
 "&" will generate an error because the parser
interprets it as the start of an character entity.
 To avoid these errors, code can be defined as
CDATA.
 Everything inside a CDATA section is ignored
by the parser.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
73
CDATA
 For example:
<![CDATA[
<?xml version=”1.0”?>
<GREETING>
Hello XML!
</GREETING>
]]>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
74
Working Example 1
 Consider the following XML file:
<?xml version="1.0" ?>
<videos>
<music>
<title>Video Title 1 </title>
<artist>Artist 1 </ artist >
</music>
< music >
<title>Video Title 2 </title>
< artist > Artist 2 </ artist >
< artist > Artist 3 </ artist >
</ music >
</ videos >
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
75
Working Example 1 - contd
 The above XML file has data related to music
videos.
 In this XML file the root node is <videos> and
this can have any number of <music>
elements. The <music> element can have a
single <title> and any number of <artist> child
nodes.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
76
Working Example 1 - contd
 The DTD for this XML is :
<!ELEMENT videos ( music+ ) >
<!ELEMENT music ( title, artist+ ) >
<!ELEMENT title ( #PCDATA ) >
<!ELEMENT artist ( #PCDATA ) >
ELEMENT indicates that the word next to that is an
element and the nodes of that particular element are
given in the brackets.
The + sign after the node in the bracket denotes that it
could be any number of nodes.
The #PCDATA indicates that it is a text node that has
some value in it.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
77
Viewing XML Files - 1
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
78
Viewing XML Files - 2
 The XML document will be displayed with color-
coded root and child elements.
 A plus (+) or minus sign (-) to the left of the
elements can be clicked to expand or collapse
the element structure.
 To view the raw XML source (without the + and
- signs), select "View Page Source" or "View
Source" from the browser menu.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
79
Viewing XML Files - 3
 Why XML documents display like this?
 XML documents do not carry information about
how to display the data.
 Since XML tags are created by the user of the XML
document, browsers do not know if a tag like
<table> describes an HTML table or a dining table.
 Without any information about how to display the
data, most browsers will just display the XML
document as it is.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
80
Using CSS to display XML Files
 CSS (Cascading Style Sheets) can be used to
format a XML document.
 Consider this XML document:
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
81
Displaying Formatted XML
document-1
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type = "text/css" href = "birthdate.css"?>
<birthdate>
<person>
<name>
<first>Anokhi</first>
<last>Parikh</last>
</name>
<date>
<month>01</month>
<day>21</day>
<year>1992</year>
</date>
</person>
</birthdate> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
82
Displaying Formatted XML
document-2
birthdate
{
background-color: #ffffff;
width: 100%;
}
person
{
margin-left: 0;
}
name
{
color: #FF0000;
font-size: 20pt;
}
month, day, year
{
display:block;
color: #000000;
margin-left: 20pt;
}
Stylesheet – birthdate.css
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
83
Final Output
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
84
XML and CSS - Example 2
 Filename: PARTS.XML
<?xml version="1.0"?>
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>
<PARTS>
<TITLE>Computer Parts</TITLE>
<PART>
<ITEM>Motherboard</ITEM>
<MANUFACTURER>ASUS</MANUFACTURER>
<MODEL>P3B-F</MODEL>
<COST> 123.00</COST>
</PART>
</PARTS>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
85
XML and CSS - Example 2
 DTD File "PARTS.DTD".
 <!ELEMENT PARTS (TITLE?, PART*)>
 <!ELEMENT TITLE (#PCDATA)>
 <!ELEMENT PART (ITEM, MANUFACTURER,
MODEL, COST)+>
 <!ELEMENT ITEM (#PCDATA)>
 <!ELEMENT MANUFACTURER (#PCDATA)>
 <!ELEMENT MODEL (#PCDATA)>
 <!ELEMENT COST (#PCDATA)>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
86
XML and CSS – Example 2
 The root element is PARTS.
 The root element may contain no TITLE
element or one TITLE element along with any
number of PART elements.
 The PART element must contain one each of
items ITEM, MANUFACTURER, MODEL, and
COST in order.
 The elements TITLE, ITEM,
MANUFACTURER, MODEL, and COST all
contain PCDATA which is parsed character
data.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
87
XML and CSS – Example 2
PARTS { display: block }
TITLE
{ display: block; font-family: arial; color:
#008000; font-weight: 600; font-size: 22;
margin-top: 12pt; text-align: center
}
PART { display: block }
ITEM { display: block; font-family: arial; color:
#000080; font-weight: 400; margin-left: 15pt;
margin-top: 12pt; font-size: 18 }
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
88
XML and CSS – Example 2
MANUFACTURER { display: block; font-family:
arial; color: #600060; font-weight: 400; margin-
left: 45pt; margin-top: 5pt; font-size: 18 }
MODEL { display: block; font-family: arial; color:
#006000; font-weight: 400; margin-left: 45pt;
margin-top: 5pt; font-size: 18 }
COST { display: block; font-family: arial; color:
#800000; font-weight: 400; margin-left: 45pt;
margin-top: 5pt; font-size: 18 }
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
89
Misc Exercise - 1
 We want a document type to be able to
describe Lists which contain Items. Write the
DTD for this.
<!ELEMENT List (Item)+> <!ELEMENT Item
(#PCDATA)>
XML file that is validated by this DTD is:
<List>
<Item>Chocolate</Item>
<Item>Coffee</Item>
<Item>Ice-cream</Item>
</List> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
90
XML Examples for Practice - 1
 1) Create a XML page with following structure apply CSS

 OS
 ABC
 5yrs
Data communication and Networking
 PQR
 1yrs
SSAD
 JKL
 10yrs
ACII
 MNO
 10yrs
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
91
XML Examples for Practice - 3
External DTD
Create a XML page with following structure.
<personelDetail>
<name>
<firstName>Sonia</firstName>
<middleName>H</middleName>
<lastName>Shah</lastName>
</name>
<address>
<city>Mumbai</city>
<country>India</country>
</address>
</personelDetail>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
93
XSLT - 1
 XSL is a language for style sheets
 XSl = Extensible Stylesheet Langugae
 An XSL style sheet is a file that describes how to
display an XML document
 XSL contains a transformation language for XML
documents: XSLT. XSLT is used for generating
HTML web pages from XML data.
 XSLT - eXtensible Stylesheet Language
Transformations
 XSLT is used to transform an XML document into an
HTML document
 XSLT is the recommended style sheet language for
XML November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
94
XSLT - 2
 XSL is made up of two parts:
 XSL Transformation (XSLT) – It is an XML based
language that allows you to transform an XML
document into HTML document
 XMLPath (Xpath) – It is an XML based langugae
that is used to access different parts of an XML
document such as elements and attributes
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
95
CSS vs XSLT
CSS XSLT
Simple to use and suitable for small
documents
Complex to use
Cannot reorder, add, delete or
perform operations on elements
Can reorder, add, delete elements
since it is aware of the structure of
the XML document
Does not offer access to PI and
attributes of XML document
Can access and modify the
comments, PI, attribute names and
values within an XML document
Syntax is different from that of XML Syntax is same as that of XML
Uses less memory since it cannot
reorder a document . No need to
create a tree representation of the
document
Uses more memory as it has to
reorder, delete, modify elements.
This requires maintaining a tree
representation of the document in
memory November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
96
How to Transform XML into XHTML
using XSLT Step - 1
 Step 1: Consider the following raw XML document (filename:
BOOKS.XML)
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<book>
<title>Java 2: Black Book</title>
<author>Steve Holzner</author>
<publisher>Dreamtech</publisher>
<year>2006</year>
</book>
<book>
<title>Computer Systems and Applications</title>
<author>Mukesh N Tekwani</author>
<publisher>Sheth Publisher</publisher>
<year>2011</year>
</book>
</catalog>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
97
How to Transform XML into XHTML
using XSLT Step - 2
Step 2: Create an XSL Stylesheet
(filename: BOOKS.XSL)
<?xml version="1.0" encoding="ISO-
8859-1"?>
<xsl:stylesheet version="1.0“
xmlns:xsl="http://www.w3.org/1999/XSL
/Transform">
<xsl:template match="/">
<html><body>
<h2>My Books Collection</h2>
<table border="1"><tr
bgcolor="#9acd32">
<th>Title</th><th>Author</th>
<th>Publisher</th><th>Year</th>
</tr>
<xsl:for-each select="catalog/book">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of
select="publisher"/></td>
<td><xsl:value-of select="year"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
98
How to Transform XML into XHTML
using XSLT Step - 3
 Add the XSL style sheet reference to your XML
document (“BOOKS.XML”)
 <?xml-stylesheet type="text/xsl"
href=“books.xsl"?>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
99
<xsl:template> - Explained
 An XSL style sheet consists of one or more set of
rules that are called templates.
 A template contains rules to apply when a specified
node is matched.
 The <xsl:template> element is used to build
templates.
 The match attribute is used to associate a template
with an XML element. The match attribute can also
be used to define a template for the entire XML
document.
 The value of the match attribute is an XPath
expression (i.e. match="/" defines the whole
document).
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
100
<xsl:template> - Explained
 Since an XSL style sheet is an XML document, it
always begins with the XML declaration: <?xml
version="1.0" encoding="ISO-8859-1"?>.
 The next element, <xsl:stylesheet>, defines that this
document is an XSLT style sheet document (along with
the version number and XSLT namespace attributes).
 The <xsl:template> element defines a template.
The match="/" attribute associates the template with
the root of the XML source document.
 The content inside the <xsl:template> element defines
some HTML to write to the output.
 The last two lines define the end of the template and
the end of the style sheet.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
101
XSL Explained
 We use the <xsl:value-of> element to extract
the value of a selected node
 The select attribute in the example above,
contains an XPath expression. An XPath
expression works like navigating a file system;
a forward slash (/) selects subdirectories.
 We use the <xsl:for-each>element to loop
through the XML elements, and display all the
records.
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
102
XSL Explained
 The XSL <xsl:for-each> element can be used
to select every XML element of a specified
node-set
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
103
Filtering the Output
 We can also filter the output from the XML file by
adding a criterion to the select attribute in the
<xsl:for-each> element.
<xsl:for-each select=“book[author=‘Mukesh N.
Tekwani']">
 Legal filter operators are:
= (equal)
!= (not equal)
&lt; less than
&gt; greater than
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
104
Filtering the Output
In the XSL Style sheet, we make the following
changes:
<xsl:for-each select="catalog/cd[author=‘Mukesh N Tekwani']">
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
105
Sorting the Data
 To sort the output, simply add an <xsl:sort>
element inside the <xsl:for-each> element in
the XSL
 <xsl:for-each select="catalog/book">
<xsl:sort select="author"/>
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
106
THANK
YOU
November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com)
107

XML

  • 1.
    Mukesh N Tekwani Mumbai,India mukeshtekwani@outlook.co m
  • 2.
    Disadvantages of HTML– Need for XML  HTML lacks syntax checking  HTML lacks structure  HTML is not suitable for data interchange  HTML is not context aware – HTML does not allow us to describe the information content or the semantics of the document  HTML is not object-oriented  HTML is not re-usable  HTML is not extensible November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 2
  • 3.
    Introduction to XML XML – Extensible Markup Language  Extensible – capable of being extended  Markup – it is a way of adding information to the text indicating the logical components of a document  How is it different from HTML?  HTML was designed to display data  XML was designed to store, describe and transport data  XML is also a markup language like HTML  XML tags are not predefined – we must design our own tags. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 3
  • 4.
    Differences between HTMLand XML HTML XML 1. Designed to display data 1. Designed to store and transport data between applications and databases. 2. Focus is on how data looks 2. Focus is on what data is 3. It has pre-defined tags such as <B>, <LI>, etc 3. No predefined tags; all tags must be defined by the user. E.g., we can create tags such as <TO>, <FROM>, <BOOKNAME>, etc 4. HTML is used to display information 4. XML is used to describe information 5. Every tag may not have a closing tag. 5. Every tag must have a closing tag. 6. HTML is not case sensitive. 6. XML is case sensitive 7. HTML is for humans 7. XML is for computersNovember 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 4
  • 5.
    Advantages of XML- 1  XML simplifies data sharing  Since XML data is stored in plain text format, data can be easily shared among different hardware and software platforms.  XML separates data from HTML  To display dynamic data in HTML, the code must be rewritten each time the data changes. With XML, data can be stored in separate files so that whenever the data changes it is automatically displayed correctly. We have to design the HTML for layout only once. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 5
  • 6.
    Advantages of XML- 2  XML simplifies data transport  With XML, data can be easily exchanged between different platforms.  XML makes data more available  Since XML is independent of hardware, software and application, XML can make your data more available and useful.  Different applications can access your data in HTML pages  XML provides a means to package almost any type of information (binary, text, voice, video) for delivery to a receiving end. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 6
  • 7.
    Advantages of XML- 3  Internationality  HTML relies heavily on ASCII which makes using foreign characters very difficult. XML uses Unicode so that many European and Asian languages are also handled easily November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 7
  • 8.
    XML Document –Example 1 <?xml version="1.0" encoding="ISO-8859-1"?> <class_list> <student> <name>Anamika</name> <grade>A+</grade> </student> <student> <name>Veena</name> <grade>B+</grade> </student> </class_list> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 9
  • 9.
    XML Document–Example 1- Explained  The first line is the XML declaration.  <?xml version="1.0" encoding="ISO-8859-1"?>  It defines the XML version (1.0)  It gives the encoding used (ISO-8859-1 = Latin-1/West European character set)  The XML declaration is actually a processing instruction (PI) an it is identified by the ? At its start and end  The next line describes the root element of the document (like saying: "this document is a class_list“)  The next 2 lines describe 2 child elements of the root (student, name, and grade)  And finally the last line defines the end of the root element: </class_list> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 10
  • 10.
    Logical Structure  XMLuses its start tags and end tags as containers.  The start tag, the content and the end tag form an element  Elements are the building blocks out of which an XML document is assembled.  An XML document has a tree-like structure with the root element at the top and all the other elements are contained within each other. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 11
  • 11.
    Tree structure  XMLdocuments form a tree structure.  XML documents must contain a root element. This element is "the parent" of all other elements.  The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree.  All elements can have sub elements (child elements)  <root> <child> <subchild>.....</subchild> </child> </root November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 12
  • 12.
    XML – Example2 November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 13
  • 13.
    XML – Example2 <bookstore> <book category = "COOKING"> <title lang = "en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category = "CHILDREN"> <title lang = "en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category = "WEB"> <title lang = "en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 14
  • 14.
    Important Definitions  XMLElement  An element is a start tag, content, and an end tag.  E.g., <greeting>”Hello World</greeting>  XML Attribute  An attribute provides additional information about elements  E.g., <note priority = “high”> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 15
  • 15.
    Important Definitions  Childelements – XML elements may have child elements <employee id = “100”> <name> <first>Anita</first> <initial>D</initial> <last>Singh</last> </name> </employee> Parent Element Name Children of parent element November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 16
  • 16.
    XML Element  AnXML element is everything from the element's start tag to the element's end tag.  An element can contain other elements, simple text or a mixture of both.  Elements can also have attributes. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 17
  • 17.
    XML Syntax Rules All XML elements must have a closing tag  XML tags are case sensitive.  The tag <Book> is different from the tag <book>  Opening and closing tags must be written with the same case <Message>This is incorrect</message> <message>This is correct</message> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 18
  • 18.
    XML Syntax Rules XML elements must be properly nested  HTML permits this: <B><I>This text is bold and italic</B></I> But in XML this is invalid. All elements must be properly nested within one another. <B><I>This text is bold and italic</I></B>  XML documents must have a root element. It is the parent of all other elements. <root> <child> <subchild>.....</subchild> </child> </root> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 19
  • 19.
    XML Syntax Rules XML Entity References  Some characters have a special meaning in XML. E.g., If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.  <message>if salary < 1000 then </message>  To avoid this error, replace the "<" character with an entity reference:  <message>if salary &lt; 1000 then</message> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 20
  • 20.
    XML Syntax Rules XML Entity References  There are 5 predefined entity references in XML: Entity Symbol Description &lt; < Less than &gt; > Greater than &amp; & Ampersand &apos; ‘ Apostrophe &quot; “ Quotation mark November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 21
  • 21.
    XML Syntax Rules Comments in XML (similar to HTML) <!-- This is a comment -->  White space is preserved in XML but not in HTML  XML Naming Rules  Names can contain letters, numbers, and other characters  Names cannot start with a number or punctuation character  Names cannot start with the letters xml (or XML, or Xml, etc)  Names cannot contain spaces  Any name can be used, no words are reserved. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 22
  • 22.
    XML Markup Delimiters Every XML element is made up of the following parts: Symbol Description < Start tag open delimiter </ End tag open delimiter something element name > tag close delimiter /> empty tag close delimiter November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 23
  • 23.
    Different Types ofXML Markups  Element Markup  It is composed of 3 parts: start tag, the content, and the end tag.  Example: <name>Neetu</name>  The start tag and the end tag can be treated as wrappers  The element name that appears in the start tag must be exactly the same as the name that appears in the end tag.  Example: <Name>Neetu</name> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 24
  • 24.
    Different Types ofXML Markups  Attribute Markup  Attributes are used to attach information to the information contained in an element.  General syntax for attributes is:  <elementname property = ‘value’> Or  <elementname property = “value”>  Attribute value must be enclosed within quotation marks  Use either single quotes or double quotes but don’t mix them. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 25
  • 25.
    Attribute Markup  Ifwe specify the attributes for the same element more than once, the specifications are merged. <?xml version = “1.0”?> <myparas> <para num = “first”>This is Para 1 </para> <para num = ‘second’ color = “red”>This is Para 2</para> <myparas> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 26
  • 26.
    Attribute Markup  Whenthe XML processor encounters line 3, it will record the fact that para element has the num attribute  When it encounters the 4th line it will record the fact that para element has the color attribute November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 27
  • 27.
    Reserved Attribute  Thexml:lang attribute is reserved to identify the human language in which the element was written  The value of attribute is one of the following:  en English  fr French  de German November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 28
  • 28.
    XML Attributes  Attributeprovides additional information about the element  Similar to attributes in HTML e.g., <IMG SRC=“sky.jpg”> In this SRC is the attribute  XML Attribute values must be quoted  XML elements can have attributes in name/value pairs just like in HTML.  In XML the attribute value must always be quoted. <note date = 01/01/2010> <---------- This is invalid <to>Priya</to> <from>Deepali</from> </note> <note date = “01/01/2010”> --------- Now OK since enclosed in double quotes <note date = ‘01/01/2010’> --------- This is also OK since enclosed in single quotes November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 29
  • 29.
    XML Attributes andElements  Consider the following example: <person gender = "female"> <firstname>Geeta</firstname> <lastname>Shah</lastname> </person> <person> <gender>female</gender> <firstname>Geeta</firstname> <lastname>Shah</lastname> </person> Gender is an attribute Gender is an element November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 30
  • 30.
    Problems with XMLAttributes  Attributes cannot contain multiple values whereas elements can  Attributes cannot contain tree structures  Attributes are not easily expandable (for future changes)  Attributes are difficult to read and maintain  Use elements for data.  Use attributes for information that is not relevant to the data. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 31
  • 31.
    Illustrating Problematic Attributes  Considerthe following example: <note day=“03" month="02" year="2010" to="Tina" from=“Yasmin" heading="Reminder" body=“Happy Birthday!"> </note>  Better way: <note> <date> <day>03</day> <month>02</month> <year>2010</year> </date> <to>Tina</to> <from>Yasmin</from> <heading>Reminder</heading> <body>Happy Birthday!</body> </note> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 32
  • 32.
    When to useAttributes?  XML Attributes can be used to assign ID references to elements.  Metadata – data about data – should be stored as attributes  The ID can then be used to identify the XML element <messages> <note id="501"> <to>Tina</to> <from>Yasmin</from> <heading>Reminder</heading> <body>Happy Birthday!</body> </note> <note id="502"> <to>Yasmin</to> <from>Tina</from> <heading>Re: Reminder</heading> <body>Thank you, my dear</body> </note> </messages> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 33
  • 33.
    What does Extensiblemean in XML?  Consider the following XML example: <note> <to>Anita</to> <from>Veena</from> <body>You have an exam tomorrow</body> </note> Suppose we create an application that extracted the <to>, <from> and <body> elements from the XML document to produce the result: MESSAGE To: Anita From:Veena You have an exam tomorrow November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 34
  • 34.
    What does Extensiblemean in XML?  Now suppose the author of the XML document added some extra information to it: <note> <date>2008-01-10</date> <to>Anita</to> <from>Veena</from> <heading>Reminder</heading> <body>You have an exam tomorrow</body> </note> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 35
  • 35.
    What does Extensiblemean in XML? This application will not crash because it will still find the <to>, <from> and <body> elements in the XML document and produce the same output. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 36
  • 36.
    XML Validation  Whatis a “well formed” XML document?  XML with correct syntax is "Well Formed" XML.  A "Well Formed" XML document has correct XML syntax.  XML documents must have a root element  XML elements must have a closing tag  XML tags are case sensitive  XML elements must be properly nested  XML attribute values must be quoted November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 37
  • 37.
    Wellformed Document -Rule 1  Elements are case-sensitive.  If you define your language to use lowercase elements, then all instances of those elements must be in lowercase. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 38
  • 38.
    Bad Examples… <H1>Sample Heading</H1> <h1>SampleHeading</H1> <H1>Sample Heading</h1> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 39
  • 39.
    Rule 2:  Allelements that contain text or other elements must have both start and ending tags. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 40
  • 40.
    Rule 3:  Allempty elements (commonly known as standalone tags) must have a slash (/) before the end of the tag. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 41
  • 41.
    Rule 4:  Allattribute values must be contained in quotes, either single or double – no exceptions! November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 42
  • 42.
    Rule 5:  Elementsmay not overlap.  Elements must be nested properly within other elements and can not start before a sub-element and end within the sub-element. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 43
  • 43.
    Rule 6:  Isolatedmarkup characters (characters essential to creating markup documents) may not appear in parsed content as is.  Isolated markup characters must be represented as a character entity and include the following: <, [, ], >, ', " and &. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 44
  • 44.
    Isolated Markup Characters <&lt; [ &#91; ] &#93; > &gt; ' &apos; " &quot; & &amp; November 9, 2019 Mukesh N Tekwani (mukeshtekwani@outlook.com) 45
  • 45.
    Bad Examples… <h1>Jack &ampJill</h1> <equation>5 &lt 2</equation> These examples are invalid since they are both examples forgetting the semi-colon following the character entity. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 46
  • 46.
    Good Examples… <h1>Jack &amp;Jill</h1> <equation>5 &lt; 2</equation> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 47
  • 47.
    Rule 7:  Element(and attribute) names must start with either a letter (uppercase or lowercase) or an underscore.  Element names may contain letters, numbers, hyphens, periods and underscores. BAD EXAMPLES <bad*characters> <illegal space> <99number-start> GOOD EXAMPLES <example-one> <_example2> <Example.Three> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 48
  • 48.
    XML Structure  AnXML document has a number of components that can be used for representing information in a hierarchical order  These components are:  Processing Instruction (PI)  Tags  Elements  Attributes  Entities  Comments  Content November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 49
  • 49.
    Processing Instruction (PI) Every XML document begins with a processing instruction (PI)  The PI is also called as XML declaration  The PI indicates the version of XML  Typical PI statement: <?xml version = “1.0” encoding = “UTI-8” ?>  All XML parsers support the Unicode “UTF-8” and “UTF-16” encodings. UTF-8 is used to create pages written in English.  UTF stands for Universal Character Set Transformation Format  This character set (UTF-8) uses 8 bits of information to represent each character November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 50
  • 50.
    Processing Instruction (PI) The xml declaration is case sensitive. It cannot start with capital XML!  If the XML declaration appears in a XML document, it must be the first statement; even comments and white spaces cannot appear before it November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 51
  • 51.
    Tags  Tags areused to identify data  XML does not have predefined tags  XML tags begin with the < sign and end with the > sign.  All tags must occur in pairs; each pair consists of a start tag and an end tag  E.g., <book> is the start tag and </book> is the end tag November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 52
  • 52.
    Elements  Element isthe basic unit used to identify and describe the XML data  Elements are the basic building blocks of an XML document  Element begins with a start tag such as <book> and ends with an end-tag such as </book>  Some elements may be empty i.e., they have no content. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 53
  • 53.
    Rules to UseElements  Elements must not overlap: a tag opened last must be the first one to be closed (LIFO structure)  E.g., the following is incorrect: <book><author>Mukesh N Tekwani</book></author> It is incorrect because the author tag was the last one to be opened so it must be the first tag to be closed. Correct: <book><author>Mukesh N Tekwani</author></book>  An XML document has exactly one root  XML element names are case sensitive. So <book> and <Book> are tow different elements. So we cannot use something like this:  <book>Introduction to XML <Book> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 54
  • 54.
    Comments  Comments beginwith <!– and end with -->  Comments can contain any data except the literal string -- November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 55
  • 55.
    XML Validation  A“well formed” XML document conforms to the rules of a Document Type Definition (DTD) <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE note SYSTEM "Note.dtd"> <note> <to>Tina</to> <from>Yasmin</from> <heading>Reminder</heading> <body>Happy Birthday!</body> </note> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 56
  • 56.
     DTD standsfor Document Type Definition.  A document type definition provides a list of the elements, attributes, and entities contained in a document, as well as their relationships to one another.  DTDs specify a set of rules for the structure of a document. For example, a DTD may indicate that a BOOK element has only one ISBN child, exactly one TITLE child, and one or more AUTHOR children, and it may or may not contain a single SUBTITLE. XML Document Type Definition November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 58
  • 57.
     A DTDcan identify which elements are valid for a particular document and which attributes are valid to use with each of those elements.  DTDs can be included in the file that contains the document they describe, or they can be linked from an external URL. This is called Internal DTD.  DTD can be stored in a separate file and is called External DTD. External DTDs can be shared by different documents and Web sites.  DTD is used to validate an XML document. XML Document Type Definition November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 59
  • 58.
    DTD Elements  ChildElements  When defining child elements in DTDs, we can specify how many times those elements can appear by adding a modifier after the element name.  If no modifier is added, the element must appear once only  Modifier Description ? Zero or one times. + One or more times. * Zero or more times.  It is not possible to specify a range of times that an element may appear (e.g, 2-4 appearances). November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 60
  • 59.
    DTD Elements andAttributes  Attributes are declared using the <!ATTLIST > declaration. The syntax is shown below.  <!ATTLIST ElementName AttributeName AttributeType State DefaultValue? AttributeName AttributeType State DefaultValue?> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 61
  • 60.
    DTD Elements  Ina DTD elements are declared with an ELEMENT declaration.  Empty Elements Empty elements are declared with the category keyword EMPTY: <!ELEMENT element-name EMPTY> Example: <!ELEMENT br EMPTY> XML example: <br /> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 62
  • 61.
    DTD Elements  Elementswith Parsed Character Data  Elements with parsed character data are declared with #PCDATA inside parentheses: <!ELEMENT element-name (#PCDATA)> Example: <!ELEMENT from (#PCDATA)> PCDATA means any plain text values November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 63
  • 62.
    DTD Elements  Elementswith any Contents Elements declared with the category keyword ANY, can contain any combination of parsable data:  <!ELEMENT element-name ANY> Example: <!ELEMENT note ANY> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 64
  • 63.
    DTD Elements  Elementswith Children (sequences)  Elements with one or more children are declared with the name of the children elements inside parentheses:  <!ELEMENT element-name (child1)> or <!ELEMENT element-name (child1, child2, ...)> Example: <!ELEMENT note (to, from, heading, body)>  When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 65
  • 64.
    External DTD  Ifthe DTD is declared in an external file, it should be enclosed in a DOCTYPE definition with the following syntax: <!DOCTYPE root-element SYSTEM "filename"> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 66
  • 65.
    External DTD  XMLFile: <!DOCTYPE note SYSTEM "note.dtd"> <note> <to>Raja</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body> </note> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 67
  • 66.
    External DTD  DTDfile: <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 68
  • 67.
    PCDATA  PCDATA meansParsed Character Data.  Parsed data means any plain text data  Character data is the text found between the start tag and the end tag of an XML element.  PCDATA is text that WILL be parsed by a parser. The text will be examined by the parser for entities and markup.  Tags inside the text will be treated as markup and entities will be expanded.  But, parsed character data should not contain any &, <, or > characters; these must be represented by the &amp; &lt; and &gt; entities, respectively. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 69
  • 68.
    PCDATA  Consider thedeclaration: <!ELEMENT YEAR (#PCDATA)> This declaration says that a YEAR may contain only parsed character data, i.e. text that is not marked up. It may not contain children of its own.  So the following are all valid expressions: <YEAR>2010</YEAR>. <YEAR>2010 – The Decade of IT</YEAR>  XML will not attempt to validate the contents of PCDATA; it only checks that the text does not contain markup. So the following is invalid: November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 70
  • 69.
    Invalid because <YEAR> cannot have children ofits own – in this case <MONTH> PCDATA  Invalid: (why invalid?) <YEAR> <MONTH>January</MONTH> <MONTH>February</MONTH> <MONTH>March</MONTH> <MONTH>April</MONTH> <MONTH>May</MONTH> <MONTH>June</MONTH> <MONTH>July</MONTH> <MONTH>August</MONTH> <MONTH>September</MONTH> <MONTH>October</MONTH> <MONTH>November</MONTH> <MONTH>December</MONTH> </YEAR> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 71
  • 70.
    CDATA  CDATA meanscharacter data.  CDATA is text that will NOT be parsed by a parser.  Tags inside the text will NOT be treated as markup and entities will not be expanded.  A CDATA section to indicate that a block of text is to be presented as is with no translation. CDATA sections begin with <![CDATA[ and end with ]]>. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 72
  • 71.
    CDATA  Characters like"<" and "&" are illegal in XML elements.  "<" will generate an error because the parser interprets it as the start of a new element.  "&" will generate an error because the parser interprets it as the start of an character entity.  To avoid these errors, code can be defined as CDATA.  Everything inside a CDATA section is ignored by the parser. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 73
  • 72.
    CDATA  For example: <![CDATA[ <?xmlversion=”1.0”?> <GREETING> Hello XML! </GREETING> ]]> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 74
  • 73.
    Working Example 1 Consider the following XML file: <?xml version="1.0" ?> <videos> <music> <title>Video Title 1 </title> <artist>Artist 1 </ artist > </music> < music > <title>Video Title 2 </title> < artist > Artist 2 </ artist > < artist > Artist 3 </ artist > </ music > </ videos > November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 75
  • 74.
    Working Example 1- contd  The above XML file has data related to music videos.  In this XML file the root node is <videos> and this can have any number of <music> elements. The <music> element can have a single <title> and any number of <artist> child nodes. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 76
  • 75.
    Working Example 1- contd  The DTD for this XML is : <!ELEMENT videos ( music+ ) > <!ELEMENT music ( title, artist+ ) > <!ELEMENT title ( #PCDATA ) > <!ELEMENT artist ( #PCDATA ) > ELEMENT indicates that the word next to that is an element and the nodes of that particular element are given in the brackets. The + sign after the node in the bracket denotes that it could be any number of nodes. The #PCDATA indicates that it is a text node that has some value in it. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 77
  • 76.
    Viewing XML Files- 1 November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 78
  • 77.
    Viewing XML Files- 2  The XML document will be displayed with color- coded root and child elements.  A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure.  To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 79
  • 78.
    Viewing XML Files- 3  Why XML documents display like this?  XML documents do not carry information about how to display the data.  Since XML tags are created by the user of the XML document, browsers do not know if a tag like <table> describes an HTML table or a dining table.  Without any information about how to display the data, most browsers will just display the XML document as it is. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 80
  • 79.
    Using CSS todisplay XML Files  CSS (Cascading Style Sheets) can be used to format a XML document.  Consider this XML document: November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 81
  • 80.
    Displaying Formatted XML document-1 <?xmlversion="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type = "text/css" href = "birthdate.css"?> <birthdate> <person> <name> <first>Anokhi</first> <last>Parikh</last> </name> <date> <month>01</month> <day>21</day> <year>1992</year> </date> </person> </birthdate> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 82
  • 81.
    Displaying Formatted XML document-2 birthdate { background-color:#ffffff; width: 100%; } person { margin-left: 0; } name { color: #FF0000; font-size: 20pt; } month, day, year { display:block; color: #000000; margin-left: 20pt; } Stylesheet – birthdate.css November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 83
  • 82.
    Final Output November 9,2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 84
  • 83.
    XML and CSS- Example 2  Filename: PARTS.XML <?xml version="1.0"?> <!DOCTYPE PARTS SYSTEM "parts.dtd"> <?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?> <PARTS> <TITLE>Computer Parts</TITLE> <PART> <ITEM>Motherboard</ITEM> <MANUFACTURER>ASUS</MANUFACTURER> <MODEL>P3B-F</MODEL> <COST> 123.00</COST> </PART> </PARTS> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 85
  • 84.
    XML and CSS- Example 2  DTD File "PARTS.DTD".  <!ELEMENT PARTS (TITLE?, PART*)>  <!ELEMENT TITLE (#PCDATA)>  <!ELEMENT PART (ITEM, MANUFACTURER, MODEL, COST)+>  <!ELEMENT ITEM (#PCDATA)>  <!ELEMENT MANUFACTURER (#PCDATA)>  <!ELEMENT MODEL (#PCDATA)>  <!ELEMENT COST (#PCDATA)> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 86
  • 85.
    XML and CSS– Example 2  The root element is PARTS.  The root element may contain no TITLE element or one TITLE element along with any number of PART elements.  The PART element must contain one each of items ITEM, MANUFACTURER, MODEL, and COST in order.  The elements TITLE, ITEM, MANUFACTURER, MODEL, and COST all contain PCDATA which is parsed character data. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 87
  • 86.
    XML and CSS– Example 2 PARTS { display: block } TITLE { display: block; font-family: arial; color: #008000; font-weight: 600; font-size: 22; margin-top: 12pt; text-align: center } PART { display: block } ITEM { display: block; font-family: arial; color: #000080; font-weight: 400; margin-left: 15pt; margin-top: 12pt; font-size: 18 } November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 88
  • 87.
    XML and CSS– Example 2 MANUFACTURER { display: block; font-family: arial; color: #600060; font-weight: 400; margin- left: 45pt; margin-top: 5pt; font-size: 18 } MODEL { display: block; font-family: arial; color: #006000; font-weight: 400; margin-left: 45pt; margin-top: 5pt; font-size: 18 } COST { display: block; font-family: arial; color: #800000; font-weight: 400; margin-left: 45pt; margin-top: 5pt; font-size: 18 } November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 89
  • 88.
    Misc Exercise -1  We want a document type to be able to describe Lists which contain Items. Write the DTD for this. <!ELEMENT List (Item)+> <!ELEMENT Item (#PCDATA)> XML file that is validated by this DTD is: <List> <Item>Chocolate</Item> <Item>Coffee</Item> <Item>Ice-cream</Item> </List> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 90
  • 89.
    XML Examples forPractice - 1  1) Create a XML page with following structure apply CSS   OS  ABC  5yrs Data communication and Networking  PQR  1yrs SSAD  JKL  10yrs ACII  MNO  10yrs November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 91
  • 90.
    XML Examples forPractice - 3 External DTD Create a XML page with following structure. <personelDetail> <name> <firstName>Sonia</firstName> <middleName>H</middleName> <lastName>Shah</lastName> </name> <address> <city>Mumbai</city> <country>India</country> </address> </personelDetail> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 93
  • 91.
    XSLT - 1 XSL is a language for style sheets  XSl = Extensible Stylesheet Langugae  An XSL style sheet is a file that describes how to display an XML document  XSL contains a transformation language for XML documents: XSLT. XSLT is used for generating HTML web pages from XML data.  XSLT - eXtensible Stylesheet Language Transformations  XSLT is used to transform an XML document into an HTML document  XSLT is the recommended style sheet language for XML November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 94
  • 92.
    XSLT - 2 XSL is made up of two parts:  XSL Transformation (XSLT) – It is an XML based language that allows you to transform an XML document into HTML document  XMLPath (Xpath) – It is an XML based langugae that is used to access different parts of an XML document such as elements and attributes November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 95
  • 93.
    CSS vs XSLT CSSXSLT Simple to use and suitable for small documents Complex to use Cannot reorder, add, delete or perform operations on elements Can reorder, add, delete elements since it is aware of the structure of the XML document Does not offer access to PI and attributes of XML document Can access and modify the comments, PI, attribute names and values within an XML document Syntax is different from that of XML Syntax is same as that of XML Uses less memory since it cannot reorder a document . No need to create a tree representation of the document Uses more memory as it has to reorder, delete, modify elements. This requires maintaining a tree representation of the document in memory November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 96
  • 94.
    How to TransformXML into XHTML using XSLT Step - 1  Step 1: Consider the following raw XML document (filename: BOOKS.XML) <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <book> <title>Java 2: Black Book</title> <author>Steve Holzner</author> <publisher>Dreamtech</publisher> <year>2006</year> </book> <book> <title>Computer Systems and Applications</title> <author>Mukesh N Tekwani</author> <publisher>Sheth Publisher</publisher> <year>2011</year> </book> </catalog> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 97
  • 95.
    How to TransformXML into XHTML using XSLT Step - 2 Step 2: Create an XSL Stylesheet (filename: BOOKS.XSL) <?xml version="1.0" encoding="ISO- 8859-1"?> <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL /Transform"> <xsl:template match="/"> <html><body> <h2>My Books Collection</h2> <table border="1"><tr bgcolor="#9acd32"> <th>Title</th><th>Author</th> <th>Publisher</th><th>Year</th> </tr> <xsl:for-each select="catalog/book"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="author"/></td> <td><xsl:value-of select="publisher"/></td> <td><xsl:value-of select="year"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 98
  • 96.
    How to TransformXML into XHTML using XSLT Step - 3  Add the XSL style sheet reference to your XML document (“BOOKS.XML”)  <?xml-stylesheet type="text/xsl" href=“books.xsl"?> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 99
  • 97.
    <xsl:template> - Explained An XSL style sheet consists of one or more set of rules that are called templates.  A template contains rules to apply when a specified node is matched.  The <xsl:template> element is used to build templates.  The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document.  The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document). November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 100
  • 98.
    <xsl:template> - Explained Since an XSL style sheet is an XML document, it always begins with the XML declaration: <?xml version="1.0" encoding="ISO-8859-1"?>.  The next element, <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).  The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document.  The content inside the <xsl:template> element defines some HTML to write to the output.  The last two lines define the end of the template and the end of the style sheet. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 101
  • 99.
    XSL Explained  Weuse the <xsl:value-of> element to extract the value of a selected node  The select attribute in the example above, contains an XPath expression. An XPath expression works like navigating a file system; a forward slash (/) selects subdirectories.  We use the <xsl:for-each>element to loop through the XML elements, and display all the records. November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 102
  • 100.
    XSL Explained  TheXSL <xsl:for-each> element can be used to select every XML element of a specified node-set November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 103
  • 101.
    Filtering the Output We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element. <xsl:for-each select=“book[author=‘Mukesh N. Tekwani']">  Legal filter operators are: = (equal) != (not equal) &lt; less than &gt; greater than November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 104
  • 102.
    Filtering the Output Inthe XSL Style sheet, we make the following changes: <xsl:for-each select="catalog/cd[author=‘Mukesh N Tekwani']"> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 105
  • 103.
    Sorting the Data To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL  <xsl:for-each select="catalog/book"> <xsl:sort select="author"/> November 9, 2019Mukesh N Tekwani (mukeshtekwani@outlook.com) 106
  • 104.
    THANK YOU November 9, 2019MukeshN Tekwani (mukeshtekwani@outlook.com) 107