Extensible Markup Language(XML): Introduction Creating XML Documents XML style Sheet Hyperlinks in XML Document Object Model XML Query Language
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>
No comments:
Post a Comment