KEMBAR78
WD Assignment 2 | PDF | Xml | Document Object Model
0% found this document useful (0 votes)
7 views6 pages

WD Assignment 2

Uploaded by

davalag784
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

WD Assignment 2

Uploaded by

davalag784
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 2

Definition XML:
Introduction to XML (eXtensible Markup Language):
XML is a versatile markup language designed to store and transport data. It is a set
of rules for encoding documents in a format that is both human-readable and
machine-readable.

History:

The inception of XML marked a transformative journey in data management and


communication. Tracing its development from conceptualization to key
milestones, the historical context illuminates XML's evolution, underscoring its
significance in shaping contemporary data standards.

Features of XML:
Extensibility: Allows the user to define their own tags.

Self-descriptive: Uses tags to define data, making it easy to understand.

Platform-independent: Can be used on any platform or programming language.

Hierarchical structure: Represents data in a tree-like structure.

Interoperability: Can be used to exchange information between different systems.

Naming Rules in XML:

Names must start with a letter or underscore.

Names can contain letters, numbers, hyphens, underscores, and periods.

Names are case-sensitive.


Names cannot start with "xml" (or XML, or Xml, etc.) unless they are in the
XML namespace.

Uses of XML:

Data storage: XML is often used to store data in a structured format. This makes XML
data easy to query and process.

Data exchange: XML is often used to exchange data between different systems. This is
because XML is a platform-independent language that is easy to parse and interpret.

Configuration files: XML is often used to store configuration files for applications. This is
because XML documents are easy to read and edit.

Building Blocks of XML Document:

Elements:
A comprehensive exploration of XML elements, including attributes and characteristics,
lays the groundwork for understanding the fundamental components that constitute
XML documents. Real-world examples illustrate diverse applications of XML elements in
data representation.

Attributes:
Detailed coverage of attributes and their role in providing additional information
enhances the understanding of how XML documents convey nuanced details. This
section explores best practices in utilizing attributes for efficient data representation.

Text Content:
An in-depth analysis of storing and representing data within XML elements elucidates
the mechanisms through which textual information is encapsulated and organized. Case
studies demonstrate effective strategies for handling and presenting text content in XML
documents.

Comments:
Understanding and utilizing comments in XML for documentation and collaboration
unveils the communicative aspects of XML documents. Practical examples showcase
how comments contribute to fostering collaboration and maintaining clarity in
development processes.
Difference between HTML and XML:

Purpose: HTML is designed to display data in a web browser, while XML is designed to
store and exchange data.

Structure: HTML has a predefined set of elements and attributes, while XML is an
extensible language that allows users to create their own elements and attributes.

Validation: HTML documents are not validated, while XML documents can be validated
against a Document Type Definition (DTD) or an XML Schema (XSD).

XML Parser:

An XML parser is a software component or a program that reads and processes


XML documents. Its primary purpose is to analyze the structure of an XML
document, validate it against a specified schema (if available), and provide a way
for applications to access and manipulate the data within the document. XML
parsers play a crucial role in facilitating communication and data exchange
between different software systems.

There are two main types of XML parsers:

DOM Parser (Document Object Model):


A DOM parser builds a hierarchical tree structure in memory that represents the
entire XML document. Each element, attribute, and piece of text in the XML
document becomes a node in the tree. This tree structure is called the Document
Object Model (DOM), and it allows applications to traverse, modify, and
manipulate the XML document easily. DOM parsers are generally memory-
intensive because they load the entire document into memory.

SAX Parser (Simple API for XML):


Unlike DOM parsers, SAX parsers do not build an in-memory representation of the
entire XML document. Instead, they work by sequentially reading the XML
document from start to end and generating events (such as start element, end
element, and text) as they encounter different parts of the document. SAX parsers
are generally more memory efficient than DOM parsers, making them suitable for
handling large XML documents. However, they are often considered more
challenging to use, as they require developers to write event handlers to respond
to the generated events.

DTD (Document Type Definition):

A DTD defines the structure and elements allowed within an XML


document. It provides a blueprint for valid XML documents and helps ensure
data consistency.

Using XML with HTML and CSS:

1. Create an XML Document (books.xml):


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore [
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, author, genre, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT genre (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book id ID #REQUIRED>
]>
<bookstore>
<book id="1">
<title>Introduction to XML</title>
<author>John Doe</author>
<genre>Programming</genre>
<price>29.99</price>
</book>
<book id="2">
<title>Data Structures in XML</title>
<author>Jane Smith</author>
<genre>Computer Science</genre>
<price>39.95</price>
</book>
</bookstore>
2. Create an HTML Document (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Books Catalog</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Books Catalog</h1>
<div id="books">
<!-- The XML data will be transformed here using XSLT -->
</div>
<script src="script.js"></script>
</body>
</html>

3. Create a CSS Stylesheet (styles.css):


body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
#books {
border: 1px solid #ccc;
padding: 10px;
margin-top: 10px;
}
.book {
border-bottom: 1px solid #eee;
padding: 10px;
}
.title {
font-size: 1.2em;
font-weight: bold;
}
.author {
color: #555;
}
.genre, .price {
margin-top: 5px;
color: #777;
}

4. Create a JavaScript File (script.js):


document.addEventListener("DOMContentLoaded", function () {
// Load and transform XML data using XSLT
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
const xsltProcessor = new XSLTProcessor();
const xslDoc = new
DOMParser().parseFromString(this.responseText,
"application/xml");
xsltProcessor.importStylesheet(xslDoc);
const xmlDoc = new
DOMParser().parseFromString(this.responseXML,
"application/xml");
const resultDocument =
xsltProcessor.transformToFragment(xmlDoc, document);

document.getElementById("books").appendChild(resultDocument);
}
};
xhttp.open("GET", "transform.xsl", true);
xhttp.send();
});

You might also like