Skip to content

XSLT

XSLT (extensible stylesheet language for transformations) is an XML-based template language that converts the structure of XML elements into other documents.

This is a recommendation of the W3C (World Wide Web Consortium), as is CSS, but XSLT goes much further, as it overcomes many of the limitations of CSS.

With XSLT you can make transformations that completely change the structure of the document, you can rearrange the information in the XML document, add new information wherever you are, make decisions based on the information in it, make calculations, and so on.

XSLT relies on other XML technologies to work:

  • It uses XPath to determine which templates to apply at any given time (and is therefore integrated into XQuery). Supports XML Schemas to define data types.

XSLT templates are increasingly being used to generate custom views of an XML document for viewing in different destinations (printer, computer screen, mobile phone…). All you have to do is get the original XML document, and the others will be created on demand.

One of the limitations of XSL is that the input document must always be XML, even though the output does not have this limitation and can end up in any format: text, HTML, XML, etc.

XSLT - Transformation

XSLT is a declarative language. Therefore, XSLT stylesheets are not written as a sequence of instructions, but as a collection of template rules. Each template sets how a particular element is transformed (defined by XPath expressions). The document is transformed as follows:

  1. The processor scans the document and builds the document tree.
  2. The processor traverses the document tree from the root node.
  3. On each node traversed, the processor may or may not apply a template:
    • If no template can be applied to a node, its contents are included in the final document (the text of the node, not the text of the descending nodes). The processor then traverses its child nodes.
    • If a template can be applied to a node, the template is applied. The template can generate text that is included in the final document. In principle, the processor does not traverse its child nodes, unless the template tells the processor that the child nodes should be traversed.
  4. When the processor has traversed the tree, the transformation is complete.

More information at XSLT Tutorial at W3Schools

How to display transformations

We have several options:

  1. By adding a reference to the XSL stylesheet in the XML document and open it from a live server.
  2. By using a VSCode extension like XML extension by Red Hat.
  3. By using a CLI command like xsltproc.

XSLT elements

Our samples are based on this template:

<?xml version="1.0" encoding="UTF-8"?>
<biblioteca>
  <libro>
    <titulo>La vida está en otra parte</titulo>
    <autor>Milan Kundera</autor>
    <fechaPublicacion año="1973"/>
  </libro>
  <libro>
    <titulo>Pantaleón y las visitadoras</titulo>
    <autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
    <fechaPublicacion año="1973"/>
  </libro>
  <libro>
    <titulo>Conversación en la catedral</titulo>
    <autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
    <fechaPublicacion año="1969"/>
  </libro>
</biblioteca>

Line breaks and whitespace: the <xsl:text> and <xsl:strip-space> commands

When transforming a document, XSLT processors embed line breaks and white space in the output, but they do not do so consistently.

There doesn't seem to be a simple solution that works on all processors, but there are solutions that work on every processor.

The <xsl:strip-space> statement

The <xsl:strip-space> command allows you to indicate whether elements containing only whitespace are included in the transformation.

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

  <xsl:strip-space elements="*" />

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

  <xsl:template match="libro">
      <p><xsl:value-of select="autor"/></p>
  </xsl:template>

</xsl:stylesheet>

The output will be:

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <h1>Autores</h1>
  <p>Milan Kundera</p>
  <p>Mario Vargas Llosa</p>
  <p>Mario Vargas Llosa</p>
</html>

<xsl:text>

<xsl:text> allow you to generate text you cannot generate simply by adding it (line breaks, and whitespaces, for instance).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">
  <xsl:template match="/">
  <html>
    <xsl:text>&#10; </xsl:text>
      <h1>Authors</h1>
      <xsl:apply-templates />
  </html>
  </xsl:template>
  <xsl:template match="libro">
    <p><xsl:value-of select="autor"/></p>
  </xsl:template>
</xsl:stylesheet>
The output will be:

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <h1>Autores</h1>
  <p>Milan Kundera</p>
  <p>Mario Vargas Llosa</p>
  <p>Mario Vargas Llosa</p>
</html>

Creating attributes

The <xsl:attribute> command allows you to generate an attribute and its value.

From the following XML document, we want to generate the <img> tag. Where the value of the src attribute is the content of the <image> tag.

<?xml version="1.0" encoding="UTF-8"?>
<licenses>
  <license>
    <name>Creative Commons By - Share Alike</name>
    <image>cc-bysa-88x31.png</image>
  </license>
</licenses>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">

<xsl:template match="license">
  <p>
    <img src="<xsl:value-of select="imagen" />" />
  </p>
</xsl:template>

</xsl:stylesheet>

Error at line 5, column 19: not well-formed (invalid token)

  <?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="1.0">
    <xsl:template match="license">
      <p><img>
      <xsl:attribute name="src">
        <xsl:value-of select="image" />
      </xsl:attribute>
      </img>
      </p>
    </xsl:template>
  </xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<p>
  <img src="cc-bysa-88x31.png"/>
</p>

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

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

      <xsl:template match="licencia">
        <p><img>
        <xsl:attribute name="src">
          <xsl:value-of select="imagen" />
        </xsl:attribute>
        </img>
        </p>
      </xsl:template>

    </xsl:stylesheet>

    <?xml version="1.0" encoding="UTF-8"?>
    <html>
      <p><img src="cc-bysa-88x31.png"/></p>
    </html>

Webography