Extensible Markup Language
Extensible Markup Language
1
Diff b/w HTML & XML
HTML XML
1. designed to display data, 1. describe data
4. exchange data
3
1. It describes the data
• In HTML, the markup <b>415-555-1234</b> is quite
meaningless.
– what is this number?
• Is it a product number, a phone number, or an employee
number?
• Markup like
<phone_number>415-555-1234<phone_number>
is much more helpful. This is exactly what XML was designed
for.
4
2. It can store the data
5
3. It can be used to exchange data
6
Goal: Strengthen Partnership by
exchanging the data
Partner 1 Partner 2
DTD
information information
XML XML
7
4. It can carry the data
• Other clients and applications can access your
XML files as data sources, like they are
accessing databases.
8
XML Can be Used to Create New
Languages.
9
EX:- ( MathML )
<?xml version=“1.0”>
<math>
<mi>F</mi>
<mo>=</mo>
<mi>G</mi>
<mfrac>
<mnum>
<mi>M</mi>
<mi>m</mi>
</mnum>
<apply>
<power>
<mi>r</mi>
<mn>2</mn>
</power>
</apply>
</mfrac>
</math>
10
• One application might use this input to display the equation.
11
Ex:-2
• The Wireless Markup Language (WML), used
to markup Internet applications for handheld
devices like mobile phones, is written in XML.
12
XML Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Fred</to>
<from>James</from>
<heading>Reminder</heading>
<body>Do send me the documents this weekend!</body>
</note>
Output
13
XML Building Blocks
a. Processing Internal
a. Attributes Instructions
Prolog
b. Document External
b. Element Type Definition
Naming (DTD)
Internal DTD
External DTD
Elements
c. Comments
1. Elements
Delimited by angle brackets
General format: <element> … </element>
Empty element: </empty-Element>
Ex. </ingredient>
15
Conditions for using Elements
Condition Example
1. All XML elements must have a closing tag <br>This is a paragraph (valid in HTML)
•
<phone>This is a paragraph</phone> (in XML)
2. XML tags are case sensitive With XML, the tag <Letter> is different from the
Opening and closing tags must therefore be tag <letter>.
written with the same case: <Message>This is incorrect</message>
<message>This is correct</message>
3. All XML elements must be properly nested <b><i>This text is bold and italic</i></b>
Improper nesting of tags makes no sense to
XML.
4. All XML documents must have a root tag All other elements must be nested within the root
The first tag in an XML document is the root element.
tag. <root>
All elements can have sub elements <child>
(children). <subchild>.....</subchild>
</child>
</root>
16
Condition Example
5. XML Elements are extensible and they <note>
have relationships. <date>1999-08-01</date>
XML documents can be extended to carry <to>Tove</to>
more information. <from>Jani</from>
<heading>Reminder</heading>
6. XML Elements have simple naming rules. Book Title: My First XML
Elements are related as parents and Chapter 1: Introduction to XML
children. What is HTML
What is XML
<book>
<title>My First XML</title>
<chapter_no>Chapter 1</chapter_no>
<chapter>Introduction to XML
<para>What is HTML</para>
<para>What is XML</para>
</chapter>
</book>
17
1.A Element Attributes
Name-value pairs that occur inside start-tags after element name, like: <element attribute=“value”>
18
1.b. Element Naming
XML elements must follow these naming rules:
a. Names can contain letters, numbers, and other characters
b. Names must not start with a number or other punctuation
characters
c. Names must not start with the letters xml (or XML or Xml ..)
d. Names cannot contain spaces
19
2. Control
Information
2. Control Information
a. Processing
Instructions
• Prolog: The part of an XML document Prolog
b. Document Type
that precedes the XML data Definition (DTD)
Includes
c. Comments
a. Processing Instructions :
XML declaration for version [, encoding, standalone]
21
Standalone :- tells the XML processor whether
they are any other files to load.
Syntax :-
Examples
22
2.B Document Type Definition ( DTD)
DTD defines the grammatical rules for the document.
• DTD either held in a separate file so that it can be used by many
documents, or placed in the same file.
Ex. <!DOCTYPE Recipes SYSTEM “recipe.dtd”>
23
Contents of ( DTD)
24
Ex:- ( External DTD )
25
Internal DTD
Ex:- ( Internal DTD )
<!DOCTYPE authorslist
[
DTD definition
entity-definitions
]
>
26
<?xml version=“1.0” encoding=“US-ASCII” standalone=“no” ?>
<!DOCTYPE authorslist SYSTEM “authors.dtd” >
<authorslist>
<name>
<firstname>chris</firstname>
<lastname>bates</lastname>
<book>web programming</book>
<year>1999</year>
</name>
<name>
<firstname>subramanyam</firstname>
<lastname>allamaraju</lastname>
<book>java server programming</book>
<year>2000</year>
</name>
</authorslist>
27
authors.dtd file
<!ELEMENT lastname(#PCDATA)>
<!ELEMENT book(#PCDATA)>
<!ELEMENT year(#PCDATA)>
28
Operators used for element names
1. +: one/more occurrences
2. *: any number of occurrences
3. ?: 0/1 occurrence
4. #PCDATA: parsed character data, may contain XML markup
5. CDATA : plain text character data, not passed thru’ parser
engine
EX. Partial dtd for cookbook
30
An XML Example
31
Representating in Xml
<person gender=“male” >
<name>
<first>Charles</first>
<last>Myers</last>
<nickname>C.R.</nickname>
</name>
<occupation> college professor </occupation>
</person>
32
An Inline DTD example:
<?xml version="1.0"?>
<!DOCTYPE person [
<!ELEMENT person (name, occupation) >
<!ELEMENT name (first, last, nickname?)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ELEMENT nickname (#PCDATA)>
<!ELEMENT occupation (#PCDATA)>
<!ATTLIST person gender (male | female) #REQUIRED>
]>
<person gender=“male”>
<name>
<first>Charles</first>
<last>Myers</last>
<nickname>C.R.</nickname>
</name>
<occupation>college professor</occupation>
</person>
33
An External DTD example:
<?xml version="1.0"?>
<!DOCTYPE person SYSTEM “persons.dtd">
<person gender=“male”>
<name>
<first>Charles</first>
<last>Myers</last>
<nickname>C.R.</nickname>
</name>
<occupation>college professor</occupation>
</person>
34
persons.dtd
35
Element declarations
Syntax :-
<!ELEMENT name content-model >
36
Five different kinds of content models
(elements)
1. Empty elements
Declares an empty element—It uses the keyword EMPTY
<!ELEMENT name EMPTY>
37
4.Elements containing only elements
expressed using a formula in special notation.
38
39
Grouping Elements
40
Defining attributes
Syntax :-
<!ATTLIST element_name
attr1_name att_type default_value
attr1_name att_type default_value
...........
>
41
Attribute type categories
CDATA
ID
Enumeration
IDREF
IDREFS
42
Attribute default values
43
Inline DTD for Newspaper
1. <!DOCTYPE NEWSPAPER [
Ex:-
45
Tokenized
Types
ID ID
– Used to establish a unique
IDREF
identifier for an element.
Ex:- IDREFS
46
Tokenized
Types
IDREF
IDREF
<!ATTLIST title ref IDREF #IMPLIED>
IDREFS
47
Tokenized
Types
IDREFS
ID
Ex:-
IDREFS
48
Enumerated
Types
<!ATTLIST schedule
day ( mon | tue | wed | thu | fri | sat | sun ) “sun” >
49
3. Document Entities
Some markup elements can contain complex data.
These elements are called Entities.
51
Types of Entity
1. Internal Entity
• Included in XML file
• Used to create small pieces of data;
• This can be used repeatedly through the schema.
• Ex: <!ENTITY POS “pinch of salt”>
• Instruction: <item> Finally add the &POS;</item>
2. External Entity
• Included in the different file and referred in the xml file.
• Ex: <!ENTITY myimage SYSTEM “winter.jpg” NDATA
JPEG>
• NDATA: notation for the type of data.
52
Namespaces
• The flexibility that XML brings can also be the cause for
conflicts when combining documents.
• Ex:
one document may use element named record to store data
about vehicle registration, another may use record to
represent employee details.
53
Name Conflicts
• In XML, element names are defined by the developer. This often results in
a conflict when trying to mix XML documents from different XML
applications.
Both contain a <table> element, but the elements have different content
and meaning. 54
Solving the Name Conflict
Using a Prefix
• Name conflicts in XML can easily be avoided using a name prefix.
1. This XML carries information about an HTML table, and a piece of furniture:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements
have different names.
55
XML Namespaces –
The xmlns Attribute
1. When using prefixes in XML, a so-called namespace for the prefix
must be defined.
xmlns:prefix="URI".
56
• <root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
• In the example above, the xmlns attribute in the <table> tag give the
h: and f: prefixes a qualified namespace.
• When a namespace is defined for an element, all child elements with
the same prefix are associated with the same namespace.
57
Namespaces can be declared in the elements where
they are used or in the XML root element:
• <root
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
58
XML Namespace
59
Syntax :-
60
Example of a document combining two
namespaces , myns and eq
<?xml version=“1.0”?>
<myns: journal xmlns:myns= “http://www.psycholabs.org/mynamespace/”>
<myns:experiment>
<myns:date>March 4,2001</myns:date>
<myns:abstract>xxxxxxxxxx</myns:abstract>
61
XML Validation
• "Well Formed" XML document
--correct XML syntax
• "Valid" XML document
– “well formed”
– Conforms to the rules of a DTD (Document Type Definition)
• XML DTD
– defines the legal building blocks of an XML document
– Can be inline in XML or as an external reference
• XML Schema
– an XML based alternative to DTD, more powerful
– Support namespace and data types
62
XML Schema
Limitations of DTD
63
3. fixed set of content models :-
Schemas provide
User –Defined data types, which allow greater flexibility in
limiting and expressing content.
64
XML Schema
65
XML Schema
An XML Schema defines:
1. elements that can appear in a document
66
There are two element types
1. Simple
2. Complex
– A simple type is one that does not contain attributes and other
elements in the content.
– Otherwise, that element is called complex type.
Syntax:-
Simple type :-
<namespace_reference :element name=“element_name”
type=“namespace_reference:datatype” />
67
Complex Type
<namespace_reference :element name=“name”>
<namespace_reference:complexType >
<namespace_reference:attribute name=“name”
type=“namespace_reference:datatype” />
</namespace_reference:complexType>
</namespace_reference :element>
68
A Simple XML Document
Ex-1
<?xml version="1.0"?>
<note>
<to> Tony </to>
<from> Jani </from>
<heading> Reminder </heading>
<body> Don't forget me this weekend! </body>
</note>
69
A DTD File
70
An XML Schema
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
71
EX-2
<?xml version="1.0"?>
<!DOCTYPE person SYSTEM “persons.dtd">
<person gender=“male”>
<name>
<first>Charles</first>
<last>Myers</last>
<nickname>C.R.</nickname>
</name>
</person>
72
An XML Schema
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name=“person">
<xs:complexType>
<xs:attribute name=“gender” type=“xs:string”/>
<xs:sequence>
<xs:element name=“first" type="xs:string"/>
<xs:element name=“last" type="xs:string"/>
<xs:element name=“nickname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
73
Diff b/w DTD & XSD
1. DTD supports types ID,IDREF,CDATA etc.,
2. Schema supports all primitive and user defined
data types
3. Schema hava minOccurs and maxOccurs
attributes
4. XML Schemas are extensible to future additions
5. XML Schemas are richer and more powerful than
DTDs
6. XML Schemas are written in XML
74
XML Parsers
• The parser is the engine for interpreting our XML
documents
• The parser reads the XML and prepares the information
for your application.
How to use a parser
• 1. Create a parser object
• 2. Pass your XML document to the parser
• 3. Process the results
75
Types of Parsers
76
Document Object Model (DOM)
• DOM uses a tree based structure.
• DOM reads an entire XML document and builds a
Document Object.
– The Document object contains the tree structure.
– The top of the tree is the root node.
– The tree grows down from this root, defining the child
elements.
– DOM is a W3C standard.
• Using DOM, we can also perform insert nodes,
update nodes, and deleting nodes.
77
XML example to construct DOM tree
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
1 <book category="cooking">
Books.xml file
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
4 <book category="web" cover="paperback">
</book>
<title lang="en">Learning XML</title>
2 <book category="children">
…… </book> <author>Erik T. Ray</author>
<year>2003</year>
3
<book category="web"> <price>39.95</price>
….. </book> </book>
</bookstore>
78
XML DOM Tree Example
XML DOM views an XML document as a node-tree.
All the nodes in the tree have a relationship to each other.
79
Look at the following XML fragment:
<bookstore>
<book category="cooking">
<title lang="en">Everyday
Italian</title>
<author>Giada De
Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
80
XML DOM Parsing
1. Most browsers have a build-in XML parser to read and manipulate XML.
Parsing XML
1. XML DOM contains methods (functions) to traverse XML trees, access, insert, and
delete nodes.
2. Before an XML document can be accessed and manipulated, it must be loaded into
an XML DOM object.
3. parser reads XML into memory and converts it into an XML DOM object that can be
accessed with JavaScript.
81
Load an XML Document
The following JavaScript fragment loads an XML document ("books.xml"):
var xmlDoc;
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(“books.xml”);
var allbooks=xmlDoc.getElementsByTagName(“book”);
var book=allbooks[0].childNodes[0].nodeValue;
82
DOM methods & Properties
1. Document.getDocumentElement()
– Returns the root element of the document
2. Node.firstChild() and Node.lastChild()
– Returns the first or last child of a given Node
3. Node.nextSibling() and Node.previousSibling()
– Returns the next or previous sibling of a given Node.
4. Node.childNodes()
– Returns the child nodes of given Node
5. Node.getNodeName
– Returns tagname of a Node
6. Node.getNodeTypedValue
– Returns content of a Node
83
Example for XML and JavaScript userinformation.xml
1. <script language="javascript"> contain:
2. function validateFromXml()
userinfo
3. {
userID (must be
4. var xmlDoc;
unique value)
5. xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
username
6. xmlDoc.async=false;
Address
7. xmlDoc.load("userinformation.xml");
H.no.
8. var allusers=xmlDoc.getElementsByTagName("username");
9. var uname=allusers[0].childNodes[0].nodeValue;
Lane no,
10. document.writeln("name is "+uname);
Street no.
11. return; Area
12. } city
13. </script> State
14. </HEAD> Pincode
Phone number
15. <BODY onLoad="validateFromXml()"> Residence
16. </BODY> Mobile
17. </HTML> userinfo
.
.
.
. 84
XML DOM Manipulations
Operation Example
Get the Value of an Element x=xmlDoc.getElementsByTagName("title")
[0].childNodes[0];
txt=x.nodeValue;
85
Operation Example
Create an Element newel=xmlDoc.createElement("edition");
createElement():creates a new element newtext=xmlDoc.createTextNode("First");
node. newel.appendChild(newtext);
createTextNode(): creates a new text x=xmlDoc.getElementsByTagName("book");
node. x[0].appendChild(newel);
appendChild(): adds a child node to a Example explained:
node (after the last child). Create an <edition> element
Create a text node with value = "First"
To create a new element with text Append the text node to the <edition> element
content, it is necessary to create both Append the <edition> element to the first
an element node and a text node. <book> element
86
SAX
• SAX parsers are event-driven
87
SAX events
The SAX API defines a number of events
• startDocument
– Signals the start of the document.
• endDocument
– Signals the end of the document
• startElement
– Signals the start of an element. The parser fires this event
when all of the contents of the opening tag have been
processed. That includes the name of the tag and any
attributes it might have.
• endElement
– Signals the end of an element.
88
Diff b/w DOM & SAX
DOM SAX
• Uses more memory and has • Uses less memory and provides
more functionality less functionality
• The entire file is stored in an • The developer must handle each
internal Document object. This SAX event before the next
may consume many resources event is fired.
89
Displaying XML
90
Displaying by using CSS
91
display
Value Description
92
Employee.html Style.css
<?xml version="1.0"?>
<?xml-stylesheet type="text/css“
href="style.css"?> first {
position:absolute;
left:300;
<person gender=“male”>
color:blue;
<name> }
<first>Charles</first> last {
<last>Myers</last> display:block;
<nickname>C.R.</nickname> color:red;
</name> }
</person>
93
Output
94
XSL
• XSL stands for eXtensible Stylesheet Language.
• CSS = HTML Style Sheets
• XSL = XML Style Sheets
– More Than a Style Sheet Language
– XSL consists of three parts:
• XSLT - a language for transforming XML documents
• XPath - a language for navigating in XML documents
• XSL-FO - a language for formatting XML documents .
95
XSLT
• XSLT stands for Extensible Stylesheet Language
Transformations.
97
Quick Overview of XSL
XSL
Procedural Rule-Based
programming programming types
Style style
98
Procedural programming style
• Procedural operations
– Control structures : XSLT instructions such as
xsl:for-each, xsl:if and xsl:choose etc.
provide control structures like conditional
execution or loops.
– Accessing content : The xsl:value-of instruction
writes the content of the current node to the
target document.
• ex :-
99
Myxsl.xsl file xml file
<?xml version="1.0"?>
<HTML xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<?xml version="1.0"?>
xsl:version="1.0">
<?xml:stylesheet type="text/xsl“
<BODY> href="myxsl.xsl"?>
<H1>Book Order</H1> <bookOrder>
<TABLE border="1" cellpadding="6"> <shipTo country="US">
<TR> <name>Venus </name>
<TH>Title</TH> <street>Chapel Street</street>
<TH>ISBN</TH> <city>papermoon</city>
<TH>Price</TH> </shipTo>
</TR>
<xsl:for-each select="bookOrder/orderlist/item"> <billTO country="UK">
<name>Rick </name>
<TR>
<street> Marine </street>
<TD><xsl:value-of select="title"/></TD> </billTO>
<TD><xsl:value-of select="@ISBN"/></TD>
<TD><xsl:value-of select="price/@currency"/> <note>Special Valentine wrapping!</note>
<xsl:value-of select="price"/> <orderlist>
</TD> <item ISBN="5855-9">
</TR> <title>The mint lawn</title>
<quantity>1</quantity>
</xsl:for-each> <price currency=“$">19</price>
<note>On stock</note>
</TABLE> </item>
</BODY> </orderlist>
</HTML> </bookOrder>
100
Changes made in xsl file
<BODY bgcolor="cyan">
<H1>Book Order</H1>
<TABLE border="1" cellpadding="6" cellspacing="10">
<TR>
<TH>Title</TH>
<TH>ISBN</TH>
<TH>Price</TH>
</TR>
<xsl:for-each select="bookOrder/orderlist/item">
<TR>
<TD><H2><xsl:value-of select="title"/></H2></TD>
<TD><xsl:value-of select="@ISBN"/></TD>
<TD><xsl:value-of select="price/@currency"/>
<xsl:value-of select="price"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
101
Number of items are increased in xml file
<note>Special Valentine wrapping!</note>
<orderlist>
<item ISBN="5855-9">
<title>The mint lawn</title>
<quantity>1</quantity>
<price currency="$">19</price>
<note>On stock</note>
</item>
<item ISBN="5855-10">
<title>Web TEchnologies</title>
<quantity>2</quantity>
<price currency="$">45</price>
<note>On stock</note>
</item>
</orderlist>
</bookOrder>
102
Rule based programming style
– Consists of set of templates.
– xsl:template -defines a set of rules for transforming
elements .
– The match attribute is used to associate a template
with an XML element.
Syntax:-
<xsl:template match=element_name >
Rules for transforming element
</xsl:template>
– match="/" defines the template for root element.
103
• xsl:apply-templates –applies all the
templates defined in the document recursively.
104
<xsl:stylesheet version="1.0“
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body> <?xml version="1.0"
<h2>My CD Collection</h2> encoding="ISO-8859-1"?>
<xsl:apply-templates /> <?xml:stylesheet type="text/xsl"
</body>
</html> href="mycd.xsl"?>
</xsl:template> <catalog>
<xsl:template match="cd">
<p> <xsl:apply-templates select="title"/> <cd>
<xsl:apply-templates select="artist"/> <title>Empire Burlesque</title>
</p> <artist>Bob Dylan</artist>
</xsl:template>
<country>USA</country>
<xsl:template match="title"> <company>Columbia</company>
Title: <span style="color:#ff0000"> <price>10.90</price>
<xsl:value-of select="."/></span>
<br />
<year>1985</year>
</xsl:template> </cd>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br /> </catalog>
</xsl:template>
105
Output
106
XML Advantages/Goals
• 1. XML is used to Exchange Data
– With XML, data can be exchanged between
incompatible systems
• 2. XML and B2B
– With XML, financial information can be
exchanged over the Internet.
• 3. XML can be used to Share Data
– With XML, plain text files can be used to share
data.
107
• 4. XML is free and extensible
– XML tags are not predefined. we must "invent" your
own tags.
• 5. XML can be used to Store Data
– With XML, plain text files can be used to store data.
• 6. XML can be used to Create new Languages
– XML is the mother of WAP and WML.
• 7. HTML focuses on "look and feel”
– XML focuses on the structure of the data.
108
END
109