kids encyclopedia robot

XSLT facts for kids

Kids Encyclopedia Facts
Quick facts for kids
XSLT
Paradigm Declarative
Developer World Wide Web Consortium (W3C)
First appeared 1998
Stable release
3.0 / June 8, 2017; 8 years ago (2017-06-08)
Filename extensions .xslt
Major implementations
libxslt, Saxon, Xalan
Influenced by
DSSSL
XSLT
Filename extension
.xslt
Internet media type
application/xslt+xml
Uniform Type Identifier (UTI) org.w3.xsl

XSLT stands for Extensible Stylesheet Language Transformations. It is a special computer language. XSLT was first made to change XML documents into other XML documents. It can also change them into other formats. These include HTML for web pages, plain text, or special formatting objects. These objects can then become files like PDF or PNG images. Later updates added support for JSON and plain text.

The newest stable version of XSLT is 3.0. It became an official recommendation in June 2017. Many programming languages support XSLT 3.0. These include Java, .NET, C/C++, Python, PHP, and NodeJS. You can even use an XSLT 3.0 library in a web browser. Older web browsers often support XSLT 1.0 directly.

When XSLT changes a document, the original document stays the same. XSLT creates a brand new document based on the old one. Usually, the starting documents are XML files. But XSLT can work with other data too. This includes information from relational databases.

Even though XSLT was made for transforming XML, it is also "Turing-complete." This means it can do almost any kind of computer calculation.

A Brief History of XSLT

XSLT was inspired by other programming languages. These include "functional languages" and text-matching languages like SNOBOL. Its closest ancestor is DSSSL. DSSSL did for SGML what XSLT does for XML.

XSLT 1.0: The First Version

XSLT 1.0 was part of a bigger project. This project was called the eXtensible Stylesheet Language (XSL). The World Wide Web Consortium (W3C) worked on it from 1998 to 1999. This project also created XSL-FO and XPath. XSLT 1.0 became an official W3C recommendation in November 1999. Even today, XSLT 1.0 is still used a lot. This is because newer versions are not built into web browsers.

XSLT 2.0: New Features Arrive

After a small attempt at version 1.1, work began on XSLT 2.0. This version worked with XPath 2.0. XPath 2.0 had a richer data model. XSLT 2.0 became an official recommendation in January 2007. Some important new features in XSLT 2.0 were:

  • Working with text using regular expressions.
  • Tools for handling dates, times, and how long things last.
  • The ability to create many output documents.
  • Better ways to group information.
  • A more detailed system for data types.

XSLT 3.0: The Latest Version

XSLT 3.0 became a W3C Recommendation on June 8, 2017. Here are its main new features:

  • Streaming transformations: Before, the whole document had to be loaded into memory. Now, XSLT 3.0 can process parts of a document as it reads them. This is great for very large documents.
  • Packages: These help organize large XSLT stylesheets.
  • Better error handling: It's easier to deal with problems that happen while processing.
  • Support for maps and arrays: This lets XSLT work with JSON data, not just XML.
  • Higher-order functions: Functions can now be used as inputs for other functions.

How XSLT Works

XSLT en
This diagram shows how XSLT works.

An XSLT processor is like a special computer program. It takes one or more XML documents. It also takes one or more XSLT stylesheets. Then, it uses them to create new documents. Unlike many programming languages, XSLT is "declarative." This means you tell it *what* you want, not *how* to do it step-by-step.

XSLT works by "pattern matching." You define rules for how to handle parts of the document that match certain patterns. These rules are called "templates." When the processor finds a part that matches a pattern, it applies the template. The template tells the processor what to create in the new document.

Here is how a typical XSLT processor works:

  • First, it reads and prepares the XSLT stylesheet.
  • Then, it builds a "source tree" from the input XML document. Think of it like a family tree for your data.
  • Next, it starts at the "root" (top) of the source tree. It finds the best matching template in the stylesheet for that part.
  • The instructions in the template tell the processor what to do. They might tell it to create new parts in the "result tree." Or they might tell it to process more parts of the source tree.
  • Finally, the result tree is turned into XML or HTML text. This is the output document.

Using XPath with XSLT

XSLT uses another language called XPath. XPath helps XSLT find specific parts within the source document tree. It also helps XSLT do calculations. XPath also has many built-in functions. XSLT adds even more functions to these.

XSLT 1.0 uses XPath 1.0. XSLT 2.0 uses XPath 2.0. XSLT 3.0 can work with either XPath 3.0 or 3.1. The XPath and XSLT standards were released at different times for version 3.0.

XSLT vs. XQuery

XSLT and XQuery have some similar features. XQuery was first made to search through large collections of XML documents.

Both XSLT 2.0 and XQuery 1.0 standards were developed by different groups at the W3C. But they worked together. This made sure they shared the same data model, type system, and function library. Both also use XPath 2.0 as a part of their language.

However, these two languages come from different backgrounds. XSLT was mainly designed as a "stylesheet" language. Its main goal was to display XML data for people to read. This could be on a screen, on the web, or on paper. XQuery was more like a database query language, similar to SQL.

Because they came from different communities, XSLT is better at handling documents that tell a story. These documents often have flexible structures. XQuery is better at handling data. For example, it's good at combining information from different tables.

Media Types for XSLT Output

When XSLT creates an output document, it can specify its "media type." This is also known as a MIME type. For example, <xsl:output output="xml" media-type="application/xml"/> tells the computer that the output is an XML file.

For a long time, there was no official media type for XSLT. So, text/xsl became a common choice. In 2007, the W3C suggested registering application/xslt+xml. This type was later officially registered.

However, many web browsers, especially older ones, still recognize text/xsl. If you want a browser to apply an XSL transformation, you often have to use this older type.

XSLT Examples

Let's look at some examples using this XML document:

<?xml version="1.0" ?>
<persons>
  <person username="JS1">
    <name>John</name>
    <family-name>Smith</family-name>
  </person>
  <person username="MI1">
    <name>Morka</name>
    <family-name>Ismincius</family-name>
  </person>
</persons>

Example 1: Changing XML to XML

This XSLT stylesheet changes the XML document:

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

  <xsl:template match="/persons">
    <root>
      <xsl:apply-templates select="person"/>
    </root>
  </xsl:template>

  <xsl:template match="person">
    <name username="{@username}">
      <xsl:value-of select="name" />
    </name>
  </xsl:template>

</xsl:stylesheet>

The result is a new XML document with a different structure:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name username="JS1">John</name>
  <name username="MI1">Morka</name>
</root>

Example 2: Changing XML to XHTML

This XSLT file changes the XML input into XHTML. XHTML is a type of HTML for web pages.

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

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

  <xsl:template match="/persons">
    <html>
      <head> <title>Testing XML Example</title> </head>
      <body>
        <h1>Persons</h1>
        <ul>
          <xsl:apply-templates select="person">
            <xsl:sort select="family-name" />
          </xsl:apply-templates>
        </ul>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="person">
    <li>
      <xsl:value-of select="family-name"/><xsl:text>, </xsl:text><xsl:value-of select="name"/>
    </li>
  </xsl:template>

</xsl:stylesheet>

When you process the XML input with this XSLT, you get this XHTML:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head> <title>Testing XML Example</title> </head>
  <body>
    <h1>Persons</h1>
      <ul>
        <li>Ismincius, Morka</li>
        <li>Smith, John</li>
      </ul>
  </body>
</html>

This XHTML code will look like this when viewed in a web browser:

Xslt ex2
This image shows the XHTML output from the XSLT transformation.

To make a web browser apply an XSL transformation, you can add a special instruction to the XML document. For example, if the stylesheet above is named "example2.xsl," you would add this line to your XML file:

<?xml-stylesheet href="example2.xsl" type="text/xsl" ?>

Even though text/xsl is not the official type, it works best in most browsers.

XSLT Processors: The Tools That Do the Work

An XSLT processor is a program that performs the transformations. Here are some common ones:

  • RaptorXML from Altova supports XSLT 3.0. It's part of a development kit.
  • IBM offers XSLT processing in special hardware called Datapower.
  • libxslt is a free library. It's written in C for speed. It supports XSLT 1.0.
    • You can use it from the command line with `xsltproc`. This tool is on macOS and many Linux distributions.
    • Web browsers like Safari and Chrome use libxslt for XSL transformations.
  • Microsoft has two XSLT 1.0 processors. One is MSXML, and the other is in the .NET system.
  • Saxon is a popular XSLT 3.0 and XQuery 3.1 processor. It has both free and paid versions. It works with Java, JavaScript, and .NET.
  • Xalan is an open source XSLT 1.0 processor from the Apache Software Foundation. It works with Java and C++.
  • Web browsers like Safari, Chrome, Firefox, Opera, and Internet Explorer support XSLT 1.0. They can transform XML files and show the results.

Images for kids

See also

Kids robot.svg In Spanish: Extensible Stylesheet Language Transformations para niños

  • XSLT elements – A list of common parts used in XSLT.
  • eXtensible Stylesheet Language – The family of languages that XSLT belongs to.
  • XQuery and XSLT compared – More details on how XQuery and XSLT are different.
  • XSL formatting objects or XSL-FO – An XML-based language for documents, often made by XSLT.
  • Identity transform – A basic transformation that copies an XML document without changes.
  • Apache Cocoon – A Java-based system for processing data using XSLT.
kids search engine
XSLT Facts for Kids. Kiddle Encyclopedia.