Your first transformation¶
The smallest useful stylesheet matches the document root and emits some HTML. This one turns the CD catalog into a table.
match="/"matches the document root — the rule fires once, for the whole document. Everything inside is the template for what to output.xsl:for-eachloops over every<cd>element under<catalog>.xsl:value-ofpulls out the text of a child element and writes it into the result.
Anatomy of a stylesheet¶
A stylesheet is itself a well-formed XML document.
- The root element is
xsl:stylesheet(or its synonymxsl:transform). - The
xsl:prefix is bound to the namespacehttp://www.w3.org/1999/XSL/Transform. The processor uses this namespace to tell instructions (things it should execute) apart from literal result elements (things it should copy to the output, like<html>and<table>). version="1.0"selects the XSLT version. 1.0 is universally supported; 2.0 and 3.0 add more features but need a processor like Saxon.
The result¶
Applied to the catalog, this produces an HTML table:
My CD Collection
| Title | Artist |
|---|---|
| Empire Burlesque | Bob Dylan |
| Hide your heart | Bonnie Tyler |
| Greatest Hits | Dolly Parton |
Next¶
The single big template works, but it does not scale: as documents grow you end up with one giant rule. Templates show how to break the work into small, composable pieces.