What is XSLT?

What Is XSLT

Wikipedia defines XSLT as follows: XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or into XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF PostScript and PNG.

The XSLT language defines a method to transform a source XML document into a target document. A transformation expressed in XSLT is called a stylesheet which is a well-formed XML document. An XSLT stylesheet defines the constructs and properties similar to a standard programming language; in this case a programming language aimed at transforming an XML document: XSLT is a Turing-complete language.


  • Library Imports: Include external XSL documents
  • Variables: Global and Local scoped-variables are defined
  • Templates & Pattern Matching: similar to method and functions. A template is callable by name or executes whenever a pattern is matched.
    • Common programming constructs are provided:
    • Enumerations & Looping – For/next loops; Sorting
    • Conditional statements – If/ Then and Switch Case
  • The XSL language focuses on XML transforms; as a result it has several XML functions:
    • Node Operations: Select, copy
    • Node Definitions: Element, Attributes


In short: A stylesheet defines a program with an expected XML input and a defined output – be it XML, HTML or text.

XSLT-functions are distinguished from normal XML through the XSL namespace. An XSLT / XSL stylesheet elements is  easily identifiable through the inclusion of the XSL namespace:

  • xsl:import – Imports XSL libraries
  • xsl:output – Defines the output formation
  • xsl:variable – Defines a variable(global or local)
  • xsl:template – Defines a ‘method’ in XSL
  • xsl:apply-template – Tries to match a template to a pattern in a prioritized order, given a selected value/node.
  • xsl:apply-templates – Search for and applies the highest priority template to each node defined by a templates’ match pattern.
  • xsl:call-template – Invokes a template ‘method’ by name
  • xsl:parm – A template ‘method’ parameter
  • xsl:value-of: retrieves the value of an XML node, XSL variable or XPATH function
  • xsl:copy-of: creates a deep copy of an XML node
  • xsl:for-each: Loops through a node list
  • xsl:sort – sorts a node list selected through a for-each
  • xsl:if – a conditional statement expressed as an XPATH Boolean expression
  • xsl:choose, xsl:when, xsl:otherwise – a conditional switch / case / default statement and construct. Conditional statements are Boolean XPATH expressions.
  • xsl:element – defines a new XML element to output
  • xsl:attribute – defines an attribute on a new XML element

XSLT makes use of the expression language defined by XPATH for selecting elements for processing, for conditional processing and for generating text. Refer to the XPATH Tutorial for an introduction to XPATH expressions and functions.

An XSLT transformation is applied through an XSL processor – which is platform dependent. The XSL processor takes two inputs: the original XML document and the XSLT transformation. A new output/result document is generated. The original document is not changed by this process. The following diagram describes the process:

Diagram of the basic elements and processing flow of XSL Transformations

 

Refer to the XSLT example to see how each platform such as Java or .NET (C# / Visual Basic) implements the XSLT processors’ .

Posted in XSLT and tagged .

Leave a Reply