Unit 3 Working With XML Introduction To XML
Unit 3 Working With XML Introduction To XML
Unit 3
Working with XML
Introduction to XML:
XML stands for eXtensible Markup Language and is a text-based markup language derived from
Standard Generalized Markup Language (SGML). The primary purpose of this standard is to provide way to
store self describing data easily. Self-describing data are those that describe both their structure and their
content. But, HTML documents describe how data should appear on the browsers screen and no
information about the data. XML documents, on the other hand describe the meaning of data. The content
and structure of XML documents are accessed by software module called XML processor.
XML Characteristics:
1. XML is extensible : XML essentially allows you to create your own language, or tags, that suits
your application.
2. XML separates data from presentation : XML allows you to store content with regard to how it
will be presented.
3. XML is a public standard : XML was developed by an organization called the World Wide Web
Consortium (W3C) and available as an open standard.
XML Usage:
A short list of XML's usage says it all
XML can work behind the scene to simplify the creation of HTML documents for large web sites.
XML can be used to exchange of information between organizations and systems.
XML can be used for offloading and reloading of databases.
XML can be used to store and arrange data in a way that is customizable for your needs.
XML can easily be mixed with stylesheets to create almost any output desired.
XML features:
XML allows the user to define his own tags and his own document structure.
XML document is pure information wrapped in XML tags.
XML is a text based language, plain text files can be used to share data.
XML provides a software and hardware independent way of sharing data.
1. Prolog:
This part of XML document may contain following parts: XML declaration, Optional processing
instructions, Comments and Document Type Declaration
XML Declaration:
Every XML document should start with one-line XML declaration which describes document itself. The
XML declaration is written as below:
Syn: <?xml version="1.0" encoding="UTF-8"?>
Where version is the XML version and encoding specify the character encoding used in the document. UTF-
8 stands for Unicode Transformation Format is used for set of ASCII characters. It also have standalone
attribute indicates whether the document can be processed as standalone document or is dependent on other
document like Document Type Declaration(DTD).
Syn: <?xml version="1.0" encoding="UTF-8" standalone=”yes|no”?>
Processing Instruction:
Processing Instructions starts with left angular bracket along with question mark(<?),ending with
question mark followed by the right angular bracket(?>). These parameters instruct the application about
how to interpret XML document. XML parser’s do not take care of processing instructions and are not text
portion of XML document.
Ex: <?xsl-stylesheet href=”simple.xsl” type=”text/xsl”?>
www.Jntufastupdates.com 1
Page|2
Comments:
Like HTML, comments may use anywhere in XML documents. An XML comments starts with <!—and
ends with -->. Everything with in these will be ignored by the parsers and will not be parsed.
Syn: <!-- this is comments -->
Following points should be remembered while using comments: do not use double hyphens, never place
inside entity declaration or within any tag, never place before XML declaration
Document Type Declaration(DTD):
XML allows to create new tags and have meaning if it has some logical structure created using set of
related tags. <!DOCTYPE > is used to specify the logical structure of XML document by imposing
constraints on what tags can be used and where. DTD may contain Name of root element, reference to
external DTD, element and entity declarations.
2. Body:
This portion of XML document contains textual data marked up by tags. It must have one element
called Document or Root element, which defines content in the XML document. Root element must be the top-
level element in the document hierarchy and there can be one and only one root element.
Ex: <?xml version=”1.0”?>
<book>
<title>WT</title>
<author>Uttam Roy</author>
<price>500</price>
</book>
In this document, the name of root element id <book> which contains sub tags <title>, <author> and
<price>. Each of these tags contains text “WT”, “Uttam Roy” and “500” respectively.
XML Elements
An XML element consists of starting tag, an ending tag and its contents and attributes. The contents
may be simple text or other element or both. XML tags are very much similar to that of HTML tags. A tag
begins with less than(<) and ends with greater than(>) character. It takes the form <tag-name> and must
have corresponding ending tag(</tag-name>). An element consists of opening tag, closing tag and contents.
Few tag may not contain any content and hence know as Empty elements. According to the well-formedness
constraint, every XML element must have closing tag. XML provides two ways for XML empty elements as
follows:
Syn: <br></br> or <br />
Following are the rules that need to be followed for XML elements:
An element name can contain any alphanumeric characters. The only punctuation allowed in names
are the hyphen ( - ), under-score ( _ ) and period ( . )
Names are case sensitive. For example Address, address, ADDRESS are different names
Element start and end tag should be identical
An element which is a container can contain text or elements as seen in the above example
Attributes: Attributes are used to describe elements or to provide more information about elements. They
appear in the starting tag of element. The syntax of specifying an attribute in element is:
Syn: <element-name attribute-name=”value”>…</elment-name>
Ex: <employee gener=”male”>ABCD</employee>
There is no strict rules that describes when to use elements and when to use attributes. However, it is
recommended not to use attributes as far as possible due to following reasons:
Too many attributes reduce readability of XML document
Attributes cannot contain multiple values, but elements can
Attributes are not easily extendable
Attributes cannot represent logical structure, but elements together with their child elements can
Attributes are difficult to access by parsers
www.Jntufastupdates.com 2
Page|3
Well-formed XML:
An XML document is said to be well-formed if it contains text and tags that conform with the basic XML well-
formedness constraints. XML can extend existing documents by creating new elements that fit their
applications. The only thing is to remember the well-formedness constraints. The following rules must be
followed by XML documents:
An XML document must have one and only one root element
All tags must be closed
All tags must be properly nested
XML tags are case-sensitive
Attributes must always be quoted
Certain characters are reserved for processing like pre-defined entities
Pre-defined Entities: W3C specification defined few entities each of which represents a special character
that cannot be used in XML document directly. All XML processors must recognize those entities, whether
they are declared or not.
Entity Name Entity Number Description Character
< < Less than <
> > Greater than >
& & Amprersand &
" " Quotation mark “
' ' Apostrophe ‘
Valid XML
Well-formed XML documents obey only basic well formedness constraints. So, valid XML documents are
those that are well formed and comply with rules specified in DTC or Schema.
Name Space
XML was developed to be used by many applications. If many applications want to communicate using
XML documents, problems may occur. In XML document, element and attribute names are selected by
developers. In some cases two different documents may have same root element. For example, both
client.xml and server.xml contains same root tag <config> as shown below.
Client.xml Server.xml
<config> <config>
<version>1.0</version> <version>1.0</version>
</config> </config>
XML namespace provides simple, straightforward way to distinguish between element names in XML
document. Namespace suggests to use prefix with every element as follows:
Client.xml Server.xml
<c:config> <s:config>
<c:version>1.0</c:version> <s:version>1.0</s:version>
</c:config> </s:config>
Uniform Resource Identifier(URI) is used to guarantee the prefixes used by different developers. In general
URL are used to choose unique name. But, URL must be prefixed for each tag instead of them we use
prefix. Prefixes are just shorthand placeholders of URLs. Association of prefix and URL is done in the
starting tag using reserved XML attribute xmlns.
Syn: xmlns:prefix=”URI”
Name Space Rules: The xmlns attribute identifies namespace and makes association between prefix and
created namespace. Many prefixes may be associated with one namespace.
www.Jntufastupdates.com 3
Page|4
Default Namespace: Namespaces may not have their associated prefixes and are called default namespace.
In such cases, a blank prefix is assumed for element and all of its descendants.
www.Jntufastupdates.com 4
Page|5
<author>Uttam Roy</author>
<publisher>Oxford</publisher>
<price>500</price>
</book>
<book>
<title>AJ</title>
<author>Schildt</author>
<publisher>TMH</publisher>
<price>200</price>
</book>
</bookstore>
2. External DTD:
Another way of connection DTD to XML document is to reference it with in XML document i.e.
create separate document, put DTD information there and point to it from XML document. The general
syntax for external DTD is.
Syn: <!DOCTYPE root-element SYSTEM | PUBLIC "uri">
Where uri is the Uniform Resource Identifier of the .dtd file. This declaration states that we are going to
define structure of root-element of XML document and its definition can be found from uri specified like
book.dtd. both xml and dtd files should be kept in same directory.
Ex:
book.xml book.dtd
<?xml version="1.0" ?> <!ELEMENT bookstore (book*)>
<!DOCTYPE book SYSTEM "book.dtd"> <!ELEMENT book (title,author,price)>
<bookstore> <!ELEMENT title (#PCDATA)>
<book> <!ELEMENT author (#PCDATA)>
<title>WT</title> <!ELEMENT publisher (#PCDATA)>
<author>Uttam Roy</author> <!ELEMENT price (#PCDATA)>
<publisher>Oxford</publisher>
<price>500</price>
</book>
<book>
<title>AJ</title>
<author>Schildt</author>
<publisher>TMH</publisher>
<price>200</price>
</book>
</bookstore>
Location of DTD need not always be local file, it can be any valid URL. Following declaration for XHTML
uses PUBLIC DTD:
Syn: <!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Transitional//EN’>
Disadvantage of using separate DTD is we have to deal with two documents.
3. Combining Internal and External DTD:
External DTD are useful for common rules for set of XML documents, whereas internal DTDs are beneficial
for defining customized rules for specific document. XML allows to combine both internal and external
DTD for complete collection of rules for given document. The general form of such DTD is:
Syn: <!DOCTYPE root-element SYSTEM | PUBLIC "uri" [ DTD declarations… ] >
Ex: <?xml version="1.0" ?>
<!DOCTYPE book SYSTEM "book.dtd"
[ <!ELEMENT excl ‘’>
]>
<msg>Hello, World! </msg>
www.Jntufastupdates.com 5
Page|6
www.Jntufastupdates.com 6
Page|7
www.Jntufastupdates.com 7
Page|8
Enumerated type: enumerated attribute values are used when we want attribute value to be one of
fixed set of values. There are two kinds of enumerated types:
o Enumeration: attributes are defined by a list of acceptable values from which document
author must choose a value. The values are explicitly specified in declaration, separated by
pipe(|)
<!ATTLIST employee gender (male|female) #REQUIRED>
o Notation: it allows to use value that has been declared a NOTATION in DTD. Notation is
used to specify format of non-XML data and common used is to describe MIME types like
image/gif, image/jpeg etc.
<!NOTATION jpg SYSTEM ‘image/gif’>
<!ENTITY logo SYSTEM ‘logo.jpg’ NDATA jpg>
<!ATTLIST photo format NOTATION (jpg) #IMPLIED>
www.Jntufastupdates.com 8
Page|9
2. XML Schema:
XML Schema Definition commonly known as XSD, is a way to describe precisely the XML
language. XSD check the validity of structure and vocabulary of an XML document against the grammatical
rules of the appropriate XML language.An XML document can be defined as:
Well-formed: If the XML document adheres to all the general XML rules such as tags must be
properly nested, opening and closing tags must be balanced and empty tags must end with '/>', then it
is called as well-formed.
Valid: An XML document said to be valid when it is not only well-formed, but it also conforms to
available XSD that specifies which tags it uses, what attributes those tags can contain and which tags
can occur inside other tags, among other properties.
Limitaions of Document Type Declaration (DTD)
There is no bult-in data type in DTDs
No new data types can be created in DTDs
The use of cardinatlity in DTDs is limited
Namespaces are not supported
DTDs provide very limited support for modularity and reuse
We can not put any restrictions on text content
Defaults for elements can not be specified
We have very little control over mixed content
DTDs are written in strange format and are difficult to validate
Strengths of XML Schema(XSD)
XML schema provided much grater specification than DTDs
They support large number of built-in data types
They support namespaces
They are extensible to future additions
They support uniqueness and referencial integrity constraints in much better way
It is easier to define data restrictions
www.Jntufastupdates.com 9
P a g e | 10
www.Jntufastupdates.com 10
P a g e | 11
www.Jntufastupdates.com 11
P a g e | 12
o maxOccurs: this attribute specifies maximum number of times an element can occur. The
declaration of element will be as follows:
<xs:element name=”option” type=”xs:string” maxOccurs=”10”/>
Schema DTD Meaning
minOccurs=’0’, maxOccurs=’unbounded’ * Zero or more
minOccurs=’1’, maxOccurs=’unbounded’ + One or more
Minoccurs=’0’ ? Optional
None None Exactly one
2.3.2 Declarting complex elements:
Complex types can be named or can be anonymous. They are associated with complex elements in
the same manner, typically using a type definition and an element declaration. By default, complex type
elements have complex content i.e. they have child elements. Complex type elements can be limited to
having simple content i.e. they contain only text. General form of element declaration is:
Syn: <xs:complexType name=”complex-type-name”><xs:sequence>
</xs:sequence></xs:complexType>
Ex: <xs:complexType name=”sName”><xs:sequence>
<xs:element name=”first” type=”xs:string”/>
<xs:element name=”middle” type=”xs:string”/>
<xs:element name=”lase” type=”xs:string”/>
</xs:sequence></xs:complexType>
www.Jntufastupdates.com 12
P a g e | 13
</xs:all>
o Choice: Only one of the child element can occur.
<xs:choice>
<xs:element name=”first” type=”xs:string”/>
<xs:element name=”middle” type=”xs:string”/>
<xs:element name=”last” type=”xs:string”/>
</xs:choice>
o Sequence: Child element can occur only in specified order.
<xs:sequence>
<xs:element name=”first” type=”xs:string”/>
<xs:element name=”middle” type=”xs:string”/>
<xs:element name=”last” type=”xs:string”/>
</xs:sequence>
Occurence Indicators
o maxOccurs - Child element can occur only maxOccurs number of times.
o minOccurs - Child element must occur minOccurs number of times.
Group Indicators
o Group: a set of related elements can be created using this indicator. the general form for
creating an element group is as follows:
Syn: <xs:group name=”group-name”> … </ xs:group>
Ex: <xs:group name=”personInfo”>
<xs:element name=”first” type=”xs:string”/>
<xs:element name=”middle” type=”xs:string”/>
<xs:element name=”last” type=”xs:string”/>
</ xs:group>
o attributeGroup: XML Schema provides this element, which is used to group a set of
attributes declarations so that they can be incorporated into complex types definitions with
syntax:
Syn: <xs:attributeGroup name=”group-name”> … </ xs:attributeGroup>
Ex: <xs: attributeGroup name=”personInfo”>
<xs:element name=”first” type=”xs:string”/>
<xs:element name=”middle” type=”xs:string”/>
<xs:element name=”last” type=”xs:string”/>
</ xs: attributeGroup>
www.Jntufastupdates.com 13
P a g e | 14
www.Jntufastupdates.com 14
P a g e | 15
www.Jntufastupdates.com 15
P a g e | 16
generate new XML documents from one or more existing XML documents for processing or sharing of data
between different applications. One possible solution is to generate separate XML document such that the
former contains only insensitive data. XSLT comes into play in this scenario.
XSLT, Extensible Stylesheet Language Transformations provides the ability to transform XML
data from one format to another automatically. An XSLT stylesheet is used to define the transformation
rules to be applied on the target XML document. XSLT stylesheet is written in XML format. XSLT
Processor takes the XSLT stylesheet and apply the transformation rules on the target XML document and
then it generates a formatted document in form of XML, HTML or text format. This formatted document
then is utilized by XSLT formatter to generate the actual output which is to be displayed to the end user.
1. Stylesheet strcture:
XSLT files are themselves XML documents and hence must follow the well-formedness
constraints. The W3C defined the exact syntax of an XSLT 2.0 document by XML schema. XSLT file starts
with XML declaration. Every XSLT file must have either <stylesheet> or <transform> as root element.
Following are simple structure of XSLT document:
<?xml version=”1.0”?>
<xsl:stylesheet version=”2.0” xmlns:xsl=”http://www.w3.org/1999/XSLT/Transform”>
…
</ xsl:stylesheet>
Or
<?xml version=”1.0”?>
<xsl:transform version=”2.0” xmlns:xsl=”http://www.w3.org/1999/XSLT/Transform”>
…
</ xsl: transform >
These elements must have the attribute version and namespace attribute xmlns. Version attribute
indicate version of XSLT being used. Namespace attribute distinguishes XSLT elements from other
elements. There are different ways to apply XSLT document to XML document. One way to add link to
XML document which points to actual XSLT files and lets the browsers do transformation with following
declaration:
<?xml version =”1.0”?>
www.Jntufastupdates.com 16
P a g e | 17
2. XSLT Elements:
An XSLT file contains elements, which instruct processor how an XML document is to be
transformed. It may contain elements that are not defined by XSLT. In such cases, XSLT processor does not
process these non-XSLT elements and add them to the ouput in the same order they occurred in the source
XSLT document. This means that the transformed XML document may use original mark-ups as well as
new mark-ups.
Element Description
stylesheet Defines the root element of a style sheet
transform Defines the root element of a style sheet
template Rules to apply when a specified node is matched
apply-templates Applies a template rule to the current element or to the current element's child nodes
call-template Calls a named template
element Creates an element node in the output document
variable Declares a local or global variable
param Declares a local or global parameter
value-of Extracts the value of a selected node
attribute Adds an attribute
attribute-set Defines a named set of attributes
if Contains a template that will be applied only if a specified condition is true
choose Used in conjunction with <when> and <otherwise> to express multiple conditional tests
when Specifies an action for the <choose> element
for-each Loops through each node in a specified node set
Imports the contents of one style sheet into another. Note: An imported style sheet has
import
lower precedence than the importing style sheet
Includes the contents of one style sheet into another. Note: An included style sheet has
include
the same precedence as the including style sheet
sort Sorts the output
processing-
Writes a processing instruction to the output
instruction
comment Creates a comment node in the result tree
copy Creates a copy of the current node (without child nodes and attributes)
copy-of Creates a copy of the current node (with child nodes and attributes)
3. XSLT templates:
An XSLT document is all about template rules. A template specifies rule and instruction, which is
executed when rule matches. The rule is specified by XSLT <template> element. It has attribute match,
which specifies pattern. The value of match attribute is subset of expression.
www.Jntufastupdates.com 17
P a g e | 18
www.Jntufastupdates.com 18
P a g e | 19
<xsl:template name=”header”>
<title>XSLT</title>
</xsl:template>
4. Selecting values:
The value of a node can also be added using <value-of> element. Value of node depends on type
of the node. For example, the value of text node is the text itself, whereas the value of element node is
concatenation of values of all text descendents. If multiple nodes are selected by select attribute, value is
concatenation of values of those selected attributes. Consider simple XML document:
<book>
<title>Web Technologies</title>
</book>
One can now extract the value of title element using <value-of> elemement as follows:
<xsl:template match=”/”>
Title: <xsl:value-of select=”book/title”/>
</xsl:template>
This XSLT file, on applying previous XML document produces following result:
Title: Web Technologies
Values of different node types:
Text: text of node
Element: concatenation of values of all text descendants
Attribute: attribute value without quotation marks
Namespace: the URI of the namespace
Comment: anything between <!--and -->
Processing instruction: anything between <? and ?>
XSLT has another element <copy-of>, which returnas all selected elements including nested
elements and text. Consider the following XSLT document.
<xsl:template match=”/”>
<xsl:copy-of select=”.”/>
</xsl:template>
When we apply this XSLT document to any XML document, it produces the same XML
document. This is because, when root element (/) is selected, <copy-of> copies root element together with
all child elements recursively.
www.Jntufastupdates.com 19
P a g e | 20
This code calls template add with parameters 2 and 4. If this XSLT applied to XML document the
output will be 6. The scope of forrmal is with in the template only. XSLT allows to declare anduse
variable. Consider the following code:
<xsl:template>
<xsl:variable name=”a”>4</xsl:variable>
<xsl:variable name=”b”> 6</xsl:variable>
<xsl:value-of select=”$a+$b”/>
</xsl:template>
6. Conditional Processing:
There are two types of branching constructs in XSLT: <if> and <choose>
www.Jntufastupdates.com 20
P a g e | 21
</xsl:choose>
6. Repetition:
XSLT allows <for-each> construct, which can be used to process set of instructions repetedly for
different items in sequence. The attribute select evaluates sequence of nodes. For each of telements in this
sequence, instruction under <for-each> element are processed. Consider the following XML file result.xml,
containing marks of different students:
<result>
<student><rollno>01</rollno><marks>80</marks></student>
<student><rollno>02</rollno><marks>70</marks></student>
<student><rollno>03</rollno><marks>60</marks></student>
<student><rollno>04</rollno><marks>55</marks></student>
<student><rollno>05</rollno><marks>77</marks></student>
</result>
The following XSLT document displays results of the students:
<xsl:for-each select=”result”>
Roll No: <xsl:value-of select =”rollno”/><br>
Marks: <xsl:value-of select =”marks”/><br>
</xsl:for-each>
www.Jntufastupdates.com 21
P a g e | 22
Processing instruction is added in the result using <processing-instruction> element. The most
popular use of this element is to insert the <stylesheet> element in output HTML/XML document with
syntax.
<xsl:processing-instruction name=”xml-stylesheet”>
<xsl:text> href=”sort.xsl” type=”text/xsl”</xsl:text>
</xsl:processing-instruction>
7.5 Creating comments:
Comment is added using <comment> element as follows:
<xsl:comment>This is XSLT document</xsl:document>
8. Grouping nodes:
XSLT allows us to group related items based on common values. Consider the following XML document.
<result>
<student><rollno>01</rollno><marks>80</marks><dept>IT</dept></student>
<student><rollno>02</rollno><marks>70</marks><dept>IT</dept></student>
<student><rollno>03</rollno><marks>60</marks><dept>CSE</dept></student>
<student><rollno>04</rollno><marks>55</marks><dept>IT</dept></student>
<student><rollno>05</rollno><marks>77</marks><dept>CSE</dept></student>
</result>
The following XSLT document displays results of the students as groups by dept:
<xsl:template match=”/result”>
<xsl:for-each-group select=”student” group-by=”@dept”>
<xsl:value-of select=”current-grouping-key()” />
<xsl:for-each select=”current-group()”>
<xsl:value-of select=”@rollno”/>
</xsl:for-each>
</xsl:template>
This enumerates group items based either on common value of grouping key or pattern specified
by group-by attribute. The current-group() function returns the current group item in the iteration and current-
grouping-key() returns commn key of current group.
9. Sorting nodes:
We can sort group of similar elements using <sort> element. The attributes of the <sort> element
describe how to perform sorting. For example, sorting can be doen alphabetically or numerically or in
increasing or decreating order. The attribute select is used to specify sorting key. The order attribute
specifies order and can have values accending or decending. The type of data to be sorted can be specified
using attribute data-type. Following example sorts list of student respect to their marks.
<table><xsl:for-each select=”/result”>
<xsl:sort select=”marks” data-type=”number”/>
<tr><td><xsl:value-of select=”rollno”/></td>
<td><xsl:value-of select=”marks”/></td>
<td><xsl:value-of select=”dept”/></td></tr>
</xsl:for-each></table>
10. Functions:
XSLT also allows custom functions to be defined in stylesheet. A function is defined using
<function> element. It has attribute name, which specifies the name of the function. Once function is
defined, it can be called from any expressin. The function name must have prefix. This is required to avoid
conflict with any function from default namespace. A prefix can not be bound to reserved namespace.
<xsl:function name=”f:fact”>
<xsl:param name=”n”>
www.Jntufastupdates.com 22
P a g e | 23
12. Numbering:
The <number> element allows to insert and format number into the result tree.
<xsl:template match=”/result”>
<xsl:for-each-group select=”student” group-by=”@dept”>
<xsl:number value=”position()”/>
<xsl:value-of select=”current-grouping-key()” />
<xsl:for-each select=”current-group()”>
<xsl:number value=”position()”/>
<xsl:value-of select=”@rollno”/>
</xsl:for-each>
</xsl:template>
www.Jntufastupdates.com 23
P a g e | 24
This portion defines the basic set of interfaces and objtects for any structured document.
2. HTML DOM:
The HTML Document Object Model (DOM) is a programming API for HTML documents. It defines
the logical structure of documents and the way a document is accessed and manipulated. With the Document
Object Model, programmers can create and build documents, navigate their structure, and add, modify, or
delete elements and content. Anything found in an HTML document can be accessed, changed, deleted, or
added using the Document Object Model, with a few exceptions - in particular, the DOM interfaces for the
internal subset and external subset have not yet been specified.
<TABLE>
<ROWS>
<TR>
<TD>Shady Grove</TD>
<TD>Aeolian</TD>
</TR>
<TR>
<TD>Over the River, Charlie</TD>
<TD>Dorian</TD>
</TR>
</ROWS>
</TABLE>
3. XML DOM:
According to the DOM, everything in an XML document is a Node. The DOM says: The entire
document is a document node, Every XML element is an element node, The text in the XML elements are
text nodes, Every attribute is an attribute node, Comments are comment nodes.
<bookstore>
<book category=”cooking”>
<title lang=”en”>Everday Italian</title>
<author>Giada De Laurentiis</author
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
The root node in the XML above is named <bookstore>. All other nodes in the document are
contained within <bookstore>. The root node <bookstore> holds four <book> nodes. The first <book> node
holds four nodes: <title>, <author>, <year>, and <price>, which contains one text node each, "Everyday
Italian", "Giada De Laurentiis", "2005", and "30.00". The XML DOM views an XML document as a tree-
structure. The tree structure is called a node-tree. All nodes can be accessed through the tree. Their contents
can be modified or deleted, and new elements can be created.
The node tree shows the set of nodes, and the connections between them. The tree starts at the root
node and branches out to the text nodes at the lowest level of the tree. The nodes in the node tree have a
hierarchical relationship to each other. The terms parent, child, and sibling are used to describe the
relationships. Parent nodes have children. Children on the same level are called siblings (brothers or sisters).
In a node tree, the top node is called the root, Every node except the root has exactly one parent node, A
www.Jntufastupdates.com 24
P a g e | 25
node can have any number of children, A leaf is a node with no children, Siblings are nodes with the same
parent.
www.Jntufastupdates.com 25
P a g e | 26
www.Jntufastupdates.com 26
P a g e | 27
ContentHandler Interface
This interface specifies the callback methods that the SAX parser uses to notify an application program of
the components of the XML document that it has seen.
void startDocument() - Called at the beginning of a document.
void endDocument() - Called at the end of a document.
void startElement(String uri, String localName, String qName, Attributes atts) - Called at the
beginning of an element.
void endElement(String uri, String localName,String qName) - Called at the end of an element.
void characters(char[] ch, int start, int length) - Called when character data is encountered.
void ignorableWhitespace( char[] ch, int start, int length) - Called when a DTD is present and
ignorable whitespace is encountered.
void processingInstruction(String target, String data) - Called when a processing instruction is
recognized.
www.Jntufastupdates.com 27
P a g e | 28
www.Jntufastupdates.com 28
P a g e | 29
www.Jntufastupdates.com 29
AJAX (Asynchronous JavaScript and XML)
AJAX is an acronym for Asynchronous JavaScript and XML. It is a group of inter-related
technologies like javascript, dom, xml, html, css etc. AJAX allows you to send and receive data
asynchronously without reloading the entire web page. So it is fast.
AJAX allows you to send only important information to the server not the entire page. So only
valuable data from the client side is routed to the server side. It makes your application interactive and
faster.
Where it is used?
There are too many web applications running on the web that are using AJAX Technology. Some
are:
1. Gmail
2. Facebook
3. Twitter
4. Google maps
5. YouTube etc.,
As you can see in the above image, full page is refreshed at request time and user is
blocked until request completes. Let's understand it another way.
www.Jntufastupdates.com 30
Unit-3 AJAX
As you can see in the above image, full page is not refreshed at request time and user gets
response from the AJAX Engine. Let's try to understand asynchronous communication by the
image given below.
AJAX Technologies
AJAX is not a Technology but group of inter-related technologies. AJAX Technologies includes:
HTML/XHTML and CSS
DOM
XML or JSON(JavaScript Object Notation)
XMLHttpRequest
JavaScript
DOM
It is used for dynamic display and interaction with data.
XML or JSON
For carrying data to and from server. JSON is like XML but short and faster than XML.
XMLHttpRequest
For asynchronous communication between client and server.
www.Jntufastupdates.com 31
2
Unit-3 AJAX
JavaScript
It is used to bring above technologies together. Independently, it is used mainly for client-side
validation.
Understanding XMLHttpRequest
An object of XMLHttpRequest is used for asynchronous communication between client and
server. It performs following operations:
1. Sends data from the client in the background
2. Receives the data from the server
3. Updates the webpage without reloading it.
Method Description
void open(method, URL) Opens the request specifying get or post method
and url.
void open(method, URL, async) Same as above but specifies asynchronous or not.
void open(method, URL, async, Same as above but specifies username and
username, password) password.
1. User sends a request from the UI and a javascript call goes to XMLHttpRequest object.
2. HTTP Request is sent to the server by XMLHttpRequest object.
3. Server interacts with the database using JSP, PHP, Servlet, ASP.net etc.
4. Data is retrieved.
5. Server sends XML data or JSON data to the XMLHttpRequest callback function.
6. HTML and CSS data is displayed on the browser.
www.Jntufastupdates.com 32
3
Unit-3 AJAX
Example
First name:
Suggestions:
Example Explained
In the example above, when a user types a character in the input field, a function called
"showHint()" is executed.
Example
<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
www.Jntufastupdates.com 33
4
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gethint.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
Code explanation:
First, check if the input field is empty (str.length == 0). If it is, clear the content of the txtHint
placeholder and exit the function.
<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
www.Jntufastupdates.com 34
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";
$hint = "";
www.Jntufastupdates.com 35
Introduction to Web Services
Technology keep on changing, users were forces to learn new application on continuous basis.
With internet, focus is shifting to-wards services based software. Users may access these services using
wide range of devices such as PDAs, mobile phones, desktop computers etc. Service oriented software
development is possible using man known techniques such as COM, CORBA, RMI, JINI, RPC etc. some
of them are capable of delivering services over web & some or not. Most of these technologies uses
particular protocols for communication & with no standardization. Web service is the concept of creating
services that can be accessed over web. Most of these
Web services receive information from clients as messages, containing instructions about what client
wants, similar to method calls with parameters. These message delivered by web services are encoded
using XML.XML enabled web services are interoperable with other web services.
www.Jntufastupdates.com 36
SOAP Encoding Rules: defines set of rules for exchanging instances of application defined data types
SOAP RPC Representation: defines convention for representing remote procedure calls & response
SOAP can be used in combination with variety of existing internet protocols & formats including
HTTP, SMTP etc. Typical SOAP message is shown below:
<IVORY:Envelope xmlns:IVORY=”http://schemas.xmlsoap.org/soap/envelope”
IVORY:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding”>
<IVORY:Body>
<m:GetLastTradePrice xmlns:m=”Some-URI”>
<symbol>DIS</symbol>
</m:GetLastTradePrice>
</IVORY:Body>
</IVORY:Envelope>
The consumer of web service creates SOAP message as above, embeds it in HTTP POST request
& sends it to web service for processing:
<SOAP-ENV:Envelope xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope”
SOAP-ENV:encodingStyle=” http://schemas.xmlsoap.org/soap/encoding” />
<SOAP-ENV:Body>
<m:GetLastTradePrice xmlns:m=”Some-URI”>
<Price>34.5</Price>
</m:GetLastTradePrice>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Interoperability:
The major goal in design of SOAP was to allow for easy creation of interoperable distributed web
services. Few details of SOAP specifications are open for interpretation; implementation may differ across
different vendors. SOAP message though it is conformant XML message, may not strictly follow SOAP
specification.
Implementations:
SOAP technology was developed by DevelopMentor, IBM, Lotus, Microsoft etc. More than 50
vendors have currently implemented SOAP. Most popular implementations are by Apache which is open
source java based implementation & by Microsoft in .NET platform. SOAP specification has been
submitted to W3C, which is now working on new specifications called XMLP (XML Protocol)
www.Jntufastupdates.com 37
MIME structure. This multi part structure is called as SOAP Message Package. This new specification
was developed by HP & Microsoft. Sample SOAP message attachment is shown here:
MIME-Version: 1.0
Content-Type: Multipart/Related; boundary=MIME_boundary;
type=text/xml; start=”<[email protected]>”
Content-Description: This is the optional message description.
--MIME_boundary
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <[email protected]>
<?xmll version= ‟ 1.0‟ ?>
If we are creating web service that offered latest stock quotes, we need to create WSDL file on
server that describes service. Client obtains copy of this file, understand contract, create SOAP request
based on contract & dispatch request to server using HTTP post. Server validates the request, if found
valid executes request. The result which is latest stock price for requested symbol is then returned to client
as SOAP response.
WSDL Document:
WSDL document is an XML document that contains of set of definitions. First we declare
name spaces required by schema definition:
<schema xmlns=”http://www.w3.org/2000/10/XMLSchema”
xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/”
targetNameSpace=http://schemas.xmlsoap.org/wsdl/ elementFormDefault=”qualified”>
The name attribute is optional & can serve as light weight form of documentation. The nmtoken
represents name token that are qualified strings similar to CDATA, but character usage is limited to letters,
digits, underscores, colons, periods & dashes. A targetNamespace may be specified by providing uri. The
import tag may be used to associate namespace with document locations. Following code segment shows
how declared namespace is associated with document location specified in import statement:
<definitions name=”StockQuote”
targetNameSpace=”http://example.com/stockquote/defiinitions”
www.Jntufastupdates.com 38
xmlns:tns=”http://example.com/stockquote/definitions”
xmlns:xsdl=”http://example.com/stockquote/schemas”
xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/‟
xmlns=”http://schemas.xmlsoap.org/wsdl/”>
<import namespace=”http://example.com/stockquote/schemas”
Location=”http://example.com/stockquote/stockquote.xsd”/>
The wsdl:documentation tag is optional as in case of definitions. The xsd type system may be used
to define types in message. WSDL allows type systems to be added via extensibility element.
Message Element: It represents abstract definition of data begin transmitted. Syntax for message element:
<wsdl:message name=”nktoken”> *
<wsdl;documentation …/>
<part name=”nmtoken” element=”qname”? type=”qname”? /> *
</wsdl:message>
The message name attribute is used for defining unique name for message with in
document scope. The wsdl:documentation is optional & may be used for declaring human
readable documentation. The message consists of one or more logical parts. The part describes
logical abstract content of message. Each part consists of name & optional element & type
attributes.\
Port Type Element: It defines set of abstract operations. An operation consists of both input &
output messages. The operation tag defines name of operation, input defines input for operation &
output defines output format for result. The fault element is used for describing contents of SOAP
fault details element. It specifies abstract message format for error messages that may be output as
result of operation:
<wsdl:portType name=”nmtoken”>*
<wsdl:documentation …./>?
<wsdl:operation name=”nmtoken”>*
<wsdl:documentation …./>?
<wsdl:input name=”nmtoken”? message=”qname”>?
<wsdl:documentation …./>?
</wsdl:input>
<wsdl:output name=”nmtoken”? message=”qname”>?
<wsdl:documentation …./>?
</wsdl:output>
<wsdl:fault name=”nmtoken”? message=”qname”>?
<wsdl:documentation …./>?
</wsdl:fault>
</wsdl:operation>
</wsdl:portType>
Binding Element: It defines protocol to be used & specifies data format for operations & messages defined
by particular portType. The full syntax for binding is given below:
www.Jntufastupdates.com 39
<wsdl:binding name=”nmtoken” type=”qname”> *
<wsdl:documentation …./>?
<--Extensibility element -->*
<wsdl:operation name=”nmtoken”>*
<wsdl:documentation …./>?
<--Extensibility element -->*
<wsdl:input> ?
<wsdl:documentation …./>?
<--Extensibility element -->*
</wsdl:input>
<wsdl:output> ?
<wsdl:documentation …./>?
<--Extensibility element -->*
</wsdl:output>
<wsdl:fault name=”nmtoken”> *
<wsdl:documentation …./>?
<--Extensibility element -->*
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
The operation in WSDL file can be document oriented or remote procedure call (RPC)
oriented. The style attribute of <soap:binding> element defines type of operation. If operation is
document oriented, input & output messages will consist of XML documents. If operation is RPC
oriented, input message contains operations input parameters & output message contains result of
operation.
Port Element: It defines individual end point by specifying single address for binding:
<wsdl:port name=”nmtoken” binding=”qname‟> *
<--Extensibility element (1) -->
</wsdl:port>
The name attribute defines unique name for port with current WSDL document. The binding
attribute refers to binding & extensibility element is used to specify address information for port.
Service Element: it aggregates set of related ports. Each port specifies address for binding:
<wsdl:service name=”nmtoken”> *
<wsdl:documentation …./>?
<wsdl:port name=”nktoken” binding=”qname”> *
<wsdl:documentation …/> ?
<--Extensibility element -->
</wsdl:port>
<--Extensibility element -->
</wsdl:service>
www.Jntufastupdates.com 40
Unit-3 AJAX
Registry itself holds information about business such as company name, contact etc. it holds both
descriptive & technical information about web service. It provides search facilities that allow to search
specific industry segment or geographic location.
Implementation:
This is global, public registry called UDDI business registry. It is possible for individuals to set up
private UDDI registries. The implementations for creating private registries are available from IBM, Idoox
etc. Microsoft has developed UDDI SDK that allows visual basic programmer to write program code to
interact with UDDI registry. The use of SDK greatly simplifies interaction with registry & shields
programmer from local level details of XML & SOAP.
It defines registry & repository where business can register themselves by providing their contact
information, address & so on. Such information is called Core Component. After business has registered
with ebXML registry, other partners can look up registry to locate that business. Once business partner is
located, the core components of located business are downloaded. Once buyer is satisfied with fact that
seller service can meet its requirements, it negotiates contract with seller. Such collaborative partner
agreements are defined in ebXML. Once both parties agree on contract terms, sign agreements &
collaborative business transaction by exchanging their private documents. ebXML provides marketplace &
defines several XML based documents for business to join & transact in such marketplace.
www.Jntufastupdates.com 41