What is XSL?
XSL is a language for expressing style sheets. An XSL style sheet is, like
with CSS, a file that describes how to display an
XML document of a given type. XSL shares the functionality and is compatible
with CSS2 (although it uses a different syntax). It also adds:
- A transformation language for XML documents: XSLT.
Originally intended to perform complex styling operations, like the
generation of tables of contents and indexes, it is now used as a general
purpose XML processing language. XSLT is thus widely used for purposes
other than XSL, like generating HTML web pages from XML data.
- Advanced styling features, expressed by an XML document type which
defines a set of elements called Formatting Objects, and
attributes (in part borrowed from CSS2 properties and adding more complex
ones.
How Does It Work?
Styling requires a source XML documents, containing the information that
the style sheet will display and the style sheet itself which describes how
to display a document of a given type.
The following shows a sample XML file and how it can be transformed and
rendered.
The XML file
<scene>
<FX>General Road Building noises.</FX>
<speech speaker="Prosser">
Come off it Mr Dent, you can't win
you know. There's no point in lying
down in the path of progress.
</speech>
<speech speaker="Arthur">
I've gone off the idea of progress.
It's overrated
</speech>
</scene>
This XML file doesn't contain any presentation information, which is
contained in the stylesheet. Separating the document's content and the
document's styling information allows displaying the same document on
different media (like screen, paper, cell phone), and it also enables users
to view the document according to their preferences and abilities, just by
modifying the style sheet.
The Stylesheet
Here are two templates from the stylesheet used to format the XML file.
The full stylesheet (which includes extra
information on pagination and margins) is available.
...
<xsl:template match="FX">
<fo:block font-weight="bold">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="speech[@speaker='Arthur']">
<fo:block background-color="blue">
<xsl:value-of select="@speaker"/>:
<xsl:apply-templates/>
</fo:block>
</xsl:template>
...
The stylesheet can be used to transform any instance of the DTD it was
designed for. The first rule says that an FX element will be transformed into
a block with a bold font. <xsl:apply-templates/>
is a
recursive call to the template rules for the contents of the current element.
The second template applies to all speech elements that have the speaker
attribute set to Arthur
, and formats them as blue blocks within
which the value speaker attribute is added before the text.
Rendering
The figure below shows a rendering of the Formatting Objects generated by
the XML file and the stylesheet. The XSL-FO vocabulary is designed to be
displayed on a wide variety of media: screen, paper, or even voice. The full PDF document is also available.
Frequently Asked Questions
- How is XSL different from CSS?
- XSL uses a XML notation, CSS uses its own. In CSS, the formatting
object tree is almost the same as the source tree, and inheritance of
formatting properties is on the source tree. In XSL, the formatting
object tree can be radically different from the source tree, and
inheritance of formatting properties is on the formatting object tree.
Most modern Web browsers support both CSS and XSLT; there
are also many stand-alone XSLT implementations.
- Will XSL replace CSS?
- No. They are likely to co-exist since they meet different needs. XSL
is intended for complex formatting where the content of the document
might be displayed in multiple places; for example the text of a
heading might also appear in a dynamically generated table of contents.
CSS is intended for dynamic formatting of online documents for multiple
media; its strictly declarative nature limits its capabilities but also
makes it efficient and easy to generate and modify in the
content-generation workflow. So they are two different tools; for some
tasks, CSS is the appropriate choice and for some tasks, XSL. They can
also be used together - use XSL on the server to condense or customize
some XML data into a simpler XML document, then use CSS to style it on
the client.
- How is XSL different from DSSSL? From DSSSL-O?
- DSSSL is an International Standard style sheet language. It is
particularly used for formatting of print documents. DSSSL-O is a
profile of DSSSL which removes some functionality and adds capabilities
to make it more suited for online documentation. XSL draws on DSSSL and
the DSSSL-O work and continues the trend towards a Web-oriented style
sheet language by integrating experience with CSS.
- Will XSL replace DSSSL?
- DSSSL has capabilities that XSL does not, and continues in use in the
print publishing industry. Experience with XSL might be used in a
future revision of DSSSL, but it is too early to say.
- So, CSS is for HTML and XSL is for XML?
- No, CSS can be used with HTML and also with XML, provided that the
XML document has a reasonably linear structure that can be displayed
without extensive manipulation. See the CSS2
Recommendation for details.
XSL is targeted at XML, in particular highly-structured, data-rich
documents that require extensive formatting.
- Should I render all my XML documents to HTML on the server?
- Unless you are very careful to retain semantics, no. XSL can be used
server-side and client-side. The XSL Submission has two classes of
output: DSSSL-style flow objects and HTML tags. Unfortunately, the
combination of server-side processing and HTML tag output can result in
completely inaccessible, hard to search, hard to index presentational
HTML (the sort that is a mass of FONT and BR tags, spacer gifs - you
know, the sort of single-shot presentational mess that style sheets were designed to avoid).
The trouble is that by "rendering" to HTML, all that remains of your
carefully crafted XML semantics are the presentational aspects - block
element, this font, that weight - which makes it hard to generate
decent HTML.
- Technical: how do I do X, Y or Z in XSL?
- First, have a look at D. Pawson's excellent XSL FAQ. If you
don't find an answer, check the XSL mailing list at
mulberrytech.com