Department of Computer Science and Engineering
(CSE)
XML
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
XML NAMESPACES
XML Namespaces provide a method to
avoid element 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.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
CONT
This XML carries HTML table information:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
CONT
This XML carries information about a table
(a piece of furniture):
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
NOTE: If these XML fragments were added
together, there would be a name conflict.
Both contain a <table> element, but the
elements have different content and
meaning.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Solving the Name Conflict Using a Prefix
Name conflicts in XML can easily be
avoided using a name prefix.
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>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
CONT
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
There will be no conflict because the two <table>
elements have different names.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
XML Namespaces - The xmlns Attribute
When using prefixes in XML, a so-
callednamespacefor the prefix must be
defined.
The namespace is defined by thexmlns
attributein the start tag of an element.
The namespace declaration has the following
syntax. xmlns:prefix="URI".
When a namespace is defined for an element,
all child elements with the same prefix are
associated with the same namespace.
Namespaces can be declared in the elements
where they are used or in the XML root element:
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
CONT.
<root>
<h:tablexmlns:h="[Link]
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:tablexmlns:f="[Link]
re">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Uniform Resource Identifier (URI)
AUniform Resource Identifier(URI) is
a string of characters which identifies an
Internet Resource.
The purpose is to give the namespace a
unique name. However, often companies
use the namespace as a pointer to a web
page containing namespace information.
Note:The namespace URI is not used by
the parser to look up information.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Default Namespaces
Defining a default namespace for an element
saves us from using prefixes in all the child
elements. It has the following syntax:
xmlns="namespaceURI
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
DisplayingXML with CSS
With CSS (Cascading Style Sheets) you can add
display information to an XML document.
CATALOG
{ background-color: #ffffff;
width: 100%;
}
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
<?xmlversion="1.0"encoding="UTF-8"?>
<?xml-stylesheettype="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>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
XMLDocument Types
An XML document with correct syntax is called
"Well Formed".
A "Valid" XML document must also conform to a
document type definition.
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
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Valid XML Documents
A "valid" XML document is not the same as a
"well formed" XML document.
A "valid" XML document must be well formed. In
addition it must conform to a document type
definition.
Rules that defines the legal elements and
attributes for XML documents are called
Document Type Definitions (DTD) or XML
Schemas.
There are two different document type definitions
that can be used with XML:
DTD - The original Document Type Definition
XML Schema - An XML-based alternative to DTD
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
When to Use a DTD/Schema?
With a DTD, independent groups of people can
agree to use a standard DTD for interchanging
data.
our application can use a standard DTD to
verify that the data we receive from the
outside world is valid.
we can also use a DTD to verify your own data.
When we are experimenting with XML, or
when you are working with small XML files,
creating DTDs may be a waste of time.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
DTD -XML Building Blocks
Elements
Attributes
Entities
PCDATA
CDATA
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
ELEMENTS
Elements are themain building blocksof both
XML and HTML documents.
Examples of HTML elements are "body" and
"table". Examples of XML elements could be
"note" and "message".
Examples:
<body>some text</body>
<message>some text</message>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
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:
<imgsrc="[Link]"/>
The name of the element is "img". The name of
the attribute is "src". The value of the attribute is
"[Link]
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
ENTITIES
Some characters have a special meaning in
XML, like the less than sign (<) that defines the
start of an XML tag.
" ". This "no-breaking-space" entity is
used in HTML to insert an extra space in a
document. Entities are expanded when a
document is parsed by an XML parser.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
PC DATA
PCDATA means parsed character data.
Think of character data as the text found between
the start tag and the end tag of an XML element.
PCDATA is text that WILL be parsed by a
[Link] 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.
However, parsed character data should not contain
any &, <, or > characters; these need to be
represented by the & < and > entities,
respectively.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
CD DATA
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.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
XMLDTD
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEnote SYSTEM "[Link]">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The DOCTYPE declaration, in the example above, is
a reference to an external DTD file
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
XML DTD
The purpose of a DTD is to define the structure
of an XML document. It defines the struct
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
XML DTD
DOCTYPE note defines that the root element of the
document is note
!ELEMENT note defines that the note element must
contain four elements: "to, from, heading, body"
!ELEMENT to defines the to element to be of type
"#PCDATA"
!ELEMENT from defines the from element to be of
type "#PCDATA"
!ELEMENT heading defines the heading element to be
of type "#PCDATA"
!ELEMENT body defines the body element to be of
type "#PCDATA"
NOTE:#PCDATA means parse-able text data.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
XSLT
XSL stands for EXtensible Stylesheet
Language, and is a style sheet language
for XML [Link] stands for XSL
Transformations.
XSLT is an XML language that can be
used to transform XML documents into
other formats, like HTML.
XPath is a language for navigating in XML
documents.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
XPath
XPath is a syntax for defining parts of an XML
document
XPath uses path expressions to navigate in XML
documents
XPath contains a library of standard functions
XPath is a major element in XSLT
XPath is a W3C recommendation
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
NODES
In XPath, there are seven kinds of nodes: element, attribute,
text, namespace, processing-instruction, comment, and
document nodes.
XML documents are treated as trees of nodes. The topmost
element of the tree is called the root element.
Look at the following XML document:
<?xmlversion="1.0"encoding="UTF-8"?>
<bookstore>
<book>
<titlelang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
X PATH SYNTAX
XPath uses path expressions to select nodes or node-sets
in an XML document. The node is selected by following a
path or steps.
<?xmlversion="1.0"encoding="UTF-8"?>
<bookstore>
<book>
<titlelang="en">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<titlelang="en">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Selecting Nodes
ExpressionDescription
nodenameSelects all nodes with the name
"nodename
/Selects from the root node
//Selects nodes in the document from the
current node that match the selection no matter
where they are
.Selects the current node
..Selects the parent of the current node
@Selects attributes
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
PREDICATES
Predicates are used to find a specific node or a
node that contains a specific value.
Predicates are always embedded in square
brackets.
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Correct Style Sheet Declaration
The root element that declares the document to
be an XSL style sheet is <xsl:stylesheet> or
<xsl:transform>.
<xsl:stylesheetversion="1.0"
xmlns:xsl="[Link]
rm">
OR
<xsl:transformversion="1.0"
xmlns:xsl="[Link]
rm">
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Start with a Raw XML Document
We want totransformthe following XML
document ("[Link]") into XHTML:
<?xmlversion="1.0"encoding="UTF-8"?>
<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>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Create an XSL Style Sheet
<?xmlversion="1.0"encoding="UTF-8"?>
<xsl:stylesheetversion="1.0"
xmlns:xsl="[Link]
<xsl:templatematch="/">
<html>
<body>
<h2>My CD Collection</h2>
<tableborder="1">
<trbgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-eachselect="catalog/cd">
<tr>
<td><xsl:value-ofselect="title"/></td>
<td><xsl:value-ofselect="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
Link the XSL Style Sheet to the XML Document
<?xmlversion="1.0"encoding="UTF-8"?>
<?xml-stylesheettype="text/xsl"href="[Link]"?
>
<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>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
<xsl:value-of> Element (CSE)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="[Link]
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering
(CSE)
<xsl:for-each>Element
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="[Link]
<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>
University Institute of Engineering (UIE)