XSLT allows you to create formatting structures which interpret and modify the existing XML elements. Learn about the syntax of XSLT elements, how the namespace attribute differs depending on the browser in use, and how to transform original XML elements.
Support files are available here for this article.
XSL or eXtensible Stylesheet Language began life in much the same way as XML -- as a submission to the W3C. The W3C quickly released several working drafts before the final working draft was released in 2000. It shared some common design goals with XML, namely:
A quick design process
Transparency to users and developers
Ease of use
Avoidance of optional features
Suitable for use over the Internet
As with XML, these goals were largely met. In October 2001, XSL was split into three separate working specifications by the W3C: XSLT, which is XSL Transformations, XSL-FO, which is XSL Formatting Objects, and Xpath, which defines parts of documents and linking structures. XSL-FO is now formally known as XSL. We will focus mainly on XSLT in the article, but XSL (XSL Formatting Objects) will be looked at briefly towards the end.
Every well-formed XML document has a tree-like structure consisting of parent and child elements. What XSLT does is allow you to create formatting structures which interpret and modify the existing XML elements and can output the results of these structures to HTML (or other output media). It allows you to transform a source tree into a result tree of XML elements.
Both XSLT and XSL must be well-formed documents, and as such XSLT documents must begin with the XML declaration. Following this comes the XSL:stylesheet element, which is the root element of the document and should contain at least the namespace declaration.
The transformation rules are then added as child elements of the root element. XSLT stylesheets are constructed using template rules that match one or nodes in the XML source documents logical tree. These template rules then create instances of fragments of the result tree. The XSLT result tree is composed of the hierarchy of nodes in the XML document to which it is applied.
The syntax of all XSLT elements is the same:
<xsl:tag-name attribute="value"> </xsl:tag-name>
These elements are reserved in the namespace URI that is set in the documents root element. Template rules also follow a similar syntax:
<xsl:template attribute="value"> </xsl:template>
There are many template element attributes, some of which we'll explore during the course of this article. As an example of XSLT techniques, we will be creating a stylesheet document that transforms the XML document created in my previous XML article.
To start with then, as stated above, XSLT documents should be well-formed XML documents, so you start with the XML declaration:
You then need to add the root element of the stylesheet document with the namespace declaration:
At this point it should be noted that the namespace attribute for XSLT documents differs depending on the browser in use. The namespace declaration used here is for IE5+ browsers. If IE5+ will not be used the following namespace declaration can be substituted: