Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 1
Aim:Create an XML document and validate it using a DTD file.
catalog.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<department code="CSE" name="Computer Science">
<course code="20CS302" credits="3">
<title>Data Structures</title>
<instructor email="[email protected]">Chandu</instructor>
<schedule>
<day>Monday</day>
<day>Wednesday</day>
<time>10:00 AM - 11:30 AM</time>
</schedule>
</course>
<course code="20CS303" credits="3">
<title>Operating Systems</title>
<instructor email="[email protected] ">Kishan Chand</instructor>
<schedule>
<day>Tuesday</day>
<day>Thursday</day>
<time>2:00 PM - 3:30 PM</time>
</schedule>
</course>
</department>
<department code="ECE" name="Electronics and Communication">
<course code="ECE201" credits="3">
<title>Analog Electronics</title>
<instructor email="[email protected]">David Lee</instructor>
<schedule>
<day>Monday</day>
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 2
<day>Friday</day>
<time>9:00 AM - 10:30 AM</time>
</schedule>
</course>
</department>
</catalog>
catalog.dtd
<!ELEMENT catalog (department+)>
<!ELEMENT department (course+)>
<!ATTLIST department
code CDATA #REQUIRED
name CDATA #REQUIRED
>
<!ELEMENT course (title, instructor, schedule)>
<!ATTLIST course
code CDATA #REQUIRED
credits CDATA #REQUIRED
>
<!ELEMENT title (#PCDATA)>
<!ELEMENT instructor (#PCDATA)>
<!ATTLIST instructor
email CDATA #REQUIRED
>
<!ELEMENT schedule (day+, time)>
<!ELEMENT day (#PCDATA)>
<!ELEMENT time (#PCDATA)>
Output:
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 3
Create an XML document and validate it using XML Schema Definition (XSD).
catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="catalog.xsd">
<department code="CSE" name="Computer Science">
<course code="20CS302" credits="3">
<title>Data Structures</title>
<instructor email="[email protected]">Chandu</instructor>
<schedule>
<day>Monday</day>
<day>Wednesday</day>
<time>10:00 AM - 11:30 AM</time>
</schedule>
</course>
<course code="20CS303" credits="3">
<title>Operating Systems</title>
<instructor email="[email protected] ">Kishan Chand</instructor>
<schedule>
<day>Tuesday</day>
<day>Thursday</day>
<time>2:00 PM - 3:30 PM</time>
</schedule>
</course>
</department>
<department code="ECE" name="Electronics and Communication">
<course code="ECE201" credits="3">
<title>Analog Electronics</title>
<instructor email="[email protected]">David Lee</instructor>
<schedule>
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 4
<day>Monday</day>
<day>Friday</day>
<time>9:00 AM - 10:30 AM</time>
</schedule>
</course>
</department>
</catalog>
catalog.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="department" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="course" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="instructor">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="email" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="schedule">
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 5
<xs:complexType>
<xs:sequence>
<xs:element name="day" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="time" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="code" type="xs:string" use="required"/>
<xs:attribute name="credits" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="code" type="xs:string" use="required"/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Output:
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 6
3.Create an XML document and convert it to HTML using XSLT.
books.xml
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="CS101" department="Computer Science">
<title>Introduction to Algorithms</title>
<author>Thomas H. Cormen</author>
<price>950</price>
<edition>3</edition>
<available>true</available>
</book>
<book id="CS102" department="Computer Science">
<title>Artificial Intelligence: A Modern Approach</title>
<author>Stuart Russell</author>
<price>870</price>
<edition>4</edition>
<available>true</available>
</book>
<book id="CS103" department="Computer Science">
<title>Computer Networks</title>
<author>Andrew S. Tanenbaum</author>
<price>720</price>
<edition>5</edition>
<available>false</available>
</book>
<book id="EE201" department="Electronics">
<title>Microelectronics</title>
<author>Jacob Millman</author>
<price>620</price>
<edition>2</edition>
<available>true</available>
</book>
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 7
</library>
books.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Computer Science Books</title>
<style>
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid black; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
.out-of-stock { color: red; font-weight: bold; }
</style>
</head>
<body>
<h2>Computer Science Department - Book Catalog</h2>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Edition</th>
<th>Price (INR)</th>
<th>Status</th>
</tr>
<xsl:for-each select="library/book[@department='Computer Science']">
<xsl:sort select="price" data-type="number" order="descending"/>
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 8
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="edition"/></td>
<td><xsl:value-of select="price"/></td>
<td>
<xsl:choose>
<xsl:when test="available='true'">Available</xsl:when>
<xsl:otherwise>
<span class="out-of-stock">Out of Stock</span>
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
<p>Total CS Books:
<xsl:value-of select="count(library/book[@department='Computer Science'])"/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 9
Output3:
sample.html
<!DOCTYPE html>
<html>
<head>
<title>Simple AJAX Example</title>
<script>
function loadData() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.txt", true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("result").innerHTML = xhr.responseText;
};
xhr.send();
</script>
</head>
<body>
<h2>Click to Load Content Using AJAX</h2>
<button onclick="loadData()">Load Data</button>
<div id="result" style="margin-top: 20px; border: 1px solid #ccc; padding: 10px;">
<!-- Data will appear here -->
</div>
</body>
</html>
data.txt
Bapatla Engineering College
Class:2/4CSE Regdno:Y23ACS556 SECTION:C PageNo: 10
This is content loaded using AJAX from a text file.
You can change this content in data.txt and see updates without reloading the page.
<!DOCTYPE html>
<html>
<head>
<title>First jQuery Example</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("p").css("background-color", "blue");
});
</script>
</head>
<body>
<p>This is first paragraph.</p>
<p>This is second paragraph.</p>
<p>This is third paragraph.</p>
</body>
</html>
Bapatla Engineering College