WT Unit 3
WT Unit 3
Technologies
XML
UNIT-3
Extensible Markup
Language
(XML)
Contents
XML: AJAX A New Approach:
Document type Definition Introduction to AJAX
XML schemas Integrating PHP and AJAX
Document object model
XSLT
DOM and SAX Approaches
Objectiv
es
Learning about types of DTD, DOM, SAX
Creation of Schema in XML
Learning about AJAX
Integrating PHP and AJAX
Introductio
n Language.
❖ XML stands for Extensible Markup
XML is a markup language much like HTML.
❖ XML was designed to carry data, not to display data.
XML tags are not predefined. You must define your own
tags.
❖ XML is designed to be self-descriptive.
❖ XML is a W3C Recommendation.
❖ XML was designed to transport and store data where
HTML was designed to display data
Rules for writing XML
oDocument:
In XML the basic entity is an element. The elements are
used for defining the tags
o The elements typically consists of opening and closing tag
o XML is a case sensitive language. For example
< name > Vignan < /Name > is not allowed
o In XML each start tag must have matching end tag.
o The XML elements in XML must be properly nested
<name>
<firstname></firstname>
<lastname></lastname>
</name>
o A space or tab character is not allowed in the elem“>”ent name or in
attribute name.
o To insert “<” or “>” or “ ’ ” or “&” or “ ”” ” characters with in text
simply use “<”,, ”&apos”, “&”, “"” respectively.
o The syntax for writing comments in XML is similar to HTML. For
example
<car>
<price>200000</price> Child
<color>red</color> Elements
</car>
Container
Element
<car>
<price>300000</price>
<color>green</color>
</car>
</vehicles>
XML CSS: We can also apply styles to XML document using CSS. This can be
done as follows
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="library.css"?>
<catalog>
<book> catalog
<title>XML Bible</title> {
<author>Winston</author> font-family:arial;
</book> color:red;
<book> font-size:16pt;
<title>AI</title> }
<author>S.Russel</author> book,author
</book> {
</catalog> font-family:Arial;
color:black;
font-size:14pt;
}
Document type
● Definition
The Document type definition is used to define the basic building blocks of
any XML document
● Using DTD we can specify the various elements types, attributes. DTD
specifies set of rules for structuring data in XML file
● Various building blocks of XML are as follows:
❏ Elements
❏ Attribute
❏ CDDATA
❏ PCDATA
Elements:
➢ The basic entity is element
➢ The elements are used for defining tags
➢ The elements consist of opening tag and closing tag
Attribute:
➢ The attributes are used to specify the values of the element
➢ These are specified with in double quotes
CDDATA:
➢ CDDATA stands for character data
PCDATA:
➢ PCDATA stands for Parsed Character Data
Types of DTD
Internal External
❏ String ❏ Numeric
❏ Date ❏ Boolean
Example:
<xs:element name="lastname" type="xs:string"/>
Example
student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="student" type="xs:string"></xs:element>
</xs:schema>
student.xml
<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation ="student.xsd">Reehana </student>
Date Data Type:
● Date data type is used to specify the date
● The format of this date is yyyy-mm-dd denotes year, month and date
student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="dob" type="xs:date"></xs:element>
</xs:schema>
student.xml
<?xml version="1.0" encoding="UTF-8"?>
<dob xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation ="student.xsd">1991-8-31 </dob>
Numeric Data Type:
To represent decimal or integer values Numeric Data Type is used
student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="percentage" type="xs:decimal"></xs:element>
</xs:schema>
student.xml
<?xml version="1.0" encoding="UTF-8"?>
<percentage xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation ="student.xsd">99.9 </percentage>
Boolean Data Type:
● For specifying true or false Boolean data type is used
student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="flag" type="xs:boolean"></xs:element>
</xs:schema>
student.xml
<?xml version="1.0" encoding="UTF-8"?>
<flag xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation ="student.xsd">99.9 </flag>
Differences between XML Schema and DTD
XML Schema DTD
➢ XML schema’s are suitable for large ➢ DTD’s are suitable for small
applications applications
➢ XML schema’s support for ➢ DTD’s does not support
defining the type of data
➢ XML schema has large number of built ➢ DTD’s has limited number of data
in and derived data types. types
➢ support namespace ➢ does not support namespace
Document Object
● Model
The Document Object Model (DOM) is an application programming interface (API)
for valid HTML and well-formed XML documents.
● It defines the logical structure of documents and the way a document is accessed and
manipulated
● With the Document Object Model Programmers can build documents, navigate their
structure, and add, modify, or delete elements and content.
● Anything found in an HTML or XML document can be accessed, changed, deleted, or
added using the Document Object Model.
● It is the W3C recommendation for handling the
structured documents.
● In DOM everything from XML is treated as a
node.
The DOM is a programming API for documents. It is based on an object structure that
closely resembles the structure of the documents it models.
DOM1 This version was issued in 1998 which was focused on XHTML and XML.
DOM2 This version was issued in 2000 that could specify the style sheet.
It also support the event model and traversal with in documents.
DOM# This is current release of DOM specification published in 2004. This version could
deal with XML with DTD and Schema, document validations, document views and
formatting.
❏ Basically Document Object Model (DOM) is an application programming
interface (API) between XML and application programs
❏ The DOM contains set of interfaces for document tree node type
❏ These interfaces are similar to the Java or C++ interfaces
❏ They have objects, properties, methods which are useful for respected node type of
web document
DOM Tree
➢ The documents in DOM are represented using a tree like structure in which
every element is represented as a node
➢ Hence the tree structure is also referred as DOM tree.
Document
<head> <body>
<html>
<head> <title>This is my web page</title>
</head>
<body>
<form name=“form1”>
<input type=”text” name=”myinput”/>
</form>
</body>
</html>
Method 1:
● Every document element is associated with some address.
● This address is called DOM address
● The document has the collection of forms and elements
Hence we can refer the element as
var DOM_Obj=document[0].elements[0];
var DOM_Obj=document.form1.myinput;
● But this may cause a validation problem because XHTML1.1 does not
support name attribute to the form element
● Hence use another method
Method 3:
● We can access the desired element from the web document using JavaScript
method getElementById
● This method is defined in DOM1
For getting values of these checkboxes we can write the following code.
var DOM_Obj=document. getElementById(“Food”);
for(i=0;i<Dom_Obj.vegetables.length;i++)
document.write(Dom_Obj.vegetables[i]+”<br/>”);
XSLT
● XSL stands for Extensible Stylesheet Language, and is a style sheet
language for XML Documents
● XSL transform XML documents into other formats.
● XSLT stands for XSL Transformations
● In this we will learn how to use XSLT to transform XML documents into
other formats, like XHTML
<?xml version="1.0" encoding=“UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<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>
XSL Code: <xsl:for-each select="catalog/cd">
<xsl:stylesheet version="1.0" <tr>
xmlns:xsl="http://www.w3.org/1999/XSL/ <td><xsl:value-of select="title"/></td>
Transform"> <td><xsl:value-of select="artist”/></td>
<xsl:template match="/"> </tr>
<html> </xsl:for-each>
<body> </table>
<h2>My CD Collection</h2> </body>
<table border="1"> </html>
<tr bgcolor="#9acd32"> </xsl:template>
<th>Title</th> </xsl:stylesheet>
<th>Artist</th>
</tr>
SAX (Simple API for
XML)from top to bottom, recognizing the tokens that make up
➢ Reads an XML document
● SAX is an event-based parser for XML documents.
a wellformed XML document.
● Unlike
➢ Tokens area processed
DOM parser, a SAX
in the sameparser
order creates noappear
that they parse tree
in the document.
➢●Reports
SAX isthe a streaming interface
application for XML
program the nature of tokens that the parser has
Applications using
encountered as they occur.SAX receive event notifications about the XML
➢document being processed
The application programanprovides
element,anand attribute,
"event" at a time
handler in sequential
that must be registered with
order starting
the parser. at the top of the document, and ending with the closing of the
➢ROOT
As theelement.
tokens are identified, callback methods in the handler are invoked with the
relevant information.
When to Use?
You should use a SAX parser when −
➢ You can process the XML document in a linear fashion from top to down.
➢ The document is not deeply nested.
➢ You are processing a very large XML document whose DOM tree would
consume too much memory
➢ Typical DOM implementations use ten bytes of memory to represent one byte
of XML.
➢ The problem to be solved involves only a part of the XML document.
➢ Data is available as soon as it is seen by the parser, so SAX works well for an
XML document that arrives over a stream.
Disadvantages of SAX
We have no random access to an XML document since it is processed in a
forward-only manner.
If you need to keep track of data that the parser has seen or change the order of
items, you must write the code and store the data on your own.
This XML document, when passed through a SAX parser, will generate a sequence
of events like the following:
getinfo.php $fr=($b->childNodes);
<?php for($i=0;$i<$fr->length;$i++)
$q=$_GET[“q”]; {
$xmlDoc=new DOMDocument(); //going through other elements
$xmlDoc->load(“FriendNames.xml”); if($fr->item($i)->nodeType==1)
$a=$xmlDoc->getElementsByTagName(‘name’); {
for($i=0;$i<=$a->length-1;$i++) echo(“<b>”.$fr->item($i)->nodeName.”:</b>”);
{ echo($fr->item($i)->childNodes->item(0)-
//going through elements >nodevalue);
if($a->item($i)->nodeType==1) echo(“<br/>”);
{ }
if($a->item($i)->childNodes->item(0)->nodevalue==$q) }
{ ?>
$b=($a->item($i)->parentNode);
}
}
}
Step4: The XML file which is handled by the PHP in the above step is:
FriendNames.xml
<?xml version=”1.0”?> <Info>
<Friend> <name>Raj</name>
<Info> <phone>333333333</phone>
<name>chitra</name> <email>[email protected]</email>
<phone>1111111111</phone> <hobby>photography</hobby>
<email>[email protected]</email> <Info>
<hobby>singing</hobby> </Friend>
</Info>
<Info>
<name>priyanka</name>
<phone>2222222222</phone>
<email>[email protected]</email>
<hobby>Reading</hobby>
</Info>
Step5: For getting the output we will open the HTML
file (Created in step 1) in browser window