Extensible Markup Language
Extensible Markup Language
eXtensible Markup
Language
Introduction
XML stands for EXtensible Markup
Language
XML is text based markup language.
XML is a markup language much like HTML
XML was designed to carry data, not to
display data
XML tags are not predefined. You must
define your own tags
XML is designed to be self-descriptive
XML is a W3C Recommendation
An XML document resides in its own file
with an .xml extension.
goals:
XML was designed to transport and store
In
XML, elements that have a single tag without a matching end tag
must end in a /, as in <img src="coffeecup.png"/>.
That way, the parser knows not to look for a </img> tag.
For example,
<input type="radio" name="language" value="Java" checked>.
In XML, all attributes must have values,
such as checked="true" or (ugh) checked="checked".
handheld devices
RSS languages for news feeds
XMLSyntax Rules
All XML Elements Must Have a Closing Tag
XML Tags are Case Sensitive
XML Elements Must be Properly Nested
XML Documents Must Have a Root Element
XML Attribute Values Must be Quoted
Comments in XML:
Identify Root element, child element, sub child elements and attributes in
following XML file. Also draw the tree structure for the same.
<bookstore>
<bookcategory="COOKING">
<titlelang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<bookcategory="CHILDREN">
<titlelang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<bookcategory="WEB">
<titlelang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
XML - Documents
The following image depicts the parts of XML
document.
<?xml version="1.0"?> Document Prolog Section
<contact-info>
<name>Tanmay Patil</name>
<company>ABC Corp</company>
Document
<phone>(011) 123-4567</phone>
Element
</contact-info>
Document Prolog Section: Thedocument
prologcomes at the top of the document,
XML - Elements
XML elements can be defined as building blocks of an XML. Elements
XML - Attributes
Attributes are part of the XML elements. An element can
</garden>
Attributes are used to distinguish among elements of the
same name. When you do not want to create a new element
for every situation. Hence, use of an attribute can add a little
more detail in differentiating two or more similar elements.
Excercise
Identify errors in following XML syntax.
1. <p>This is a paragraph.</p>
<br>
2. <Message>This is incorrect</message>
3. <b><i>This text is bold and italic</b></i>
4. <notedate=12/11/2007>
<to>Tove</to>
<from>Jani</from>
</note>
5. <?xml version="1.0"?>
<contact-info>
<name>Tanmay Patil</name>
<company>ABC Corp</company>
<phone>(011) 123-4567</phone>
</contact-info>
<contact-info>
<name>Anuradha Patil</name>
<company>XYZ Corp</company>
<phone>(011) 123-4567</phone>
</contact-info>
<note>
<to>Tove</to>
<from>Jani</Ffrom>
<heading>Reminder</heading>
<body>Don't forget me this weekend!
</body>
</note>
"
quotation
mark
Excercise
Design an XML document to store information
Document Type
Definitions (DTD)
A Document Type Definition (DTD) defines
Elements
Attributes
Entities
PCDATA
CDATA
Elements
Elements are themain building blocksof both XML
Attributes
Attributes provideextra information
about elements.
Attributes are always placed inside the
opening tag of an element. Attributes
always come in name/value pairs.
The following "img" element has additional
information about a source file:
<img src="computer.gif" />
Entities
Some characters have a special meaning in
PCDATA
PCDATA means parsed character data.
Think of character data as the text found between
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.
Declaring Elements
In a DTD, elements are declared with an
ELEMENT declaration.
Empty Elements
Empty elements are declared with the
A DTD can be declared inline inside an XML
document, or as an external reference.
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT to (a)>
<!ELEMENT a (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove
<a>eeeeee</a>
</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Hello</body>
</note>
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Hello</body>
</note>
note.dtd
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
Example:
<!ELEMENTnote (message)>
The example above declares that the child element "message" must
occur once, and only once inside the "note" element.
Declaring Minimum One Occurrence of an Element
<!ELEMENTelement-name (child-name+)>
Example:
<!ELEMENTnote (message+)>
The + sign in the example above declares that the child element
"message" must occur one or more times inside the "note" element.
Declaring Zero or More Occurrences of an Element
<!ELEMENTelement-name (child-name*)>
Example:
<!ELEMENTnote (message*)>
The * sign in the example above declares that the child element
"message" can occur zero or more times inside the "note" element.
Example:
<!ELEMENTnote (message?)>
The ? sign in the example above declares that the child element
"message" can occur zero or one time inside the "note" element.
Declaring either/or Content
<!ELEMENTnote (to,from,header,(message|body))>
The example above declares that the "note" element must
Attributes in DTD
In a DTD, attributes are declared with an
ATTLIST declaration.
<!ATTLISTelement-name attribute-name
attribute-type attribute-value>
DTD example:
<!ATTLISTpayment type CDATA "check>
XML example:
<paymenttype="check"/>
Theattribute-typecan be one of the
following:
CDATA
(en1|en2|..)
ID
IDREF
IDREFS
NMTOKEN
NMTOKENS
ENTITY
ENTITIES
NOTATION
xml:
#IMPLIED
#FIXEDvalu
e
#REQUIRED:
<!ATTLISTelement-name attribute-name
attribute-type #REQUIRED>
DTD:
<!ATTLISTperson number CDATA #REQUIRED>
Valid XML:
<personnumber="5677"/>
Invalid XML:
<person/>
Use the #REQUIRED keyword if you don't have
an option for a default value, but still want to
force the attribute to be present.
#IMPLIED
<!ATTLISTelement-name attribute-name
attribute-type #IMPLIED>
DTD:
<!ATTLISTcontact fax CDATA #IMPLIED>
Valid XML:
<contactfax="555-667788"/>
Valid XML:
<contact/>
Use the #IMPLIED keyword if you don't want to
force the author to include an attribute, and you
don't have an option for a default value.
#FIXED
<!ATTLISTelement-name attribute-name attribute-type
#FIXED "value">
DTD:
<!ATTLISTsender company CDATA #FIXED "Microsoft>
Valid XML:
<sendercompany="Microsoft"/>
Invalid XML:
<sendercompany=ABC"/>
Use the #FIXED keyword when you want an attribute to
have a fixed value without allowing the author to
change it. If an author includes another value, the XML
parser will return an error.
en2|..) default-value>
DTD:
<!ATTLISTpayment type (check|cash) "cash>
XML example:
<paymenttype="check"/>
or
<paymenttype="cash"/>
Use enumerated attribute values when you want
the attribute value to be one of a fixed set of
legal values.
Example:
Namespaces
XML Namespaces provide a method to avoid element name conflicts.
In XML, element names are defined by the developer. This often results
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.
defined.
The namespace is defined by thexmlns attributein the start tag of an
element.
The namespace declaration has the following syntax. xmlns:prefix="URI.
<root>
<h:tablexmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:tablexmlns:f="http://www.abccorp.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
Default Namespaces
Defining a default namespace for an element saves us from using
Fan
Ceiling Fan
Table Fan
Fan
English Movies Fan
Hindi Movies Fan
Apply Qualified Namespace to differentiate
Both Fan Tag.
XMLSchema
An XML Schema describes the structure of an XML
<xs:elementname="note>
<xs:complexType>
<xs:sequence>
<xs:elementname="to"type="xs:string"/
>
<xs:elementname="from"type="xs:string
"/>
<xs:elementname="heading"type="xs:st
ring"/>
<xs:elementname="body"type="xs:strin
g"/>
</xs:sequence>
files
You can use your XML parser to parse your Schema
files
You can manipulate your Schemas with the XML
DOM
You can transform your Schemas with XSLT
xs:boolean
xs:date xs:time
Example
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
Default and Fixed Values for Simple Elements
Simple elements may have a default value OR a fixed value
specified.
A default value is automatically assigned to the element
when no other value is specified.
<xs:element name="color" type="xs:string" default="red"/>
<xs:element name="color" type="xs:string" fixed="red"/>
Restrictions on Values:
Restrictions are used to define acceptable values for XML elements or
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
attributes.
There are four kinds of complex elements:
empty elements
<product pid="1345"/>
elements that contain only other elements
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
elements that contain only text
<food type="dessert">Ice cream</food>
elements that contain both other elements and text
<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="employee"
type="personinfo"/>
<xs:element name="student"
type="personinfo"/>
<xs:element name="member"
type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname"
type="xs:string"/>
<xs:element name="lastname"
type="xs:string"/>
document.
Create an XML file
Create a style sheet
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>
XSLT
XSLT stands for XSL Transformations
XSLT is the most important part of XSL
XSLT transforms an XML document into
Documents.
the official W3C XSLT namespace. If you use this namespace, you
must also include the attribute version="1.0.
template:
<?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 CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Document
document ("cdcatalog.xml"):
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl
href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
</catalog>
Exercise
8. Create an XML file as below:
<book>
<bookname>
<author>
<price>
<booktype>
Provide css for the above XML file in such a way that
hierarchy is displayed in proper format.
9. Apply XSLT for XML defined in Q.1.View All employees
with their Name and Designation in a table.
10. Apply XSLT for XML defined in Q.2.View All employees
with their Name and Designation in a list.