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.



JAXB Create XML File And Get Data From XML XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials The Simple API for XML (SAX) APIs Part I XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials XML Tutorial Ignoring Comments While Parsing an XML File Accessing XML file from Java Preventing Expansion of Entity References Getting next Tag in the XML File Replacing a Node with a New One Reading XML Data from a Stream Reading an XML document using JDOM Query XML with an XPath expression Normalize All of the Text in a Document Modifying Text by Replacement Insert a Processing Instruction and a Comment Node Getting text values from a NodeList Finding an ID Value and Print the Element Text Edit Text by Insertion and Replacement Duplicating a Subtree Deleting Two Attributes Delete the First Child of the Root Node Creating XML Tree Creating a DocumentFragment Subtree and Appending it to the Document Creates element node, attribute node, comment node, processing instruction and a CDATA section Creates a New DOM Parse Tree Replacing a Text Node with a New CDATA Section Node Create an Empty DOM Document Adding an Attribute in DOM Document Transforming XML with XSLT Transforming an XML File with XSL Transforming XML with SAXFilters Storing properties in XML file Testing EntityReferences in Xml Splitting One Text Node into Three Reading XML from a File Modifying Text by Cutting and Pasting Locating a Node by Using Siblings Locate a Node and Change Its Content Loading properties from a XML file Listing nodes used in a document Inserting a New Entry in a List Creating an InlineStreamFilter Ignoring Comments While Parsing an XML File Handling Errors While Parsing an XML File Emitting DOCTYPE Declaration while writing XML File Converting CDATA Nodes into Text Nodes Java DOM Tutorial Creating Blank DOM Document

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"/>




Share on Google+Share on Google+

Introduction to XSL

Posted on: January 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Introduction to XSL  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image