Unit- IV Web Technologies Notes
Unit- IV Web Technologies Notes
in
Unit – IV
XML (Chapter –I)
1.1 XML Basics
XML stands for Extensible Markup Language and is a text-based markup language
derived from Standard Generalized Markup Language (SGML).
XML was designed to store and transport data
XML was designed to be self-descriptive
XML is a W3C Recommendation
XML documents are readable by both humans and machines.
An XML parser is responsible for identifying components of XML documents (typically
files with the .xml extension) and then storing those components in a data structure for
manipulation
An XML document can optionally reference a Document Type Definition (DTD) or
schema that defines the XML document’s structure.
If an XML parser (validating or non validating) can process an XML document
successfully, that XML document is well-formed
There are three important characteristics of XML that make it useful in a variety of
systems and solutions:
XML is extensible: XML allows you to create your own self-descriptive tags, or
language, that suits your application.
XML carries the data, does not present it: XML allows you to store the data
irrespective of how it will be presented.
XML is a public standard: XML was developed by an organization called the World
Wide Web Consortium (W3C) and is available as an open standard.
The XML document can optionally have an XML declaration. It is written as below:
<?xml version="1.0" encoding="UTF-8"?>
Where version is the XML version and encoding specifies the character
encoding used in the document
An XML file is structured by several XML-elements, also called XML-nodes
or XML-tags.
XML-element names are enclosed by triangular brackets < > as shown below
Syntax :
<element>....</element>
An XML document contains text that represents its content (i.e., data) and elements
that specify its structure. XML documents delimit an element with start and end tags.
An XML-element can contain multiple XML-elements as its children, but the children
elements must not overlap.
An XML document can have only one root element
An XML document with correct syntax is called "Well Formed".
General Syntax :
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
For example A contains a hash mark (“#”) followed by a number. The
number always refers to the Unicode code of a character. In this case, 65 refers
to alphabet "A".
Some characters are reserved by the XML syntax itself. Hence, they cannot be used
directly. To use them, some replacement-entities are used, which are listed below:
If these XML fragments were added together, there would be a name conflict. Both
contain a <table> element, but the elements have different content and meaning.
A user or an XML application will not know how to handle these differences
This XML carries information about an HTML table, and a piece of furniture:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements
have different names.
When using prefixes in XML, a namespace for the prefix must be defined.
The namespace can be defined by an xmlns attribute in the start tag of an element.
Syntax:
xmlns:prefix="URL"
<h:table xmlns:h="http://www.w3.org/TR/html/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
The xmlns attribute in the first <table> element gives the h: prefix a
qualified namespace.
<!DOCTYPE students
[
<!ELEMENT students (student+)>
<!ELEMENT student (name,group,gender)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT group (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
]>
<students>
<student>
<name>Raju</name>
<group>MSCs</group>
<gender>Male</gender>
</student>
<student>
<name>Ramya</name>
<group>MSCs</group>
<gender>Female</gender>
</student>
<student>
<name>Ramu</name>
<group>MSCs</group>
<gender>Male</gender>
</student>
</students>
Explanation :
The DOCTYPE declaration has an exclamation mark (!) at the start of the element
name. The DOCTYPE informs the parser that a DTD is associated with this XML
document.
The DOCTYPE declaration is followed by body of the DTD, where you declare elements,
attributes, entities, and notations.
Several elements are declared here that make up the vocabulary of the <name>
document. <!ELEMENT name (#PCDATA)> defines the element name to be of type
"#PCDATA". Here #PCDATA means parse-able text data.
Finally, the declaration section of the DTD is closed using a closing bracket and a closing
angle bracket (]>).
Students.dtd
<!ELEMENT students (student+)>
<!ELEMENT student (name,group,gender)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT group (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
Students.xml
<!DOCTYPE students SYSTEM "D:\students.dtd">
<students>
<student>
<name>Raju</name>
<group>MSCs</group>
<gender>Male</gender>
</student>
<student>
<name>Ramya</name>
<group>MSCs</group>
<gender>Female</gender>
</student>
<student>
<name>Ramu</name>
<group>MSCs</group>
<gender>Male</gender>
</student>
</students>
Complex Type :
Element content can contain other elements or the element can have attributes (or both).
Syntax :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root element name">
<xs:complexType>
<xs:sequence>
<!-- Content goes here-->
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The element xs:sequence is one of several ways to combine elements in the content.
Other xs:element Attributes
An xs:element element may also have attributes that specify the number of
occurrences of the element at this position in the sequence.
minOccurs="0" // default = 1
maxOccurs="5" // default = maximum(1, minOccurs)
maxOccurs="unbounded"
Example :
STUDENT.XML
<students>
<student>
<name>Raju</name>
<group>MSCs</group>
<gender>Male</gender>
</student>
<student>
<name>Ramya</name>
<group>MSCs</group>
<gender>Female</gender>
</student>
<student>
<name>Ramu</name>
<group>MSCs</group>
<gender>Male</gender>
</student>
</students>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded"
minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
<xs:element type="xs:string" name="group"/>
<xs:element type="xs:string" name="gender"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Some of the XML vocabularies are XHTML, RSS, XSL, DTD, and Schema
1.7.1 MathML
MathML markup describes mathematical expressions for display. MathML is divided
into two types of markup—content markup and presentation markup.
Content MathML allows programmers to write mathematical notation specific to
different areas of mathematics
Presentation MathML is directed toward formatting and displaying mathematical
notation. We focus on Presentation MathML in the MathML examples.
MathML files end with the .mml filename extension.
Basic elements :
MathML most basic elements are mrow, mi, mo and mn.
Example :
𝑥 + 𝑦 = 2 is encoded in MathML as:
3
Example: √𝑥 Example: √𝑥 + 𝑦
<mroot> <msqrt>
<mi>x</mi> <mi>x</mi>
<mn>3</mn> <mo>+</mo>
</mroot> <mi>y</mi>
</msqrt>
Example: 𝑥𝑖 Example: 𝑥 𝑖
<msub> <msup>
<mi>x</mi> <mi>x</mi>
<mi>i</mi> <mi>i</mi>
</msub> </msub>
In other words: The XML DOM is a standard for how to get, change, add, or delete XML
elements.
Programming Interface
The DOM models XML as a set of node objects. The nodes can be accessed with JavaScript or
other programming languages.
The programming interface to the DOM is defined by a set standard properties and methods.
<html>
<head>
<title> XML DOM </title>
<script>
function myfun()
{
var text, parser, xmlDoc;
text = "<bookstore><book>" +
"<title>Java</title>" +
"<author>James</author>" +
"<year>1991</year>" +
"</book></bookstore>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue;
}
</script>
<body>
<p id="demo">this is paragraph text</p>
<button type="button" onclick="myfun()">click me</button>
</body>
</html>
Output :
1.9 XSLT
Example: (example.xml)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<Article>
<Title>My Article</Title>
<Authors>
<Author>Mr. Santhosh</Author>
<Author>Mr. Chary</Author>
</Authors>
<Body>This is my article text.</Body>
</Article>
Example: (example.xsl)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
Article - <xsl:value-of select="/Article/Title"/>
Authors: <xsl:apply-templates
select="/Article/Authors/Author"/>
</xsl:template>
<xsl:template match="Author">
- <xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
Output :
Article - My Article
Authors:
- Mr. Santhosh
- Mr. Chary
(Chapter –II)
Ajax-Enabled Rich Internet Applications
2.1 Introduction :
Ajax acronym for Asynchronous JavaScript and XML.
Ajax is a Client side web technique.
JavaScript with AJAX you can able to make background server calls for fetching
addition data, Updating some portion in web page without refreshing the whole
page.
Using AJAX you can create better, faster, and more user-friendly web applications.
Ajax is based on JavaScript, CSS, HTML and XML etc. So you can easily learn.
Ajax behaviour and works is like a desktop application. So Ajax use for creating a
rich web application.
2.2 History :
The term Ajax was coined by Jesse James Garrett of Adaptive Path in February 2005,
when he was presenting the previously unnamed technology to a client
All of the technologies involved in Ajax (XHTML, JavaScript, CSS, dynamic HTML, the
DOM and XML) have existed for many years.
In 1998, Microsoft introduced the XMLHttpRequest object to create and manage
asynchronous requests and responses.
Popular applications like Flickr, Google’s Gmail and Google Maps use the
XMLHttpRequest object to update pages dynamically
Ajax has quickly become one of the hottest technologies in web development, as it
enables web top applications to challenge the dominance of established desktop
applications.
2.3 Traditional web applications Vs Ajax Applications:
In traditional web applications, the user fills in the form’s fields, then submits the form.
The browser generates a request to the server, which receives the request and
processes it.
The server generates and sends a response containing the exact page that the browser
will render, which causes the browser to load the new page and temporarily makes the
browser window blank.
The client waits for the server to respond and reloads the entire page with the data
from the response.
In an Ajax application, when the user interacts with a page, the client creates an
XMLHttpRequest object to manage a request.
The XMLHttpRequest object sends the request to and awaits the response from the
server. The requests are asynchronous, allowing the user to continue interacting with
the application while the server processes the request concurrently.
Ajax-enabled forms are more interactive. Entries are validated dynamically as the user
enters data into the fields. If a problem is found, the server sends an error message that
is asynchronously displayed to inform the user of the problem.
Sending each entry asynchronously allows the user to address invalid entries quickly,
rather than making edits and resubmitting the entire form repeatedly until all entries
are valid.
Asynchronous requests could also be used to fill some fields based on previous fields’
values.
The XMLHttpRequest object can be used to exchange data with a server behind the
scenes. This means that it is possible to update parts of a web page, without reloading
the whole page.
Create an XMLHttpRequest Object
All modern browsers (Chrome, Firefox, Edge (and IE7+), Safari, Opera) have a built-in
XMLHttpRequest object
Syntax :
variable = new XMLHttpRequest();
Example :
var xhttp = new XMLHttpRequest();
Example Program
<!DOCTYPE html>
<html>
<body>
<h2>Using the XMLHttpRequest Object</h2>
<div id="demo">
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</div>
<script>
function loadXMLDoc()
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "xmlhttp_info.txt", true);
xhttp.send();
}
</script>
</body>
</html>
Example Explained
The onreadystatechange property specifies a function to be executed every time the
status of the XMLHttpRequest object changes:
When readyState property is 4 and the status property is 200, the response is ready:
The responseText property returns the server response as a text string.
open() initializes a newly-created request, or re-initializes an existing one.
send() sends the request to the server.
To include the Dojo.js script file in your web application, place the following script
in the head element of your XHTML document :
where path is the relative or complete path to the Dojo toolkit’s files.