WIT - Course Material - Unit-4
WIT - Course Material - Unit-4
COURSE MATERIAL
UNIT 4
COURSE B.TECH
SEMESTER 32
Version V-1
BTECH_CSE-SEM 32
SVCE TIRUPATI
9 PRACTICE QUIZ 41
10 ASSIGNMENTS 42
11 PART A QUESTIONS & ANSWERS (2 MARKS QUESTIONS) 42
12 PART B QUESTIONS 44
13 SUPPORTIVE ONLINE CERTIFICATION COURSES 44
14 REAL TIME APPLICATIONS 44
15 CONTENTS BEYOND THE SYLLABUS 44
16 PRESCRIBED TEXT BOOKS & REFERENCE BOOKS 45
17 MINI PROJECT SUGGESTION 45
BTECH_CSE-SEM 32
SVCE TIRUPATI
1. Course Objectives
The objectives of this course is to
• To introduce client side scripting with Javascript and DHTML
• To introduce server side programming with Java servlets, JSP and PHP.
• To learn the basic web concepts and Internet protocols
2. Prerequisites
Students should have knowledge on
1. Object Oriented Programming through Java.
2. SQL QURIES.
3. Syllabus
UNIT IV
Creating and Using Forms Understanding Common Form Issues, GET vs. POST, Validating
form input, Working with multiple forms, and Preventing Multiple Submissions of a form.
XML: Basic XML- Document Type Definition XML Schema DOM and Presenting XML, XML
Parsers and Validation, XSL and XSLT Transformation, News Feed (RSS and ATOM).
4. Course outcomes
1. Student must be able to apply a web page and identify its elements and attributes.
2. Student must be able to analyze web pages using XHTML and Cascading Styles sheets
3. Student should be able design dynamic web pages.
4. Student must be able implement web applications using PHP.
CO1 3 2 2 3
CO2 3 2 3 2
CO3 3 2 3 2 3
CO4 3 2 3 3 2
1||WIT-UNIT -IV
BTECH_CSE-SEM 32
SVCE TIRUPATI
6. Lesson Plan
2||WIT-UNIT -IV
BTECH_CSE-SEM 32
SVCE TIRUPATI
GET vs. POST, Validating form input
3 T1, R1
Working with multiple forms, and Preventing Multiple Submissions of a
4 form. T1, R1
8. Lecture Notes
BTECH_CSE-SEM 32
SVCE TIRUPATI
TEXTAREA - A larger box that can contain paragraph-style entries
FILE - An element that allows you to browse your computer for a file
SUBMIT - A button that will submit the form
RESET - A button that will reset the form to its original state
A well-designed form, divides itself into logical chunks using the < fieldset >
element. Each chunk gets a title, courtesy of the < legend > element.
The following Example demonstrate the forms
registration.html
<html>
<head>
<title>Registration form</title>
</head>
<body>
<h1>Registration Form</h1>
<form name=―registration‖ method=―‖ action=‖‖>
<fieldset>
<legend>Personal Information</legend>
Name : <input type='text' name='name' placeholder="Name As Per
SSC"><br>
Father Name: <input type=―text‖ name=―fname‖ placeholder=―Name As
Per SSC‖ ><br> Gender: <select id="gender">
<option value="female">Female</option>
<option value="male">Male</option>
</select><br>
Age: <input id="age" type="number" min="0" max="120"><br> </fieldset>
<fieldset>
<legend>Contact Details</legend> Telephone: <input id="telephone"><br>
Email: <input type='text' id="email"><br>
</fieldset>
<fieldset>
<legend>Pick Your Favorite Animals</legend>
<input id=―cow" type="checkbox"> Cow
<input id="cat" type="checkbox"> Cat
<input id=―dog" type="checkbox"> Dog
<input id="elephant" type="checkbox"> Elephant
</fieldset>
<p><input type="submit" value="Submit Application"></p>
</form>
4||WIT-UNIT -IV
BTECH_CSE-SEM 32
SVCE TIRUPATI
</body>
</html>
BTECH_CSE-SEM 32
SVCE TIRUPATI
<title>Example for GET</title>
</head>
<body>
<?php
if (trim ($_GET['uname']) != "" && trim ($_GET['pwd']) != ""){ echo "Your User
Name (with GET): " . $_GET['uname'];
echo "<br>Your password (with GET) : ". $_GET['pwd'];
} else {
echo "You must submit a value.";
}
?><br /><a href="get.php">Try Again</a>
</body>
</html>
6||WIT-UNIT -IV
BTECH_CSE-SEM 32
SVCE TIRUPATI
</head>
<body>
<form action="post-demo.php" method="post">
<p>POST Example:</p>
<input type="hidden" name="submitted" value="yes" />
User Name: <input type="text" name="uname" maxlength="150" /><br /><br
/> Password: <input type="password" name="pwd" maxlength="150" /><br />
<input type="submit" value="Submit with POST" style="margin-top: 10px;" />
</form>
</body>
</html>
7||WIT-UNIT -IV
BTECH_CSE-SEM 32
SVCE TIRUPATI
Hitting the Refresh button after submitting data using the POST form, the
browser will ask you if you want to resubmit the data that was passed to it
previously. If you want to resend the data, you must select Yes (Resend) to
this option.
GET Vs POST
8||WIT-UNIT -IV
BTECH_CSE-SEM 32
SVCE TIRUPATI
<head>
<title> Validation DEMO </title>
<?php
if($_SERVER["REQUEST_METHOD"]=="PO
ST"){
if($_POST["uname"]==―‖){
echo "<font color=red>Please Enter valid User name</font><br>";
}
else if(strlen($_POST["uname"])<6){
echo "<font color=red>Please Enter valid User name with more than 6
characters<br></font>";
}
if($_POST["pwd"]==―‖){
echo "<font color=red>Please Enter valid Password</font><br>";
}
else if(strlen($_POST["pwd"])<6){
echo "<font color=red>Please Enter valid Password with more than 6
characters<br></font>";
}
}
?>
</head>
<body>
<form method=POST action="<?php $_SERVER['PHP_SELF']?>" >
<table>
<tr> <td>NAME:</td> <td><input type=text name="uname" /></td> </tr>
<tr> <td>PASSWORD:</td> <td><input type=password name="pwd" /></td>
</tr>
<tr><td><input type=reset value=CLEAR /></td> <td><input type=submit
value=NEXT
/></td></tr>
</table>
</form>
<?php if($_SERVER["REQUEST_METHOD"]=="POST"){
if($_POST["uname"]!=―‖ && strlen($_POST["uname"])>=6 && $_POST["pwd"]!=―‖
&& strlen($_POST["pwd"])>=6){
echo "Name:<font color=green>".$_POST['uname']."<br></font>"; echo
"Password: <font color=green>".$_POST['pwd']."<br></font>";
}
}
?>
</body>
9||WIT-UNIT -IV
BTECH_CSE-SEM 32
SVEC TIRUPATI
</html>
10 | | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
</html>
BTECH_CSE-SEM 32
SVEC TIRUPATI
<table align=center>
<tr> <td>SSC Percentage</td><td><input type="text" name="ssc"></td> </tr>
<tr> <td>Intermediate/10+2</td><td><input type="text" name="inter"></td>
</tr>
<tr> <td>UG </td><td><input type=text name=ug></td> </tr>
<input type=hidden name="name" value="<?php echo $_POST['name'];?>"
/>
<input type=hidden name="fname" value="<?php echo $_POST['fname'];?>"
/>
<input type=hidden name="mname" value="<?php echo
$_POST['mname'];?>" />
<input type=hidden name="gen" value="<?php echo $_POST['gen'];?>" />
<input type=hidden name="email" value="<?php echo $_POST['email'];?>" />
<input type=hidden name="Mobile" value="<?php echo $_POST['Mobile'];?>"
/>
<input type=hidden name="address" value="<?php echo
$_POST['address'];?>" />
<tr> <td><input type=reset value=clear></td> <td><input type="submit"
value="NEXT>>"></td>
</tr>
</table>
</form>
</body>
</html>
BTECH_CSE-SEM 32
SVEC TIRUPATI
<tr> <td>Mobile</td><td><?php echo $_POST["Mobile"];?></td> </tr>
<tr> <td>ADDRESS</td><td><?php echo $_POST["address"];?></td> </tr>
<tr> <td colspan=2><h5>Educational details</h5></td> </tr>
<tr> <td>SSC Percentage</td><td><?php echo $_POST["ssc"];?></td> </tr>
<tr> <td>Intermediate/10+2</td><td><?php echo $_POST["inter"];?></td>
</tr>
<tr> <td>UG </td><td><?php echo $_POST["ug"];?></td> </tr>
</table>
</body>
</html>
13 | | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
}
else if(strlen($_POST["uname"])<6){
echo "<font color=red>Please Enter valid User name with more than 6
characters<br></font>";
}
if($_POST["pwd"]==―‖){
echo "<font color=red>Please Enter valid Password</font><br>";
}
else if(strlen($_POST["pwd"])<6){
echo "<font color=red>Please Enter valid Password with more than 6
characters<br></font>";
}
}
?>
</head>
<body>
<form method=POST action="<?php $_SERVER['PHP_SELF']?>" >
14 | | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
<table>
<tr><td>NAME:</td> <td><input type=text name="uname" /></td> </tr>
<tr><td>PASSWORD:</td> <td><input type=password name="pwd" /></td>
</tr>
<tr><td><input type=reset value=CLEAR /></td> <td><input type=submit
value=NEXT /></td>
</tr>
</table>
</form>
<?php if($_SERVER["REQUEST_METHOD"]=="POST"){
if($_POST["uname"]!=―‖ && strlen($_POST["uname"])>=6 &&
$_POST["pwd"]!=―‖ && strlen($_POST["pwd"])>=6){
echo "Name:<font color=green>".$_POST['uname']."<br></font>"; echo
"Password: <font color=green>".$_POST['pwd']."<br></font>";
}
}
?>
</body>
</html>
Fig: Name with less than 6 characters and with out password
15 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
<?php function
test() {
$foo = "local content";
echo '$foo in global scope: ' .
$GLOBALS["foo"]; echo '<br>$foo in
current scope: ' . $foo . "\n";
}
$foo = "Global
content";
test();
?>
OUTPUT:
$foo in global scope: Global content
$foo in current scope: local content
Superglobal variables
Superglobals are built-in variables that are always available in all scope.
Several predefined variables in PHP are "superglobals", which means they are
available in all scopes throughout a script. Superglobals were introduced to
PHP 4.1. There is no need to do global $variable; to access them within
functions or methods. These superglobal variables are: The $_SERVER
superglobal contains information created by the web serverdetails regarding
the server and client configuration and the current request environment.
Examples:
• $_SERVER['HTTP_REFERER']: The URL of the page that referred the user to the
current location.
• $_SERVER['REMOTE_ADDR']: The clients IP address.
• The $_GET superglobal contains information pertinent to any parameters
passed using the GET method.
• The $_POST superglobal contains information pertinent to any parameters
passed using the POST method.
• The $_COOKIE superglobal stores information passed into the script through
HTTP cookies. Such cookies are typically set by a previously executed PHP script
through the PHP function setcookie().
Example:
<html>
<head>
<title> Validation DEMO </title>
<?php if($_SERVER["REQUEST_METHOD"]=="POST"){
if($_POST["uname"]==―‖){
echo "<font color=red>Please Enter valid User name</font><br>";
16 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
}
else if(strlen($_POST["uname"])<6){
echo "<font color=red>Please Enter valid User name with more than 6
characters<br></font>";
}
if($_POST["pwd"]==―‖){
echo "<font color=red>Please Enter valid Password</font><br>";
}
else if(strlen($_POST["pwd"])<6){
echo "<font color=red>Please Enter valid Password with more than 6
characters<br></font>";
}
}
?>
</head>
<body>
<form method=POST action="<?php $_SERVER['PHP_SELF']?>" >
<table>
<tr> <td>NAME:</td> <td><input type=text name="uname" /></td> </tr>
<tr> <td>PASSWORD:</td> <td><input type=password name="pwd" /></td>
</tr>
<tr><td><input type=reset value=CLEAR /></td> <td><input type=submit
value=NEXT
/></td></tr>
</table>
</form>
<?php if($_SERVER["REQUEST_METHOD"]=="POST"){
if($_POST["uname"]!=―‖ && strlen($_POST["uname"])>=6 &&
$_POST["pwd"]!=―‖ && strlen($_POST["pwd"])>=6){
echo "Name:<font color=green>".$_POST['uname']."<br></font>"; echo
"Password: <font color=green>".$_POST['pwd']."<br></font>";
}
}
?>
</body>
</html>
4.4 Preventing Multiple Submissions of a Form
One possible occurrence that happens often is that users become impatient
when waiting for your script to do what it is doing, and hence they click the
submit button on a form repeatedly. This can create confusion to your script
17 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
because, while the user may not see anything happening, your script is
probably going ahead with whatever it has been programmed to do.
Of particular danger are credit card number submittals. If a user continually
hits the submit button on a credit card submittal form, their card may be
charged multiple times if the developer has not taken the time to validate
against such an eventuality. You can deal with multiple submittal validation in
essentially two ways.
• Server side refers to a script located on the server that is receiving the data.
• Client side is mostly browser related.
Preventing Multiple Submissions on the Server Side
While you can accomplish this goal in a number of ways from a server-side
perspective, we prefer to use a session-based method. Basically, once the
submit button has been clicked; the server logs the request from the individual
user. If the user attempts to resubmit a request, the script notes a request is
already in motion from this user and denies the subsequent request. Once the
script has finished processing, the session is unset, and you have no more
worries. The following script is an example for Preventing Multiple Submissions
on the Server Side
Filename: p1.php
<html>
<body>
<form name="test" method="post" action="p2.php"> Name::<input type="text"
name="uname"><br> Password::<input type="password" name="pwd"><br>
<input type="submit" value="SUBMIT" id="submitbut"><br>
</form>
</body>
</html>
Filename: p2.php
<?php
$name=$_POST['uname'];
$pwd=$_POST['pwd']; session_start(); if(!isset($_SESSION['x'])){
$_SESSION['x']=TRUE;
} if($_SESSION['x']==TRUE){
mysql_connect("localhost","root",""); mysql_select_db("TEST");
mysql_query("INSERT INTO login(uname,pwd) VALUES('$name','$pwd')");
$_SESSION['x']=FALSE;
for($i=0;$i<=2000000;$i++);//do nothing
for($i=0;$i<=2000000;$i++);//do nothing
for($i=0;$i<=2000000;$i++);//do nothing
18 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
for($i=0;$i<=2000000;$i++);//do nothing
for($i=0;$i<=2000000;$i++);//do nothing
}
echo "Successfully added to database"; session_unset();
?>
Preventing Multiple Submissions on the Client Side
Handling multiple submittals from a client-side perspective is actually much
simpler than doing it on the server side. With well-placed JavaScript, you can
ensure that the browser will not let the submittal go through more than once.
The problem with this method, of course, is that JavaScript is not always
foolproof because of the user‘s ability to turn it off. The following example uses
JavaScript to cut off multiple submittals from a client-side (browser) level.
Filename: p1.php
<html>
<head>
<script>
function checkandsubmit() {
//Disable the submit button.
document.test.submitbut.disabled = true;
//Then submit the form.
document.test.submit();
}
</script>
</head>
<body>
<form name="test" onsubmit="return checkandsubmit ()" method="post"
action="p2.php"> Name::<input type="text" name="uname"><br>
Password::<input type="password" name="pwd"><br>
<input type="submit" value="SUBMIT" id="submitbut"><br>
</form>
</body>
</html>
After submitting, the button will be disabled as follows
BTECH_CSE-SEM 32
SVEC TIRUPATI
want to ensure that no malicious users are attempting to sabotage your script
with bad (or special) characters. PHP allots a number of functions to use in this
regard. string trim ( string str [, string charlist] )
string htmlspecialchars ( string string [, int quote_style [, string charset]] ) string
strip_tags ( string str [, string allowable_tags] )
string addslashes ( string str )
The following script demonstrates above functions
<?php
$msg1=" Welcome to PHP ";//for trim
$msg2="<b>Welcome to php</b>";
$msg3="Welcome \n to \php";
echo "With out using trim(): **".$msg1."**<br>";
echo "Using trim():**".trim($msg1)."**<br><br><br>";
echo "With out using htmlspecialchars(): ".$msg2."<br>";
echo "Using htmlspecialchars(): ".htmlspecialchars($msg2)."<br><br><br>";
echo "Using strip_tags(): ".strip_tags($msg2)."<br><br><br>";
echo "With out Using addslashes(): ".$msg3."<br>";
echo "Using addslashes(): ".addslashes($msg3)."<br><br><br>";
?>
OUTPUT:
With out using trim(): ** Welcome to PHP ** Using trim():**Welcome to PHP**
With out using htmlspecialchars(): Welcome to php Using htmlspecialchars():
<b>Welcome to php</b> Using strip_tags(): Welcome to php
With out Using addslashes(): Welcome to \php Using addslashes(): Welcome
to \\php
• The trim() function removes any blank space found at the beginning or end
of the submitted string.
• The htmlspecialchars() function turns attempted HTML into its special
character equivalent.
• The strip_tags() function completely removes any characters it sees as being
a tag.
• addslashes(), places a slash in front of any characters that could be harmful
to the database such as apostrophes. The end result is a string that is quite
squeaky clean.
7. File Uploads
Handling file uploads in PHP is not exactly difficult from a syntax point of view,
but it is important (extremely important in fact) to ensure that the file being
uploaded is within the upload constraints you lay out for it. The following are
the constrains of File:
• size: The size of the uploaded file (in bytes). You could easily find your server
20 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
under some heavy loads if you are not careful about what size of files are being
uploaded.
• type: The MIME type of the uploaded file. Ex: .jpeg, .pdf, .doc, etc,.
• name: The original file name that was uploaded. It is possible to change file
name at the time of uploading.
• tmp_name: The temporary name of the file that has been uploaded.
• error: The error code that may be generated by the file upload. The
following Example demonstrate file uploading using Form
<form action="" method="post" enctype="multipart/form-data">
<p>Upload Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" name="submitbut"/>
</p>
</form>
<?php
if(isset($_POST['sub
mitbut']))
{
foreach ($_FILES["pictures"]["error"] as $key
=> $error) { if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name =
$_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name,
"data/$name");
}
}
}
?>
XML:
What is XML?
Extensible Markup Language (XML) is used to describe data. The XML standard
is a flexible way to create information formats and electronically share
structured data via the public Internet, as well as via corporate networks.
Short for Extensible Markup Language, a specification developed by the W3C.
XML is a pared- down version of SGML, designed especially for Web
documents. It allows designers to create their own customized tags, enabling
the definition, transmission, validation, and interpretation of data between
21 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
22 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
➢ It is based on international standards.
➢ It is platform-independent, thus relatively immune to changes in
technology.
➢ Forward and backward compatibility are relatively easy to maintain
despite changes in DTD or Schema.
How can XML be used?
➢ XML can keep data separated from HTML
➢ XML can be used to store data inside HTML documents
➢ XML can be used as a format to exchange information
➢ XML can be used to store data in files or in databases
<root>
<child>
<subchild>
</subchild>
</child>
</root>
5. XML elements can have attributes in name/value pairs just like in
HTML. In XML the attribute value must always be quoted.
<?xml version="1.0"?>
23 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
<note date=12/11/99> // Incorrect
<note date="12/11/99"> // Correct
<to>Abhi</to>
<from>Avi</from>
<heading>Reminder</heading>
<body>Don't forget the visit!</body>
</note>
The logical structure of an XML document.
BTECH_CSE-SEM 32
SVEC TIRUPATI
competitors but rather complement to each other and clearly serving
altogether different purposes.
Types of XML Documents
There are two kinds of XML documents:
1. Well-formed
A "Well Formed" XML document is a document that conforms to the XML syntax
rules. They contain text and XML tags. Everything is entered correctly. They do
not, however, refer to a DTD. The following is a "Well Formed" XML document:
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
2. Valid
Valid documents not only conform to XML syntax but they also are error
checked against a
Document Type Definition (DTD) or schema
The following is the same document as above but with an added reference
to a DTD:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "InternalNote.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The building blocks of XML documents
XML documents (and HTML documents) are made up by the following building
blocks:- Elements, Tags, Attributes, Entities, PCDATA, and CDATA
This is a brief explanation of each of the building blocks:
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".
In a DTD, elements are declared with an ELEMENT declaration.
Tags
Tags are used to markup elements.
A starting tag like <element_name> mark up the beginning of an element, and
an ending tag like
25 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
</element_name> mark up the end of an element.
Examples: A body element: <body>body text in between</body>. A message
element:
<message>some message in between</message>
Attributes
Attributes provide extra information about elements.
Attributes are placed inside the start tag of an element. Attributes come in
name/value pairs. The following "img" element has an additional information
about a source file:
<img src="computer.gif" />
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 " /".
PCDATA
PCDATA stands for Parsed Character data. PCDATA is the text that will be
parsed by a parser. Tags inside the PCDATA will be treated as markup and
entities will be expanded.
CDATA
CDATA: (Unparsed Character data): CDATA contains the text which is not
parsed further in an XML document. Tags inside the CDATA text are not treated
as markup and entities will not be expanded.
Entities
Entities as variables used to define common text. Entity references are
references to entities.
Most of you will known the HTML entity reference: " " that is used to insert
an extra space in an HTML document. Entities are expanded when a
document is parsed by an XML parser.
The following entities are predefined in XML:
: Entity Character
References
< <
> >
& &
" "
' '
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
Document Type Definition Introduction to DTD
The purpose of a DTD is to define the legal building blocks of an XML
document. It defines the document structure with a list of legal elements. A
26 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
DTD can be declared inline in your XML document, or as an external reference.
Internal DTD
This is an XML document with a Document Type Definition
<?xml version="1.0"?> <!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The DTD is interpreted like this:
!ELEMENT note (in line 2) defines the element "note" as having four elements:
"to,from,heading,body". !ELEMENT to (in line 3) defines the "to" element to be
of the type "CDATA". !ELEMENT from (in line 4) defines the "from" element to be
of the type "CDATA" and so on.....
External DTD
This is the same XML document with an external DTD:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to><from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
This is a copy of the file "note.dtd" containing the Document Type Definition:
<?xml version="1.0"?>
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
Example(Program) on DTD
This is an example of an XML document that used an external DTD file and
cascading style sheet (CSS) file.
The XML File
The following file is called "parts.xml".
<?xml version="1.0"?>
27 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>
<PARTS>
<TITLE>Computer Parts</TITLE>
<PART>
<ITEM>Motherboard</ITEM>
<MANUFACTURER>ASUS</MANUFACTURER>
<MODEL>P3B-F</MODEL>
<COST> 123.00</COST>
</PART>
<PART>
<ITEM>Video Card</ITEM>
<MANUFACTURER>ATI</MANUFACTURER>
<MODEL>All-in-Wonder Pro</MODEL>
<COST> 160.00</COST>
</PART>
<PART>
<ITEM>Sound Card</ITEM>
<MANUFACTURER>Creative Labs</MANUFACTURER>
<MODEL>Sound Blaster Live</MODEL>
<COST> 80.00</COST>
</PART>
<PART>
<ITEMᡋ inch Monitor</ITEM>
<MANUFACTURER>LG Electronics</MANUFACTURER>
<MODEL> 995E</MODEL>
<COST> 290.00</COST>
</PART>
</PARTS>
This file specifies the use of two external files.
BTECH_CSE-SEM 32
SVEC TIRUPATI
MANUFACTURER
{ display: block; font-family: arial; color: #600060;
font-weight: 400; margin-left: 45pt; margin-top: 5pt; font-size: 18 } MODEL
{ display: block; font-family: arial; color: #006000;
font-weight: 400; margin-left: 45pt;
margin-top: 5pt; font-size: 18 } COST
{ display: block; font-family: arial; color: #800000;
font-weight: 400; margin-left: 45pt; margin-top: 5pt; font-size: 18 }
BTECH_CSE-SEM 32
SVEC TIRUPATI
constraints on the data contained in the document. Therefore XML schema is
considered to be richer and powerful than DTD.
iii)Also, XML schema provides an object oriented approach for defining the
structure of a XML document. But since XML schema is a new technology,
some XML parsers do not support it yet.
iv) XML Schema is namespace aware, while DTD is not.
v) XML Schemas are written in XML, while DTDs are not.
vi) XML Schema is strongly typed, while DTD is not.
vii) XML Schema has a wealth of derived and built-in data types that are not
available in DTD.
viii) XML Schema does not allow inline definitions, while DTD does.
Example(Program) on Schema
This example attempts to validate an XML data file (books.xml) against an XML
schema definition file (books.xsd). According to books.xsd, each <book>
element must have a <pub_date> child element. The second <book> element
in books.xml does not have this required element. Therefore, when we attempt
to validate the XML file , we should get a validation error.
XML file(books.xml)
<?xml version="1.0"?>
<x:books xmlns:x="urn:books">
<book id="bk001">
30 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
<author>Writer</author>
<title>The First Book</title>
<genre>Fiction</genre>
<price>44.95</price>
<pub_date>2000-10-01</pub_date>
<review>An amazing story of nothing.</review>
</book>
<book id="bk002">
<author>Poet</author>
<title>The Poet's First Poem</title>
<genre>Poem</genre>
<price>24.95</price>
<review>Least poetic poems.</review>
</book>
</x:books>
31 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
before passing it on to the application.
Why do we need XML Parsers
We need XML parser because we do not want to do everything in our
application from scratch, and we need some "helper" programs or libraries to
do something very low-level but very necessary to us.
o These low-level but necessary things include checking the well-formedness,
validating the document against its DTD or schema (just for validating parsers),
resolving character reference, understanding CDATA sections, and so on.
o XML parsers are just such "helper" programs and they will do all these jobs.
Types of parsers: SAX and DOM Simple API for XML (SAX)
Simple API for XML (SAX) parsing is different from DOM as it parses the XML files
step by step and is event based model. The SAX parser triggers an event when
they encounter an opening tag, element or attribute. Unlike in DOM parser it
is advisable to use the SAX parser for parsing large XML documents as it does
not load the complete XML file in the memory. This parser parses node by node
so it can read large XML files in smaller parts.
Difference between SAX and DOM
DOM SAX
Tree model parser (Tree of nodes) Event based parser (Sequence
of events)
DOM loads the file into the memory SAX parses the file at it reads i.e.
and then Parses node
parse the file by node
Has memory constraints since it No memory constraints as it does
loads the not store the
whole XML file before parsing XML content in the memory
DOM is read and write (can insert or SAX is read only i.e. can‘t insert or
delete the delete the
node) node
If the XML content is small then prefer Use SAX parser when memory
DOM parser content is large
Backward and forward search is SAX reads the XML file from top to
possible for searching the tags and bottom and backward navigation
evaluation of the information inside is not possible
the tags. So this gives the
ease of navigation
Slower at runtime Faster at runtime
That‘s all on difference between SAX and DOM parsers in Java, now it‘s up to
you on which XML parser you going to choose. DOM parser is recommended
over SAX parser if XML file is small enough and go with SAX parser if you don‘t
know size of xml files to be processed or they are large.
What are the usual application for a DOM parser and for a SAX parser?
In the following cases, using SAX parser is advantageous than using DOM
parser.
o The input document is too big for available memory
32 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
o You can process the document in small contiguous chunks of input. You do
not need the entire document before you can do useful work
o You just want to use the parser to extract the information of interest, and all
In the following cases, using DOM parser is advantageous than using SAX
parser.
o Your application needs to access widely separately parts of the document
at the same time.
o Your application may probably use an internal data structure which is
almost as complicated as the document itself.
o Your application has to modify the document repeatedly.
o Your application has to store the document for a significant amount of time
through many method calls.
33 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
CSS file, CDATA vs PCDATA and difference between DTD and XML schema.
Let's see an example of XML using DTD file.
employee.xml
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>[email protected]</email>
</employee> Test it Now
DTD XSD
DTD stands for Document XSD stands for XML Schema Definition.
Type Definition.
DTD doesn't define order XSD defines order for child elements.
for child elements.
34 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
DTD is not simple to learn. XSD is simple to learn because you don't
need to learn new language.
<xsl:stylesheetversion="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
What You Should Already Know
Before you continue you should have a basic understanding of the following:
• HTML
35 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
• XML
If you want to study these subjects first, find the tutorials on our Home page.
XSLT References
XSLT Elements
Description of all the XSLT elements from the W3C Recommendation, and
information about browser support.
XSLT, XPath, and XQuery Functions
XSLT 2.0, XPath 2.0, and XQuery 1.0, share the same functions library. There are
over 100 built-in functions. There are functions for string values, numeric values,
date and time comparison, node and QName manipulation, sequence
manipulation, and more.
XSL(T) Languages
XSLT is a language for transforming XML documents. XPath is a language for
navigating in XML documents. XQuery is a language for querying XML
documents.
It Started with XSL
XSL stands for EXtensible Stylesheet Language.
The World Wide Web Consortium (W3C) started to develop XSL because there
was a need for an XML-based Stylesheet Language.
CSS = Style Sheets for HTML
HTML uses predefined tags. The meaning of, and how to display each tag is
well understood. CSS is used to add styles to HTML elements.
XSL = Style Sheets for XML
XML does not use predefined tags, and therefore the meaning of each tag is
not well understood. A <table> element could indicate an HTML table, a piece
of furniture, or something else - and browsers do not know how to display it!
So, XSL describes how the XML elements should be displayed. XSL - More Than
a Style Sheet Language
XSL consists of four parts:
• XSLT - a language for transforming XML documents
• XPath - a language for navigating in XML documents
• XSL-FO - a language for formatting XML documents (discontinued in
2013)
• XQuery - a language for querying XML documents
With the CSS3 Paged Media Module, W3C has delivered a new standard for
document formatting. So, since 2013, CSS3 is proposed as an XSL-FO
replacement.
What is XSLT?
• XSLT stands for XSL Transformations
• XSLT is the most important part of XSL
• XSLT transforms an XML document into another XML document
• XSLT uses XPath to navigate in XML documents
• XSLT is a W3C Recommendation XSLT = XSL Transformations
XSLT is the most important part of XSL.
36 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
XSLT is used to transform an XML document into another XML document, or
another type of document that is recognized by a browser, like HTML and
XHTML. Normally XSLT does this by transforming each XML element into an
(X)HTML element.
With XSLT you can add/remove elements and attributes to or from the output
file. You can also rearrange and sort elements, perform tests and make
decisions about which elements to hide and display, and a lot more.
A common way to describe the transformation process is to say that XSLT
transforms an XML source-tree into an XML result-tree.
BTECH_CSE-SEM 32
SVEC TIRUPATI
attribute version="1.0".
Start with a Raw XML Document
We want to transform the following XML document ("cdcatalog.xml") into
XHTML:
<?xml version="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>
Viewing XML Files in IE, Chrome, Firefox, Safari, and Opera: Open the XML file
(click on the link below) - The XML document will be displayed with color-
coded root and child elements (except in Safari). Often, there is a plus (+) or
minus sign (-) to the left of the elements that can be clicked to expand or
collapse the element structure. Tip: To view the raw XML source, right- click in
XML file and select "View Source"!
Create an XSL Style Sheet
Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation
template:
<?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>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>
38 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Link the XSL Style Sheet to the XML Document
Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):
<?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>
</catalog>
If you have an XSLT compliant browser it will nicely transform your XML into
XHTML.
News Feed (RSS and ATOM)
➢ The benefit of RSS and Atom.
➢ What is RSS about.
➢ Structure and Syntax of RSS.
➢ What is Atom about.
➢ Structure and Syntax of Atom.
➢ What is RSS and Atom all about?
They are a form of communication using XML documents to broadcast
information updates to a large group of subscribers.
• RSS stands for Really Simple Syndication. It's an easy way for you to keep up
with news and information that's important to you, and helps you avoid the
conventional methods of browsing or searching for information on websites.
Now the content you want can be delivered directly to you without cluttering
your inbox with e-mail messages. This content is called a "feed.―
• RSS is written in the Internet coding language known as XML (eXtensible
Markup Language).
➢ RSS must be the root element followed by one channel element.
➢ Required channel elements:
✓ Title: The name of the channel. It's how people refer to your service. If you
39 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
have an HTML website that contains the same information as your RSS file, the
title of your channel should be the same as the title of your website.
✓ Link: The URL to the HTML website corresponding to the channel.
✓ description : Phrase or sentence describing the channel.
RSS 2.0
Optional <channel > Elements
<item>
<title
What is ATOM 1.0 ?
• ATOM – The Atom Syndication Format is the next generation of XML-based
file formats, designed to allow information--the contents of web pages, for
example--to be syndicated between applications. Like RSS before it, Atom
places the content and metadata of an internet resource into a machine-
parsable format, perfect for displaying, filtering, remixing, and archiving.
40 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPAA
to TI
40 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
Atom Sample
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<link rel="self" href="http://example.org/blog/index.atom"/>
<id>http://example.org/blog/index.atom</id>
<icon>../favicon.ico</icon>
<title>An Atom Sampler</title>
<subtitle>No Splitting</subtitle>
<author>
<name>Ernie Rutherford </name>
<email>[email protected]</email>
<uri>.</uri>
</author>
<updated>2006-10-25T03:38:08-04:00</updated>
<link href="."/>
<entry>
<id>tag:example.org,2004:2417</id>
<link href="2006/10/23/moonshine"/>
<title>Moonshine</title>
<content type="text">
Anyone who expects a source of power from the transformation of the atom
is talking moonshine.
</content>
<published>2006-10-23T15:33:00-04:00</published>
41 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
<updated>2006-10-23T15:47:31-04:00</updated>
</entry>
<entry>
<id>>tag:example.org,2004:2416</id>
<link href="2006/10/21/think"/>
<title type="html"><strong>Think!</strong></title>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<p>We haven't got the money, so we've got to
think!</p>
</div>
</content>
<updated>2006-10-21T06:02:39-04:00</updated>
</entry>
</feed>
9. Practice Quiz
42 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
d) All of the above.
7. What does DTD stand for?
a) Dynamic Type Definition. B.
b) Document Type Definition
c) Direct Type Definition
d) DATA Type Definition
8. For the XML parser to ignore a certain section of your XML document, which syntax is
correct?
a) <CDATA> Text to be ignored </CDATA>
b) <![CDATA[ Text to be ignored ]]>
c) <PCDATA> Text to be ignored </PCDATA>
d) <xml:CDATA[ Text to be ignored ]>
9. What is a correct way of referring to a stylesheet called "mystyle.xsl" ?
a) <link type="text/xsl" href="mystyle.xsl" />
b) <?xml-stylesheet type="text/xsl" href="mystyle.xsl"?>
c) <stylesheet type="text/xsl" href="mystyle.xsl" />
d) All of the above
10. Assignments
S.No Question BL CO
List and explain the important features of XML which make it more
1 suitable than HTML for creating web related services. 2 3
43 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
2 Explain Types of XML Documents
Ans. There are two kinds of XML documents:
3. Well-formed
A "Well Formed" XML document is a document that conforms to
the XML syntax rules. They contain text and XML tags. Everything
is entered correctly. They do not, however, refer to a DTD. 2 3
4. Valid
Valid documents not only conform to XML syntax but they also
are error checked against a Document Type Definition (DTD) or
schema
3 Define DTD?
Ans. The purpose of a DTD is to define the legal building blocks of an
XML document. It defines the document structure with a list of legal
1 3
elements. A DTD can be declared inline in your XML document, or as
an external reference.
44 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
12. Part B- Questions
S.No Question BL CO
1 Explain and show how XML is useful in defining data for web 1 3
applications.
2 Explain how styling XML with cascading style sheets is done for the 2 3
library information domain.
4 List and explain the important features of XML which make it more 1 3
suitable than HTML for creating
5 How do you set default and fixed values for simple Elements? 2 3
2 Digital Advertising 2
3 E-Commerce 3
45 | W I T - U N I T - I V
BTECH_CSE-SEM 32
SVEC TIRUPATI
References:
1. Deitel and Deitel and Nieto, “Internet and World Wide Web - How to Program”, Prentice
Hall, 5 th Edition, 2011.
2. Herbert Schildt, “Java-The Complete Reference”, Eighth Edition, Mc Graw Hill
Professional, 2011.
46 | W I T - U N I T - I V
BTECH_CSE-SEM 32