Introduction to XSL

XSL stands for E X tensible Stylesheet Language. The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language.



Introduction to XSL


XSL stands for E X tensible Stylesheet Language. The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language. Thus it is a language for expressing Stylesheets.

A stylesheet specifies the presentation of XML information using two basic categories of techniques:

  • An optional transformation of the input document into another structure.
  • A description of how to present the transformed information.

The components of the XSL language

The full XSL language logically consists of three component languages, which are described in three W3C (World Wide Web Consortium) Recommendations:

  1. XPath: XML Path Language is an expression language used by XSLT to access or refer specific parts of an XML document
  2. XSLT: XSL Transformations is a language for describing how to transform one XML document (represented as a tree) into another.
  3. XSL-FO: Extensible Stylesheet Language Formatting Objects is a language for formatting XML documents and Formatting Properties.

Understanding XSL Stylesheet Structure

(a) XSLT namespace

The XSL stylesheet starts with the root element <xsl:stylesheet> or <xsl:transform> that declares the document to be an XSL style sheet.

The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:

<?xml version="1.0" ?>

<xsl:stylesheet version="1.0"



<xsl:transform version="1.0"


Since an XSL style sheet is an XML document itself, it always begins with the XML declaration: <?xml version="1.0" ?>

To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.

The xmlns:xsl="" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0". This specification uses a prefix of xsl: for referring to elements in the XSLT namespace. However, XSLT stylesheets are free to use any prefix.

Now set it up to produce HTML-compatible output:


  <xsl:output method="html"/>



(b) Stylesheet Element

The <xsl:template> Element

An XSL style sheet consists of a set of rules that are called templates. Each template "matches" some set of elements in the source tree and then describes the contribution that the matched element makes to the result tree.

Most templates have the following form:

<xsl:template match="/">





Before processing can begin, the part of the XML document with the information to be copied to the output must be selected with an XPath expression. The selected section of the document is called a node and is normally selected with the match operator.

In the above statements, the <xsl:template> element defines a template. The match="/" attribute associates the template with the root node of the XML source document. Another approach is to match the document element (the element that includes the entire document).

The <xsl:apply-templates> Element

The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.
If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.

The <xsl:value-of> Element

The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation. For example, the given expression will select the value of Emp_Id attribute of the specified element and write to the output:

<xsl:value-of select="Emp_Id"/>


<xsl:value-of select="Employee-Detail/Employee/Emp_Id"/>

Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.

The <xsl:for-each select="elementName "> Element

The 'for-each' expression is a loop that processes the same instructions for these elements. The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set. For example, the given expression finds all ?Employee' elements in the ?Employee-Detail' element context using the XPath expression ?Employee-Detail/ Employee'. If the selected node contains all elements in the root, all of the ?Employee-Detail' elements will be selected.

<xsl:for-each select="Employee">

<xsl:value-of select="Emp_Id"/>

<xsl:value-of select="Emp_Name"/>