XML Unit 3
XML Unit 3
XML stands for Extensible Markup Language. It is a text-based markup language derived
from Standard Generalized Markup Language (SGML).
XML tags identify the data and are used to store and organize the data, rather than
specifying how to display it like HTML tags, which are used to display the data. XML is
not going to replace HTML in the near future, but it introduces new possibilities by
adopting many successful features of HTML.
There are three important characteristics of XML that make it useful in a variety of
systems and solutions:
XML is extensible: XML allows you to create your own self-descriptive tags or
language, that suits your application.
XML carries the data, does not present it: XML allows you to store the data
irrespective of how it will be presented.
XML is a public standard: XML was developed by an organization called the
World Wide Web Consortium (W3C) and is available as an open standard.
XML Usage
A short list of XML usage says it all:
XML can be used to exchange the information between organizations and systems.
XML can be used for offloading and reloading of databases.
XML can be used to store and arrange the data, which can customize your data
handling needs.
XML can easily be merged with style sheets to create almost any desired output.
Virtually, any type of data can be expressed as an XML document.
XML
What is Markup?
XML is a markup language that defines set of rules for encoding documents in a format
that is both human-readable and machine-readable. So, what exactly is a markup
language? Markup is information added to a document that enhances its meaning in
certain ways, in that it identifies the parts and how they relate to each other. More
specifically, a markup language is a set of symbols that can be placed in the text of a
document to demarcate and label the parts of that document.
Following example shows how XML markup looks, when embedded in a piece of text:
<message>
<text>Hello, world!</text>
</message>
This snippet includes the markup symbols, or the tags such as <message>...</message>
and <text>... </text>. The tags <message> and </message> mark the start and the end of
the XML code fragment. The tags <text> and </text> surround the text Hello, world!.
<?xml version="1.0"?>
<contact-info>
<name>Tanmay Patil</name>
<company>AbcTechnologies</company>
<phone>(011) 123-4567</phone>
</contact-info>
You can notice, there are two kinds of information in the above example:
XML Declaration
The XML document can optionally have an XML declaration. It is written as follows:
Where version is the XML version and encoding specifies the character encoding used in
the document.
<element>
<element>....</element>
<element/>
<?xml version="1.0"?>
<contact-info>
<company>AbcTechnologies
<contact-info>
</company>
<?xml version="1.0"?>
<contact-info>
<company>AbcTechnologies</company>
<contact-info>
Root Element: An XML document can have only one root element. For example,
following is not a correct XML document, because both the x and y elements occur at the
top level without a root element:
<x>...</x>
<y>...</y>
<root>
<x>...</x>
<y>...</y>
</root>
Case Sensitivity: The names of XML-elements are case-sensitive. That means the
name of the start and the end elements need to be exactly in the same case.
XML Attributes
An attribute specifies a single property for the element, using a name/value pair. An
XML-element can have one or more attributes. For example:
<a href="http://www.AbcTechnologies.com/">AbcTechnologies!</a>
Attribute names in XML (unlike HTML) are case sensitive. That is, HREF and href
are considered two different XML attributes.
XML
Same attribute cannot have two values in a syntax. The following example shows
incorrect syntax because the attribute b is specified twice:
Attribute names are defined without quotation marks, whereas attribute values
must always appear in quotation marks. Following example demonstrates
incorrect xml syntax:
<a b=x>....</a>
In the above syntax, the attribute value is not defined in quotation marks.
XML declaration
It contains details that prepare an XML processor to parse the XML document. It is
optional, but when used, it must appear in the first line of the XML document.
Syntax
Following syntax shows XML declaration:
<?xml
version="version_number"
encoding="encoding_declaration"
standalone="standalone_status"
?>
Each parameter consists of a parameter name, an equals sign (=), and parameter value
inside a quote. Following table shows the above syntax in detail:
10646-UCS-2, ISO-
EUC-JP
<?xml >
<?xml version="1.0">
Rule 1
XML tags are case-sensitive. Following line of code is an example of wrong syntax
</Address>, because of the case difference in two tags, which is treated as erroneous
syntax in XML.
Following code shows a correct way, where we use the same case to name the start and
the end tag.
Rule 2
XML tags must be closed in an appropriate order, i.e., an XML tag opened inside another
element must be closed before the outer element is closed. For example:
<outer_element>
<internal_element>
This tag is closed before the outer_element
</internal_element>
</outer_element>
Document Type Declaration(DTD)
The XML Document Type Declaration, commonly known as DTD, is a way to describe
XML language precisely. DTDs check vocabulary and validity of the structure of XML
documents against grammatical rules of appropriate XML language.
An XML DTD can be either specified inside the document, or it can be kept in a separate
document and then liked separately.
Syntax
Basic syntax of a DTD is as follows:
Internal DTD
A DTD is referred to as an internal DTD if elements are declared within the XML files. To
refer it as internal DTD, standalone attribute in XML declaration must be set to yes. This
means, the declaration works independent of an external source.
XML
Syntax
Following is the syntax of internal DTD:
where root-element is the name of root element and element-declarations is where you
declare the elements.
Example
Following is a simple example of internal DTD:
Start Declaration - Begin the XML declaration with the following statement.
DTD - Immediately after the XML header, the document type declaration follows,
commonly referred to as the DOCTYPE:
<!DOCTYPE address [
The DOCTYPE declaration has an exclamation mark (!) at the start of the element name.
The DOCTYPE informs the parser that a DTD is associated with this XML document.
DTD Body - The DOCTYPE declaration is followed by the body of the DTD, where you
declare elements, attributes, entities, and notations.
Several elements are declared here that make up the vocabulary of the <name>
document. <!ELEMENT name (#PCDATA)> defines the element name to be of type
"#PCDATA". Here, #PCDATA means parse-able text data.
End Declaration - Finally, the declaration section of the DTD is closed using a closing
bracket and a closing angle bracket (]>). This effectively ends the definition, and
thereafter, the XML document follows immediately.
Rules
The document type declaration must appear at the start of the document (preceded
only by the XML header) — it is not permitted anywhere else within the document.
Similar to the DOCTYPE declaration, the element declarations must start with an
exclamation mark.
The Name in the document type declaration must match the element type of the
root element.
External DTD
In external DTD elements are declared outside the XML file. They are accessed by
specifying the system attributes which may be either the legal .dtd file or a valid URL. To
refer it as external DTD, standalone attribute in the XML declaration must be set as no.
This means, declaration includes information from the external source.
Syntax
Following is the syntax for external DTD:
Example
The following example shows external DTD usage:
Types
You can refer to an external DTD by using either system identifiers or public
identifiers.
System Identifiers
A system identifier enables you to specify the location of an external file containing DTD
declarations. Syntax is as follows:
As you can see, it contains keyword SYSTEM and a URI reference pointing to the location
of the document.
Public Identifiers
Public identifiers provide a mechanism to locate DTD resources and is written as follows:
As you can see, it begins with keyword PUBLIC, followed by a specialized identifier. Public
identifiers are used to identify an entry in a catalog. Public identifiers can follow any format,
however, a commonly used format is called Formal Public Identifiers, or FPIs.
Seen from a DTD point of view, all XML documents are made up by the following building
blocks:
Elements
Attributes
Entities
PCDATA
CDATA
Elements
Elements are the main building blocks of both XML and HTML documents.
Examples of HTML elements are "body" and "table". Examples of XML elements could be
"note" and "message". Elements can contain text, other elements, or be empty.
Examples of empty HTML elements are "hr", "br" and "img".
Examples:
<body>some text</body>
<message>some text</message>
Attributes
Attributes provide extra 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:
The name of the element is "img". The name of the attribute is "src". The value of the
attribute is "computer.gif". Since the element itself is empty it is closed by a " /".
Entities
Some characters have a special meaning in XML, like the less than sign (<) that defines
the start of an XML tag.
Most of you know the HTML entity: " ". 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.
< <
> >
& &
" "
' '
PCDATA
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 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.
However, parsed character data should not contain any &, <, or > characters; these
need to be represented by the & < and > entities, respectively.
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.
Non DTD XML files must use the predefined character entities for amp(&),
apos(single quote), gt(>), lt(<), quot(double quote).
It must follow the ordering of the tag. i.e., the inner tag must be closed before
closing the outer tag.
Each of its opening tags must have a closing tag or it must be a self-ending tag
(<title>....</title> or <title/>).
It must have only one attribute in a start tag, which needs to be quoted.
amp(&), apos(single quote), gt(>), lt(<), quot(double quote) entities
other than these must be declared.
Example
Following is an example of a well-formed XML document:
Syntax
You need to declare a schema in your XML document as follows:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
Example
The following example shows how to use schema:
The basic idea behind XML Schemas is that they describe the legitimate format that an
XML document can take.
Elements
Elements are the building blocks of XML document. An element can be defined within an
XSD as follows:
Definition Types
You can define XML schema elements in the following ways:
Simple Type
Simple type element is used only in the context of the text. Some of the predefined
simple types are: xs:integer, xs:boolean, xs:string, xs:date. For example:
Complex Type
A complex type is a container for other element definitions. This allows you to specify
which child elements an element can contain and to provide some structure within your
XML documents. For example:
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="company" type="xs:string" />
In the above example, Address element consists of child elements. This is a container for
other <xs:element> definitions, that allows to build a simple hierarchy of elements in
the XML document.
Global Types
With the global type, you can define a single type in your document, which can be used
by all other references. For example, suppose you want to generalize the person and
company for different addresses of the company. In such case, you can define a general
type as follows:
<xs:element name="AddressType">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="company" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
XML
<xs:element name="Address1">
<xs:complexType>
<xs:sequence>
<xs:element name="address" type="AddressType" />
<xs:element name="phone1" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Address2">
<xs:complexType>
<xs:sequence>
<xs:element name="address" type="AddressType" />
<xs:element name="phone2" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
Instead of having to define the name and the company twice (once for Address1 and once
for Address2), we now have a single definition. This makes maintenance simpler, i.e., if you
decide to add "Postcode" elements to the address, you need to add them at just one place.
Attributes
Attributes in XSD provide extra information within an element. Attributes have name and
type property as shown below:
employee.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3. targetNamespace="http://www.abc.com"
4. xmlns="http://www.abc.com"
5. elementFormDefault="qualified">
6. <xs:element name="employee">
7. <xs:complexType>
8. <xs:sequence>
9. <xs:element name="firstname" type="xs:string"/>
10. <xs:element name="lastname" type="xs:string"/>
11. <xs:element name="email" type="xs:string"/>
12. </xs:sequence>
13. </xs:complexType>
14. </xs:element>
15. </xs:schema>
Let's see the xml file using XML schema or XSD file.
employee.xml
1. <?xml version="1.0"?>
2. <employee
3. xmlns="http://www.abc.com"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://www.abc.com employee.xsd">
6. <firstname>vimal</firstname>
7. <lastname>jaiswal</lastname>
8. <email>[email protected]</email>
9. </employee>
Description of XML Schema
<xs:element name="employee"> : It defines the element name employee.
DTD vs XSD
There are many differences between DTD (Document Type Definition) and XSD (XML Schema
Definition). In short, DTD provides less control on XML structure whereas XSD (XML schema) provides
more control.
1) DTD stands for Document Type XSD stands for XML Schema Definition.
Definition.
5) DTD doesn't define order for XSD defines order for child elements.
child elements.
6) DTD provides less control on XSD provides more control on XML structure.
XML structure.
SAMPLE.xsd
The tree structure contains root (parent) elements, child elements, and so on. By using tree
structure, you can get to know all succeeding branches and sub-branches starting from the
root. The parsing starts at the root, then moves down the first branch to an element, take
the first branch from there, and so on to the leaf nodes.
Example
Following example demonstrates simple XML tree structure:
<?xml version="1.0"?>
<Company>
<Employee>
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>Bangalore</City>
<State>Karnataka</State>
<Zip>560212</Zip>
</Address>
</Employee>
</Company>
The XML DOM, on the other hand, also provides an API that allows a
developer to add, edit, move, or remove nodes in the tree at any point in
order to create an application.
parentNode
childNodes
firstChild
lastChild
nextSibling
previousSibling
EXAMPLE for DOM-1:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
}
</script>
</body>
</html>
EXAMPLE-2:
<?xml version="1.0"?>
<contact-info>
<name>Tanmay Patil</name>
<company>AbcTechnologies</company>
<phone>(011) 123-4567</phone>
</contact-info>
<html>
<body>
<h1>AbcTechnologies DOM example </h1>
<div>
<b>Name:</b> <span id="name"></span><br> <b>Company:</b>
<span id="company"></span><br> <b>Phone:</b> <span
id="phone"></span>
</div>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","address.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.getElementById("name").innerHTML=
xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
document.getElementById("company").innerHTML=
xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeVa
lue;
document.getElementById("phone").innerHTML=
xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue
;
</script>
</body>
</html>
1. XSLT – Overview XSLT
XSL
Before learning XSLT, we should first understand XSL which stands for EXtensible
Stylesheet Language. It is similar to XML as CSS is to HTML.
XSLT - used to transform XML document into various other types of document.
XPath - used to navigate XML document.
XSL-FO - used to format XML document.
What is XSLT
XSLT, Extensible Stylesheet Language Transformations, provides the ability to transform
XML data from one format to another automatically.
1
XSLT
Advantages
Here are the advantages of using XSLT:
2
2. XSLT – Syntax XSLT
Let’s suppose we have the following sample XML file, students.xml, which is required to be
transformed into a well-formatted HTML document.
students.xml
<?xml version="1.0"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
We need to define an XSLT style sheet document for the above XML document to meet the
following criteria:
3
XSLT
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<!-- xsl stylesheet declaration with xsl namespace:
Namespace tells the xlst processor about which element is to be processed
and which is used for output purpose only
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- xsl template declaration:
template tells the xlst processor about the section of xml document which is to be
formatted. It takes an XPath expression.
In our case, it is matching document root element and will tell processor to
process the entire document with this template.
-->
<xsl:template match="/">
<!-- HTML tags
Used for formatting purpose. Processor will skip them and browser will
simply render them.
-->
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<!-- for-each processing instruction
Looks for each element matching the XPAth expression
-->
4
XSLT
<xsl:for-each select="class/student">
<tr>
<td>
<!-- value-of processing instruction
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
...
</class>
5
XSLT
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
6
XSLT
Output
XPath uses path expressions to select nodes in an XML document.
The node is selected by following a path or steps. The most useful path
expressions are listed below:
Expression Description
@ Selects attributes
3. XSLT – Template XSLT
<xsl:template> defines a way to reuse templates in order to generate the desired output
for nodes of a particular type/context.
Declaration
Following is the syntax declaration of <xsl:template> element.
<xsl:template
name= Qname
match = Pattern
priority = number
mode = QName >
</xsl:template>
Attributes
Name Description
Elements
Number of
occurrences Unlimited
Parent
xsl:stylesheet, xsl:transform
elements
8
XSLT
Demo Example
This template rule has a pattern that identifies <student> elements and produces an output
in a tabular format.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
9
XSLT
students_imports.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
10
XSLT
Output
11
4. XSLT – <value-of> XSLT
<xsl:value-of> tag puts the value of the selected node as per XPath expression, as text.
Declaration
Following is the syntax declaration of <xsl:value-of> element
<xsl:value-of
select = Expression
disable-output-escaping = "yes" | "no" >
</xsl:value-of>
Attributes
Name Description
disable-
Default-"no". If "yes", output text will not escape xml characters from
output-
text.
escaping
Elements
Number of
Unlimited
occurrences
Demo Example
This example creates a table of <student> element with its attribute rollno and its child
<firstname>, <lastname>, <nickname>, and <marks>.
12
XSLT
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
13
XSLT
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
14
XSLT
15
5. XSLT – <for-each> XSLT
Declaration
Following is the syntax declaration of <xsl:for-each> element
<xsl:for-each
select = Expression >
</xsl:for-each>
Attributes
Name Description
Elements
Number of
Unlimited
occurrences
16
XSLT
Demo Example
This example creates a table of <student> element with its attribute rollno and its child
<firstname>, <lastname>, <nickname> and <marks> by iterating over each student.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
17
XSLT
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
18
XSLT
Output
19
6. XSLT – <sort> XSLT
Declaration
Following is the syntax declaration of <xsl:sort> element.
<xsl:sort
select = string-expression
lang = { nmtoken }
data-type = { "text" | "number" | QName }
order = { "ascending" | "descending" }
case-order = { "upper-first" | "lower-first" } >
</xsl:sort>
Attributes
Name Description
Elements
Number of occurrences Unlimited
20
XSLT
Demo Example
This example creates a table of <student> element with its attribute rollno and its child
<firstname>, <lastname>, <nickname>, and <marks> by iterating over each student sort
them by first name.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
21
XSLT
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<xsl:sort select="firstname"/>
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
22
XSLT
Output
23
7. XSLT – <if> XSLT
Declaration
Following is the syntax declaration of <xsl:if> element.
<xsl:if
test = boolean-expression >
</xsl:if>
Attributes
Name Description
Elements
Number of
Unlimited
occurrences
Demo Example
This example creates a table of <student> element with its attribute rollno and its child
<firstname>, <lastname>, <nickname>, and <marks> by iterating over each student. It
checks marks to be greater than 90 and then prints the student(s) details.
24
XSLT
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
25
XSLT
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<xsl:if test="marks > 90">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
26
XSLT
Output
27
8. XSLT – <choose> XSLT
<xsl:choose> tag specifies a multiple conditional tests against the content of nodes in
conjunction with the <xsl:otherwise> and <xsl:when> elements.
Declaration
Following is the syntax declaration of <xsl:choose> element.
<xsl:choose >
</xsl:choose>
Elements
Number of
Unlimited
occurrences
Demo Example
This example creates a table of <student> element with its attribute rollno and its child
<firstname>, <lastname>, <nickname>, and <marks> by iterating over each student. It
checks and then prints the grade details.
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
28
XSLT
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
<th>Grade</th>
</tr>
<xsl:for-each select="class/student">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
29
XSLT
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
<td>
<xsl:choose>
<xsl:when test="marks > 90">
High
</xsl:when>
<xsl:when test="marks > 85">
Medium
</xsl:when>
<xsl:otherwise>
Low
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
30
XSLT
Output
31
9. XSLT – <key> XSLT
<xsl:key> tag element specifies a named name-value pair assigned to a specific element in
an XML document. This key is used with the key() function in XPath expressions to access
the assigned elements in an XML document.
Declaration
Following is the syntax declaration of <xsl:key> element
<xsl:key
name = QName
match = Pattern
use = Expression >
</xsl:key>
Attributes
Name Description
Use XPath expression to identify the value of the nodes of xml document.
Elements
Number of
occurrences Unlimited
Demo Example
This example creates a table of <student> element with its attribute rollno and its child
<firstname>, <lastname>, <nickname>, and <marks> by iterating over each student. It
checks key as firstname to be one of the student's name and then prints the student details.
32
XSLT
students.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<class>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
33
XSLT
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="key('firstname-search', 'Dinkar')">
<tr>
<td>
<xsl:value-of select="@rollno"/>
</td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="nickname"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output
34
Web Programming Step by Step
Chapter 10
Ajax and XML for Accessing Data
Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica
Miller.
Web applications
web application: a web site that mimics the look, feel, and overall user experience of a desktop
application
a web app presents a continuous user experience rather than disjoint pages
as much as possible, "feels" like a normal program to the user
examples of web apps
Gmail, Google Maps, Google Docs and Spreadsheets, Flickr, A9
many web apps use Ajax to battle these problems of web pages:
slowness / lack of UI responsiveness
lack of user-friendliness
jarring nature of "click-wait-refresh" pattern
What is Ajax?
Ajax: Asynchronous JavaScript and XML
asynchronous: user can keep interacting with page while data loads
communication pattern made possible by Ajax
// at this point, the request will have returned with its data
do something with ajax.responseText;
web servers return status codes for requests (200 means Success)
you may wish to display a message or take action on a failed request
function functionName(ajax) {
do something with ajax.responseText;
}