How Stylesheets Work
Page Index
Introduction
Stylesheets can be used for any (or all) of the following purposes:
- To format XML documents for presentation or printout.
- To convert other XML-based stylesheets into a recommended format.
- To create stylesheet variants from another stylesheet.
- To convert XML documents that conform to one schema into documents that conform to others, making information much easier to pass back and forth between different systems.
- To convert XML documents to non-XML plain text formats.
Why XSL Stylesheets specifically?
XSL (Extensible Stylesheet Language) is the W3C recommended stylesheet standard
An XML document typically contains no information on presentation: it is simply a representation of the information hierarchy present in the document. For example, a sentence might be represented in XML as follows.
<sentence>
<noun>Jack</noun>
<verbphrase>
<verb>kissed</verb>
<noun>Jill</noun>
</verbphrase>
</sentence>
While this is rich in information that might be useful to a syntactician, the non-linguist would prefer to view this sentence without all the syntactic markup and non-linear formatting, probably marked-up with simply a period at the end. Unfortunately, no web-browser yet developed knows what a sentence, verbphrase or noun tag means, let alone how to display it.
Therefore there must be something in addition to the XML document that describes how the document should be displayed; and that is XSL!
How does XSL work?
XSL consists of three parts:
-
XSLT, a language for transforming XML documents. Transformations are carried out by Templates which tell the web-browser how to display certain elements. For example:
<xsl:template match="sentence">
<xsl:apply-templates />
<xsl:text>.</xsl:text>
</xsl:template>
This template would simply display everything inside the sentence elements, and then the text "." (a period).
-
XPath, a language for specifying elements in an XML document. Enclosed in square brackets, XPath expressions allow further specification of elements, beyond simply their name.
<xsl:template match="noun[parent::verbphrase]">
<!-- syntactic objects -->
</xsl:template>
<xsl:template match="noun">
<!-- syntactic subjects -->
</xsl:template>
The XPath expression above can be read as "match all noun elements, such that its parent element is a verbphrase.
-
XSL Formatting Objects, a vocabulary for formatting XML documents for print.
Since this part of XSL is more of use to professional publishers and typesetters, we will not deal with it here. More information is available at O'Reilly Media's xml.com.
Simply put, XSLT is the most important part of the XSL standard. It is used to transform an XML document into another XML document, or any plain text format. One such format is XHTML. Normally, XSLT does this by transforming each XML element into an XHTML element.
With XSLT you have the ability to control and manipulate the elements in the output file. You can add new elements, remove elements, and rearrange and sort elements. You can also test to make decisions about which elements to display.
| Related Links | |
|---|---|
|
About Stylesheets How XSL Works Example Files ![]() How to Use Stylesheets |
|
| User Contributed Notes How Stylesheets Work |
+ Add a comment |
| + View comments |


