XML basics – structure data –XML namespace –Document Type Definitions (DTDs) –
W3C XML Schema Documents – Document Object Model (DOM)
XML Basics
XML stands for eXtensible Markup Language and is a text-based markup language
derived from Standard Generalized Markup Language (SGML).
XML is a software- and hardware-independent tool for storing and transporting data.
XML is a markup language much like HTML
XML was designed to store and transport data
XML was designed to be self-descriptive
XML is a W3C Recommendation
XML tags identify the data and are used to store and organize the data, rather than
specifying how to display it like HTML tags, which are used to display the data.
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.
XML (eXtensible Markup Language)
Document Type XML Schema XML Namespace eXtensible
Definition (DTD) (XSD) Stylesheet
Language (XSL)
Defines the structure Advanced Helps distinguish Used for
and legal alternative to DTD elements and transforming and
elements/attributes using XML syntax to attributes with the presenting XML
of XML define structure, data same name but
types, etc. different contexts
DOM (Document Object Model)
Represents the XML document as a tree structure, enabling programmatic access and
updates
Difference between XML and HTML
XML and HTML were designed with different goals:
XML was designed to carry data - with focus on what data is
HTML was designed to display data - with focus on how data looks
XML tags are not predefined like HTML tags
XML documents form a tree structure that starts at "the root" and branches to "the
leaves".
XML Tree Structure
XML Namespaces
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when
trying to mix XML documents from different XML applications.
This XML carries HTML table information:
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.
XML Namespaces – The xmlns Attribute
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.
The namespace declaration has the following syntax. xmlns:prefix="URI".
XML Validator
Well Formed XML Documents
An XML document with correct syntax is called "Well Formed". The syntax rules were
described in the previous chapters:
XML documents must have a root element
XML elements must have a closing tag
XML tags are case sensitive
XML elements must be properly nested
XML attribute values must be quoted
Valid XML Documents
A "well formed" XML document is not the same as a "valid" XML document.
A "valid" XML document must be well formed. In addition, it must conform to a document
type definition.
There are two different document type definitions that can be used with XML:
DTD - Document Type Definition
XML Schema - An XML-based alternative to DTD
XML DTD: (Document Type Definition)
An XML document with correct syntax is called "Well Formed".
An XML document validated against a DTD is both "Well Formed" and "Valid".
A "Valid" XML document is a "Well Formed" XML document, which also conforms
to the rules of a DTD.
DTD is the basic building block of XML.
The purpose of a DTD is to define the structure of an XML document. It defines the
structure with a list of legal elements.
XML Schema
An XML Schema describes the structure of an XML document, just like a DTD.
An XML document with correct syntax is called "Well Formed".
An XML document validated against an XML Schema is both "Well Formed" and
"Valid". XML Schema is commonly known as XML Schema Definition (XSD).
It is used to describe and validate the structure and the content of XML data.
XML schema defines the elements, attributes and data types. Schema element
supports Namespaces.
It is similar to a database schema that describes the data in a database.
XML Schema is an XML-based alternative to DTD
Example
XSD - this defines the structure and rules for the XML data
XML - this contains actual data
Studdoc.xsd
<xml>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema
elementFormDefault="qualified">
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="addr">
<xs:complexType>
<xs:sequence>
<xs:element name="city" type="xs:string"/>
<xs:element name="pincode" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ph" type="xs:long"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Student.xml (Actual Data)
<xml>
<student xsi:schemaLocation="http://www.w3schools.com student.xsd">
<name>Ajay</name>
<age>20</age>
<addr>
<city>Coimbatore</city>
<pincode>641003</pincode>
</addr>
<ph>9300011202</ph>
</student>
XSLT
Introduction XSL (EXtensible Stylesheet Language) is a styling language for XML.
XSLT stands for XSL Transformations.
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 Document Object Model (XML DOM)
The XML DOM is a tree-like structure that lets programming languages (like JavaScript,
Java, or Python) access, read, change, and delete parts of an XML document.
<book>
<title>XML Basics</title>
<author>Asha</author>
</book>
In the DOM, this is like a tree:
Document
└── book (Element)
├── title (Element)
│ └── "XML Basics" (Text)
└── author (Element)
└── "Asha" (Text)
Example
<html>
<body>
<h3>Book Info</h3>
<p id="output"></p>
<script>
// Step 1: Your XML as a string
let xmlData = `
<book>
<title>XML Basics</title>
<author>Asha</author>
</book>
`;
// Step 2: Convert string to XML DOM
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlData, "text/xml");
// Step 3: Use DOM to get values
let title = xmlDoc.getElementsByTagName("title")[0].textContent;
let author = xmlDoc.getElementsByTagName("author")[0].textContent;
// Step 4: Show result on the page
document.getElementById("output").innerHTML =
"Title: " + title + "<br>Author: " + author;
</script>
</body>
</html>