XSL (eXtensible Stylesheet Language)
1. Definition
XSL is the W3C family of standards for styling and transforming XML documents — because XML has no fixed tags, it needs a style language that can be told how to format any vocabulary.
The XSL family:
| Part | Name | Purpose |
|---|---|---|
| XSLT | XSL Transformations | Transform XML → HTML, text, or another XML |
| XPath | XML Path Language | Address/select nodes inside an XML tree (used by XSLT) |
| XSL-FO | XSL Formatting Objects | Precise print/PDF page layout (now less used) |
2. Why XSL?
- XML separates data from presentation; XSL is the presentation layer.
- One XML source → many outputs: HTML for a browser, WML for a phone, PDF for print, RSS for syndication.
- Supports sorting, filtering, reordering, computed values — not just styling.
3. XSLT Working
XML document + XSLT style sheet --> XSLT PROCESSOR --> result (HTML/XML/text)
(input tree) (rules/templates) XPath selects (output tree)
An XSLT style sheet is itself an XML document containing templates; each template matches a node (via XPath) and emits output.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><body>
<h2>Student List</h2>
<table border="1">
<xsl:for-each select="class/student">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="marks"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
4. Important XSLT Elements
| Element | Purpose |
|---|---|
<xsl:stylesheet> / <xsl:output method="html"/> |
Root / output format |
<xsl:template match="…"> |
Rule applied to matched nodes |
<xsl:value-of select="…"/> |
Insert the text value of a node |
<xsl:for-each select="…"> |
Loop over selected nodes |
<xsl:if test="…"> / <xsl:choose> |
Conditional output |
<xsl:sort select="marks" order="descending"/> |
Sorting |
<xsl:apply-templates/> |
Continue processing children |
5. XPath in One Minute
| Expression | Selects |
|---|---|
/class |
Root element |
//student |
All student elements anywhere |
//student[marks > 80] |
Students with marks > 80 (predicate) |
//student/name/text() |
Text content |
@roll |
An attribute |
//book[3], last(), position() |
Positional selection |
6. XSL vs CSS
| Aspect | CSS | XSL |
|---|---|---|
| Designed for | HTML (and simple XML) | Any XML |
| Can reorder/rename content? | No (display only) | Yes — full transformation |
| Can compute, sort, filter? | No | Yes |
| Output | Styled page | New document (HTML/XML/text/PDF via FO) |
| Syntax | Property declarations | XML templates + XPath |
7. Exam Points
- What is XSL? Explain the parts (XSLT, XPath, XSL-FO) — 7 marks.
- Write an XSLT style sheet converting an XML student list to an HTML table — 10 marks (very frequent).
- Compare XSL and CSS — 5 marks.
- Basic XPath expressions — 5 marks.