Web Technology · Unit 5 · 7 hrs
AJAX and XML
Exam-focused notes for AJAX and XML (Web Technology, CSC329): what the TU syllabus asks and how it has actually been tested, with 11 solved past questions from this unit.
AJAX and XML in short
This unit covers two technologies that let a page exchange data behind the scenes: AJAX, which updates a page without a full reload, and XML, a self-describing data format together with the schema and transformation languages built around it.
What this unit covers
- Basics of AJAX
- Introduction to XML and its Application
- Syntax Rules for creating XML document
- XML Elements
- XML Attributes
- XML Tree
- XML Namespace
- XML schema languages: Document Type Definition(DTD), XML Schema Definition (XSD)
- XSD Simple Types
- XSD Attributes
- XSD Complex Types
- XML Style Sheets (XSLT)
- XQuery
XSD Complex Types
2082 · 10 marks
What is the use of XML? Create an XML file having complex type elements containing other elements and text. At least one element should contain an attribute. Similarly, there should be elements containing string, integer and date type values. Now write the equivalent XSD for the XML.[10]
Show solved answerHide answer
XML: Uses, XML File with Complex Types, and Equivalent XSD
Uses of XML
XML (eXtensible Markup Language) is used for:
- Data storage and transport between applications and systems
- Platform-independent data exchange over the internet
- Configuration files for software applications (e.g., web.xml, pom.xml)
- Web services (SOAP, REST use XML for message formatting)
- Document representation (e.g., Office documents, RSS feeds)
- Database-like storage for small structured datasets
- Separating data from presentation (used with XSLT for transformation)
XML File with Complex Type Elements
The following XML file represents a Library with Book records. It includes:
- Complex type elements containing other elements and text
- At least one element with an attribute
- Elements with string, integer, and date type values
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="B001">
<title>Introduction to Programming</title>
<author>Dennis Ritchie</author>
<publisher>
<name>TechPress</name>
<country>USA</country>
</publisher>
<price>1500</price>
<publishedDate>2020-06-15</publishedDate>
<available>true</available>
</book>
<book id="B002">
<title>Data Structures and Algorithms</title>
<author>Thomas Cormen</author>
<publisher>
<name>MIT Press</name>
<country>USA</country>
</publisher>
<price>2200</price>
<publishedDate>2018-03-10</publishedDate>
<available>false</available>
</book>
</library>
Explanation of the XML
| Element/Attribute | Type | Description |
|---|---|---|
<library> | Root element | Contains all book records |
<book id="B001"> | Complex type with attribute | id is an attribute of book |
<title>, <author>, <name>, <country> | String | Text values |
<price> | Integer | Numeric value |
<publishedDate> | Date | Date in YYYY-MM-DD format |
<publisher> | Complex type | Contains child elements <name> and <country> |
Equivalent XSD (XML Schema Definition)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Root Element -->
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="bookType"
minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Complex Type: bookType -->
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="publisher" type="publisherType"/>
<xs:element name="price" type="xs:integer"/>
<xs:element name="publishedDate" type="xs:date"/>
<xs:element name="available" type="xs:boolean"/>
</xs:sequence>
<!-- Attribute for book element -->
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
<!-- Complex Type: publisherType (nested complex type) -->
<xs:complexType name="publisherType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Explanation of the XSD
| XSD Component | Purpose |
|---|---|
xs:schema | Root element of every XSD file |
xs:element | Declares an XML element |
xs:complexType | Defines elements that contain child elements or attributes |
xs:sequence | Child elements must appear in the defined order |
xs:attribute use="required" | Declares id as a required attribute of <book> |
xs:string | Simple type for text values (title, author, name, country) |
xs:integer | Simple type for whole number values (price) |
xs:date | Simple type for date values (publishedDate) |
xs:boolean | Simple type for true/false values (available) |
minOccurs / maxOccurs | Controls how many times an element can appear |
Note: XSD is like DTD but provides more control on XML structure. It supports predefined simple types such as
xs:integer,xs:boolean,xs:string, andxs:date, and uses complex types as containers for other element definitions to build a hierarchy of elements.
Syntax Rules for creating XML document
2081 · 10 marks
Describe the rules for creating XML document. Create a XML file with simple type and elements with attribute and nested elements. Write its equivalent XSD.[10]
2080 · 10 marks
How XML is different from HTML? Write XML script to create XML document containing element Person having sub elements Age, Name and Pincode. Write equivalent XSD for validating restrictions on the elements as the Age should be between 10 and 20, the Name should be only accepting either Ram or Shyam and the pincode should be 3 digit.[10]
XML Style Sheets
2081 · 5 marks
How can you transform XML document into HTML using XSLT. Support your answer with an example. [5]
Introduction to XML and its Application
2080 · 5 marks
What is XML? Why is it called extensible? Mention the use of XML. [5]
2076 · 5 marks
What is XML? Create an XML file containing records of employee having elements of simple and complex types. [5]
Basics of AJAX
2080 · 5 marks
What is XMLHttpRequest object? Write HTML script with AJAX showing the use of XMLHttpRequest object. [5]
Show solved answerHide answer
XMLHttpRequest Object and AJAX
XMLHttpRequest Object
The XMLHttpRequest (XHR) object is a built-in browser object that is used to exchange data with a web server in the background. It is the core component of AJAX technology. Using the XMLHttpRequest object, a web page can request data from a server and update only a portion of the page without reloading the entire page.
Syntax to create an XMLHttpRequest object:
var xhr = new XMLHttpRequest();
Key Methods of XMLHttpRequest Object
| Method | Description |
|---|---|
open(method, url, async) | Specifies the type of request (GET/POST), the server URL, and whether the request is asynchronous (true) or synchronous (false) |
send() | Sends the request to the server (used for GET) |
send(string) | Sends the request to the server (used for POST) |
Key Properties of XMLHttpRequest Object
| Property | Description |
|---|---|
responseText | Returns the server response as a JavaScript string |
responseXML | Returns the server response as an XML DOM object |
onreadystatechange | Defines a function to be called when the readyState changes |
readyState | Holds the status of the request (0 to 4) |
status | Returns the HTTP status code (e.g., 200 for OK) |
HTML Script with AJAX Using XMLHttpRequest Object
<!DOCTYPE html>
<html>
<head>
<title>AJAX XMLHttpRequest Example</title>
<script>
function loadData() {
// Step 1: Create XMLHttpRequest object
var xhr = new XMLHttpRequest();
// Step 2: Define what happens when response is received
xhr.onreadystatechange = function() {
// readyState 4 means request is done
// status 200 means response is OK
if (xhr.readyState == 4 && xhr.status == 200) {
// Step 5: Update the page with the server response
document.getElementById("result").innerHTML = xhr.responseText;
}
};
// Step 3: Open the request (GET method, async = true)
xhr.open("GET", "ajax_info.txt", true);
// Step 4: Send the request to the server
xhr.send();
}
</script>
</head>
<body>
<h2>AJAX Demo Using XMLHttpRequest</h2>
<button onclick="loadData()">Click to Load Data</button>
<!-- This div will be updated without page reload -->
<div id="result">
<p>Data will appear here without reloading the page.</p>
</div>
</body>
</html>
How This Script Works (AJAX Working Steps)
- The user clicks the "Click to Load Data" button, which triggers the
loadData()function. - A new XMLHttpRequest object (
xhr) is created. - The
open()method specifies a GET request toajax_info.txtwithasync = true(asynchronous). - The
send()method sends the request to the web server. - When the server responds (
readyState == 4andstatus == 200), theonreadystatechangefunction is called. - The response text from the server is displayed inside the
<div id="result">element without reloading the page.
Note: The file
ajax_info.txtis a simple text file stored on the server that contains the data to be fetched and displayed.
XML schema languages
2079 · 10 marks
What is the use of XML? Create a XML file with simple type and complex type elements. Write its equivalent DTD.[10]
Show solved answerHide answer
Use of XML, XML File with Simple/Complex Types, and Equivalent DTD
Uses of XML
XML (eXtensible Markup Language) is a markup language designed to store, transport, and structure data in a human-readable and machine-readable format. Its main uses are:
| Use | Description |
|---|---|
| Data Storage | Stores data in a structured, self-describing format |
| Data Transport | Exchanges data between incompatible systems and platforms |
| Configuration Files | Many applications use XML for settings and configuration |
| Web Services | SOAP-based web services use XML for request/response messages |
| Separation of Data from Presentation | XML stores data; HTML/CSS handles display |
| Platform Independence | Works across different operating systems and programming languages |
| Document Representation | Used in formats like DOCX, SVG, and RSS feeds |
XML File with Simple Type and Complex Type Elements
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<book id="101">
<title>Web Technology</title>
<author>
<firstname>John</firstname>
<lastname>Smith</lastname>
</author>
<price>450.00</price>
<year>2020</year>
</book>
<book id="102">
<title>XML Fundamentals</title>
<author>
<firstname>Mary</firstname>
<lastname>Jones</lastname>
</author>
<price>350.50</price>
<year>2021</year>
</book>
</library>
Explanation of Element Types
Simple Type Elements contain only text/data and no child elements:
<title>,<firstname>,<lastname>,<price>,<year>
Complex Type Elements contain other child elements or attributes:
<library>contains one or more<book>child elements<book>contains child elements and has an attributeid<author>contains<firstname>and<lastname>child elements
: "Simple type element is used only in the context of text" and "A complex type is a container for other element definitions, allowing us to specify which child elements an element can contain."
Equivalent DTD (Document Type Definition)
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, price, year)>
<!ATTLIST book id CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (firstname, lastname)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT year (#PCDATA)>
Explanation of DTD Declarations
| DTD Declaration | Meaning |
|---|---|
<!ELEMENT library (book+)> | library contains one or more book elements (+ means one or more) |
<!ELEMENT book (title, author, price, year)> | book is a complex type with four ordered child elements |
<!ATTLIST book id CDATA #REQUIRED> | book has a required attribute id of character data type |
<!ELEMENT title (#PCDATA)> | title is a simple type containing parsed character data (text only) |
<!ELEMENT author (firstname, lastname)> | author is a complex type with two child elements |
<!ELEMENT firstname (#PCDATA)> | firstname is a simple type containing text only |
<!ELEMENT lastname (#PCDATA)> | lastname is a simple type containing text only |
<!ELEMENT price (#PCDATA)> | price is a simple type containing text only |
<!ELEMENT year (#PCDATA)> | year is a simple type containing text only |
: "In DTD, attributes are declared with an ATTLIST declaration" with syntax
<!ATTLIST element-name attribute-name attribute-type attribute-value>.
Summary
- XML provides a flexible, platform-independent way to structure and exchange data.
- Simple type elements hold only text data (
#PCDATA). - Complex type elements contain other child elements or attributes, acting as containers.
- DTD defines the legal structure and rules for the XML document, ensuring data validity and consistency.
2078 · 5 marks
What is DTD? Create a XML file and write its equivalent DTD. [5]
XML Namespace
2079 · 5 marks
What is XML Namespace? How is it used to avoid element name conflict in XML? Justify with an example. [5]
XSD Attributes
2076 · 5 marks
How XSD of a XML file is created? Illustrate with an example. [5]
Make Unit 5 stick
Practice CSC329 with flashcards & quizzes