Extensible Markup Language(XML): Introduction Creating XML Documents XML style Sheet Hyperlinks in XML Document Object Model XML Query Language
An XML document must follow certain rules to ensure correctness and consistency. It always begins with a declaration and contains a single root element under which all other elements appear.
Basic Rules
• Only one root element must exist.
• Tags must be properly opened and closed.
• Tags are case-sensitive.
• Attribute values must be in quotes.
• Elements must be properly nested (no overlap).
Structure of an XML Document
1. XML Declaration
o Appears at the top.
o Specifies XML version and encoding format.
Example:<?xml version="1.0" encoding="UTF-8"?>
2. Root Element
o The main container for all other XML data.
Example: <Library> in a library dataset.
3. Child Elements
o Represent inner structured data.
Example:
o <Book>
o <Title>Python Basics</Title>
o </Book>
4. Attributes
o Provide additional metadata about elements.
Example: <Book id="B001">
Well-Formed vs Valid XML
Type Meaning
Well-formed XML Follows syntactic rules of XML
Valid XML Follows rules defined in DTD or XML Schema (XSD)
Why Validation is Important
• Ensures consistency of data structure.
• Prevents errors when exchanging data between applications.
• Maintains data accuracy and reliability.
3. XML Style Sheets
XML is mainly a data representation and data exchange language. It focuses on what the data is, not how it should appear when displayed. Therefore, XML does not define presentation, layout, color, fonts, or visual format. To display or convert XML data in a readable form, Style Sheets are used.
Purpose of Style Sheets
Style sheets are used to control the presentation of XML content without modifying the actual data. This supports the principle of separation of content and presentation, which improves usability and maintainability.
Using CSS with XML
CSS (Cascading Style Sheets) is commonly used to style HTML. But the same CSS can also
be applied to XML documents to enhance visual appearance, especially when XML files are
opened in web browsers.
Features of CSS for XML
Feature Explanation
Controls appearance only CSS modifies visual properties such as font style, size, color,
background, margins, etc.
Does not change the data only presentation is affected — underlying XML structure remains unchanged.
Useful for browsers most web browsers can apply CSS to XML for simple display
formatting.
Lightweight Suitable for simple display cases where data transformation is not required.
Example: Applying CSS to XML
XML File (students.xml)
<?xml-stylesheet type="text/css" href="style.css"?>
<Students>
<Student>
<Name>Arjun</Name>
<Course>AIML</Course>
<Year>2024</Year>
</Student>
</Students>
CSS File (style.css)
Students {
display: block;
font-family: Arial;
}
Student {
display: block;
margin: 10px;
padding: 10px;
border: 1px solid black;
}
Name {
color: blue;
font-weight: bold;
}
What is XSLT?
XSLT is a powerful style sheet language that transforms XML documents into other
formats, such as:
• HTML → to display data on a web page
• Plain Text → for printing or messaging
• Another XML Format → for data exchange between different systems
• PDF / Word (indirectly via processors)
How XSLT Works
• Uses template rules that “match” XML elements.
• Based on these matches, it generates new output.
• Works like pattern matching + formatting instructions.
Example: Transforming XML to HTML Using XSLT
XML File (books.xml)
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<Library>
<Book>
<Title>Learning Python</Title>
<Author>Mark Lutz</Author>
</Book>
</Library>
XSLT File (books.xsl)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Library Book List</h2>
<table border="1">
<tr><th>Title</th><th>Author</th></tr>
<xsl:for-each select="Library/Book">
<tr>
<td><xsl:value-of select="Title"/></td>
<td><xsl:value-of select="Author"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
4. Hyperlinks in XML (XLink)
XML uses XLink to provide hyperlinking capabilities similar to (and more flexible than)
HTML.
Purpose of XLink
• Allows any XML element to act as a hyperlink.
• Supports linking to internal or external documents.
Types of Links
Type Description
Simple Link Works like HTML <a> hyperlink
Extended Link Connects multiple documents/resources
XLink Attributes
• xlink:href → Specifies link target
• xlink:type → Specifies link type (simple / extended)
Advantages
• More descriptive and flexible linking
• Useful in technical documentation, digital libraries, and distributed systems.
5. XML Document Object Model (DOM)
The XML DOM is a programming interface that treats an XML document as a structured
tree of nodes. It allows software to read, navigate, and modify the content of XML data.
Hierarchy (Tree Structure)
Document → Root Element → Child Elements → Sub-elements →
Attributes
DOM Capabilities
• View and access nodes
• Add or remove elements
• Modify element text or attributes
• Search elements dynamically
Where DOM is Used
• Web browsers (JavaScript XML processing)
• Database applications
• Data parsing and transformation systems
Benefits
• Platform and language independent
• Enables programmatic control over XML data
• Suitable for applications that need frequent updates to XML data
6. XML Query Language
XML Query Languages help in extracting and processing data stored in XML documents.
i) XML documents are structured hierarchically like a tree, where elements are arranged as
parent–child nodes. XPath is a query language used to navigate through this tree structure
and select specific parts (nodes) of the XML document.
Purpose of XPath
• To locate elements, attributes, text, or parts of XML documents.
• Used heavily in:
o XSLT transformations
o Web APIs (such as SOAP)
o XML-based data processing tools
o Automation and testing tools (e.g., Selenium Web Driver)
How XPath Works
XPath uses path-like syntax, similar to file paths, to navigate XML elements.
Example XML
<Library>
<Book id="B101">
<Title>Data Structures</Title>
<Author>Ravi Kumar</Author>
<Price>500</Price>
</Book>
</Library>
Basic XPath Syntax and Selectors
XPath Expression Meaning / What it Selects
/Library Selects the root element <Library>
/Library/Book Selects the <Book> element inside <Library>
//Book Selects all Book elements in the entire document
/Library/Book/Title Selects the <Title> element
//Book[@id='B101'] Selects Book element with id attribute equals B101
//Price/text() Extracts the text content inside <Price>
Node Types XPath Can Select
• Element nodes → <Book>
• Attribute nodes → @id
• Text nodes → text inside tags
• Parent / child / sibling relationships
Advantages of XPath
• Works with both simple and complex XML structures.
• Highly efficient for searching data in XML trees.
• Foundation for higher-level query tools like XQuery.
XQUERY
While XPath helps in navigation, XQuery helps in querying and retrieving data, similar to
SQL for databases.
Purpose of XQuery
• Extract data from XML documents.
• Perform filtering, sorting, grouping, and joining operations.
Used in:
o Native XML Databases (e.g., BaseX, eXist-db)
o Enterprise Integration Systems
o Web Services
o Data Analytics Platforms
XQuery vs SQL
Feature SQL XQuery
Operates on Tables (rows & columns) XML trees (nodes, elements)
Data Structure Relational Hierarchical
Output Tabular data XML, HTML, or text
FLWOR Expression Structure (Core of XQuery)
FLWOR → FOR, LET, WHERE, ORDER BY, RETURN
It resembles natural programming & SQL-like query processing.
Part Meaning Purpose
FOR Iterates over a sequence of XML nodes Acts like a loop
LET Assigns values to variables Useful for optimization
WHERE Applies conditions / filters Similar to SQL WHERE
ORDER BY Sorts the results Ascending / Descending
RETURN Defines what output should be generated Final structured result
Example XQuery Using FLWOR
XML Input
<Library>
<Book>
<Title>AI Foundations</Title>
<Author>Dr. Rao</Author>
<Price>750</Price>
</Book>
<Book>
<Title>Machine Learning</Title>
<Author>Prof. Anita</Author>
<Price>900</Price>
</Book>
</Library>
XQuery
for $b in /Library/Book
where $b/Price > 800
order by $b/Title
return
<ExpensiveBook>
<Name>{ $b/Title/text() }</Name>
<Cost>{ $b/Price/text() }</Cost>
</ExpensiveBook>
Output
<ExpensiveBook>
<Name>Machine Learning</Name>
<Cost>900</Cost>
</ExpensiveBook>
Where XML Queries Are Used (Real World Applications)
Area Usage
Business Data Analytics Extracting structured information from XML-based data warehouses.
Web Services (SOAP/REST) Many APIs return data in XML → XPath/XQuery helps parse it.
Database Systems Querying Native XML Databases and XML-enabled RDBMS like
Oracle / SQL Server.
Configuration and Enterprise Many enterprise software store config/settings in XML which Systems are queried using XPath/XQuery.
No comments:
Post a Comment