XML holds data but says nothing about how to display it. XSL describes how to transform and present it. XHTML applies XML's strict rules to HTML.
XSL and XSLT
XSL (eXtensible Stylesheet Language) is a family of languages for transforming and presenting XML. XSLT (XSL Transformations) transforms an XML document into another document, most often HTML or XHTML for display, or another XML format or text. XPath selects parts of the XML tree, with expressions such as /students/student/name. A third part, XSL-FO, formats documents for print. An XML file is linked to its stylesheet with the processing instruction <?xml-stylesheet type="text/xsl" href="students.xsl"?>, and the browser applies the transformation when it opens the XML file.
Definition: XSLT. XSL Transformations, a language for transforming an XML document into another document, such as HTML, using templates and XPath expressions.
XSL Elements
An XSLT stylesheet is an XML document with root <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">. <xsl:template match="/"> defines a template applied to the matched nodes. <xsl:value-of select="name"/> outputs the value of a node. <xsl:for-each select="students/student"> loops over nodes. <xsl:sort select="name"/> sorts them. <xsl:if test="..."> and <xsl:choose> with <xsl:when> and <xsl:otherwise> make decisions. <xsl:apply-templates> applies other templates.
Transforming with XSLT
The processor reads the XML tree, starts at the root template, and builds the output: for each student it writes an HTML table row with the values. The same XML data can thus be shown in several ways by using different stylesheets, which separates data from presentation.
XHTML
XHTML (eXtensible HyperText Markup Language) is HTML rewritten to follow the strict rules of XML. In XHTML, all elements must be closed (<br/>, <img ... />), tags and attributes must be in lower case, attribute values must be quoted, elements must be properly nested, there must be one root html element with the XHTML namespace, and attributes cannot be minimised (checked="checked", not just checked). XHTML documents can be processed by XML tools and are stricter and cleaner. HTML5 has largely replaced XHTML, but its good habits remain best practice.
Table 4.5: HTML and XHTML
| Rule | HTML | XHTML |
|---|---|---|
| Closing tags | Some optional | All required |
| Case | Any | Lower case |
| Attribute values | Quotes optional | Quotes required |
| Empty elements | <br> | <br/> |
Example 4.5: Transforming student XML into an XHTML table
Problem: Write an XSLT stylesheet that turns the student XML file into an XHTML page with a table of register number, name and email, sorted by name.
Solution:
- Link the stylesheet in the XML file with xml-stylesheet.
- In a template matching "/", write the html, body and table.
- Use xsl:for-each over students/student with xsl:sort on name.
- Output each field with xsl:value-of inside td cells.
Result: Opening the XML file in a browser shows a neat table of students.
<?xml version="1.0" encoding="UTF-8"?>
<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">
<tr><th>Reg. No.</th><th>Name</th><th>Email</th></tr>
<xsl:for-each select="students/student">
<xsl:sort select="name"/>
<tr>
<td><xsl:value-of select="regno"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="email"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
Common mistakes
- Forgetting the xsl namespace in the stylesheet element.
- Writing select paths that do not match the XML structure.
- Leaving empty elements unclosed in XHTML.
Key takeaways
- XSLT transforms XML into HTML, XHTML, XML or text.
- XPath selects nodes; xsl:for-each, value-of, sort and if do the work.
- Linking: <?xml-stylesheet type="text/xsl" href="..."?>.
- XHTML is HTML with XML's strict rules.
Practice questions
- What is XSLT used for?
- What does <xsl:value-of select="name"/> do?
- Give two rules of XHTML.
Answers to the practice questions
- To transform XML documents into other formats, such as HTML or XHTML.
- It outputs the value of the name node.
- All elements must be closed, and tags and attributes must be in lower case (also quoted attributes).
Sources
- Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit IV
