XML XSLT

XSLT stands for eXtensible Stylesheet Language Transformations. It is a language used to transform the content of an XML document into a different format — such as HTML for a webpage, plain text for a report, or even a different XML structure entirely.

Think of XSLT as a recipe: it takes an XML document (the ingredients) and a stylesheet (the recipe instructions), and produces a completely new document (the finished dish).

How XSLT Works

XSLT transformation involves three components:

  1. Source XML Document: The input data.
  2. XSLT Stylesheet: The transformation rules written in XSLT syntax.
  3. Output Document: The result — usually HTML, plain text, or new XML.

The transformation is performed by an XSLT processor, which reads both the XML and the stylesheet, then produces the output. Most browsers have built-in XSLT processors, and all major programming platforms provide XSLT libraries.

XSLT Stylesheet Structure

An XSLT file is itself a valid XML document. The root element is <xsl:stylesheet> or <xsl:transform>, and it uses the XSLT namespace.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Transformation rules go here -->

</xsl:stylesheet>

The Source XML Document

The following XML file is used in all examples in this topic:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
  <employee id="E01">
    <name>Sara Kim</name>
    <department>Marketing</department>
    <salary>55000</salary>
  </employee>
  <employee id="E02">
    <name>James Osei</name>
    <department>Engineering</department>
    <salary>82000</salary>
  </employee>
  <employee id="E03">
    <name>Priya Sharma</name>
    <department>Marketing</department>
    <salary>61000</salary>
  </employee>
</employees>

Template Rules: The Core of XSLT

The main building block of an XSLT stylesheet is the template rule, defined with <xsl:template>. A template rule matches a specific part of the XML source (using an XPath expression) and specifies what to output for that match.

<xsl:template match="pattern">
  <!-- Output content here -->
</xsl:template>

Example 1: Transform XML to an HTML Table

File: employees.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Match the root of the document -->
  <xsl:template match="/">
    <html>
      <body>
        <h2>Employee List</h2>
        <table border="1">
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Department</th>
            <th>Salary</th>
          </tr>
          <!-- Loop through each employee -->
          <xsl:for-each select="employees/employee">
            <tr>
              <td><xsl:value-of select="@id" /></td>
              <td><xsl:value-of select="name" /></td>
              <td><xsl:value-of select="department" /></td>
              <td><xsl:value-of select="salary" /></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

This stylesheet produces an HTML page with a table of employees. The XML employees data is now presented as a formatted web page.

Key XSLT Elements

<xsl:value-of>

Extracts the text value of an element or attribute and inserts it into the output.

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

This outputs the text content of the <name> element.

<xsl:for-each>

Loops through a set of nodes selected by an XPath expression.

<xsl:for-each select="employees/employee">
  <p><xsl:value-of select="name" /></p>
</xsl:for-each>

<xsl:if>

Outputs content only when a condition is true.

<xsl:for-each select="employees/employee">
  <xsl:if test="salary > 60000">
    <p><xsl:value-of select="name" /> — High Earner</p>
  </xsl:if>
</xsl:for-each>

Only employees with a salary greater than 60,000 will be listed.

<xsl:choose>, <xsl:when>, <xsl:otherwise>

Works like an if-else-if chain to handle multiple conditions.

<xsl:for-each select="employees/employee">
  <xsl:choose>
    <xsl:when test="salary > 75000">
      <p><xsl:value-of select="name" /> — Senior Level</p>
    </xsl:when>
    <xsl:when test="salary > 55000">
      <p><xsl:value-of select="name" /> — Mid Level</p>
    </xsl:when>
    <xsl:otherwise>
      <p><xsl:value-of select="name" /> — Junior Level</p>
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each>

<xsl:sort>

Sorts the selected nodes before outputting them.

<xsl:for-each select="employees/employee">
  <xsl:sort select="name" order="ascending" />
  <p><xsl:value-of select="name" /></p>
</xsl:for-each>

This will output employee names in alphabetical order.

<xsl:apply-templates>

A more flexible way to process children — it tells the XSLT processor to find and apply the appropriate template for each child node.

<xsl:template match="/">
  <html><body>
    <xsl:apply-templates />
  </body></html>
</xsl:template>

<xsl:template match="employee">
  <p><xsl:value-of select="name" /></p>
</xsl:template>

Linking an XSLT Stylesheet to an XML File

A processing instruction at the top of the XML file links it to an XSLT stylesheet. Modern browsers will then apply the transformation when the XML file is opened.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="employees.xsl"?>
<employees>
  ...
</employees>

XSLT Output Methods

The <xsl:output> element at the top of the stylesheet specifies the type of output being produced.

<xsl:output method="html" indent="yes" encoding="UTF-8" />

The method attribute can be html, xml, or text.

Key Points

  • XSLT transforms XML documents into HTML, plain text, or other XML formats.
  • A transformation requires a source XML file and an XSLT stylesheet.
  • Template rules (<xsl:template match="...">) define what to output for matched XML nodes.
  • <xsl:value-of> extracts values; <xsl:for-each> loops through nodes.
  • <xsl:if> and <xsl:choose> implement conditional logic.
  • <xsl:sort> orders output; <xsl:apply-templates> applies rules recursively.
  • XSLT stylesheets use XPath extensively to select and navigate XML nodes.

Leave a Comment

Your email address will not be published. Required fields are marked *