XSLT facts for kids
Paradigm | Declarative |
---|---|
Developer | World Wide Web Consortium (W3C) |
First appeared | 1998 |
Stable release |
3.0 / June 8, 2017
|
Filename extensions | .xslt |
Major implementations | |
libxslt, Saxon, Xalan | |
Influenced by | |
DSSSL |
Filename extension |
.xslt |
---|---|
Internet media type |
application/xslt+xml
|
Uniform Type Identifier (UTI) | org.w3.xsl |
XSLT stands for Extensible Stylesheet Language Transformations. It's a special computer language. Its main job is to change XML documents into other XML documents. It can also turn XML into other formats. These include HTML for web pages, or just plain text.
Sometimes, these changed documents can then be turned into other files. Examples are PDF files or PNG images. Newer versions of XSLT can even work with JSON data. XSLT works with many programming languages. These include Java, Python, and PHP. You can also use it right inside your web browser.
XSLT tells a computer how to take an XML document and make a new one. This new document can be XML or another type, like plain text. Usually, the starting files are XML. But XSLT can work with data from other places too. This includes information from databases.
Even though XSLT was made for changing XML, it's powerful enough to do almost any computer task.
Contents
How XSLT Started
XSLT was inspired by other computer languages. These languages focus on how things work, not just step-by-step instructions. It also learned from languages that find patterns in text. A language called DSSSL was a direct ancestor. 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 by the World Wide Web Consortium (W3C). It happened between 1998 and 1999. This project also created other important web standards.
XSLT 1.0 was officially released in November 1999. Even though it's an older version, many people still use it. This is because newer versions don't always work directly in web browsers.
XSLT 2.0: New Features
After a try at version 1.1, a new group worked on XSLT 2.0. This version was released in January 2007. It brought many useful updates:
- It could handle text better using special patterns.
- It added ways to work with dates, times, and how long things last.
- It could create more than one output document at a time.
- It made it easier to group information together.
- It had a better system for checking data types.
XSLT 3.0: The Latest Version
XSLT 3.0 became an official W3C recommendation on June 8, 2017. It added some big improvements:
- Streaming transformations: Older versions needed to load the whole document into memory. XSLT 3.0 can process large documents piece by piece. This is helpful for very big files.
- Packages: This helps organize large XSLT files into smaller, reusable parts.
- Better error handling: It's easier to deal with problems that happen while XSLT is running.
- Support for maps and arrays: This means XSLT can now work with JSON data, not just XML.
- Higher-order functions: Functions can now be used as inputs for other functions.
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 processes them to make 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. It works by matching patterns. You define rules for how to handle parts of the XML document. The XSLT processor then applies these rules.
Here's how it generally works:
- First, the processor reads your XSLT stylesheet.
- Then, it builds a "tree" from your input XML document. Think of it like a family tree for your data.
- It starts at the top of this tree. It finds the best rule in your stylesheet for that part of the tree.
- The instructions in the rule tell the processor what to create in the new document. Or, they tell it to process more parts of the original XML tree.
- Finally, the new document is saved as XML or HTML text.
XPath: Finding Your Way in XML
XSLT uses something called XPath. XPath helps XSLT find specific parts within an XML document. It's like a map for navigating the XML tree. XPath also has many built-in functions. XSLT adds even more functions to this list.
XSLT 1.0 uses XPath 1.0. XSLT 2.0 uses XPath 2.0. The latest XSLT 3.0 works with XPath 3.0 or 3.1.
XSLT vs. XQuery
XSLT and XQuery are two different languages. But they can do some similar things. XQuery was first made for searching through large collections of XML documents.
Both XSLT 2.0 and XQuery 1.0 share some common parts. They use the same way to understand data. They also share a library of functions. Both include XPath 2.0.
However, they come from different backgrounds. XSLT was designed to make XML look good for people. It's often used to create web pages. XQuery was more like a database query language. It's similar to SQL, which is used for databases.
Because of their different origins, XSLT is better at handling documents that tell a story. These documents often have flexible structures. XQuery is stronger at handling data. For example, it's good at combining information from different sources.
Media Types
When you create an output with XSLT, you can tell it what kind of file it is. This is done using a "media-type" setting. For example, you can say it's an XML file.
For a long time, there wasn't an official media type for XSLT itself. So, `text/xsl` became a common, unofficial type. In 2007, with XSLT 2.0, the W3C suggested `application/xslt+xml` as the official type. This was later registered.
However, many web browsers, like Internet Explorer, still used `text/xsl`. This meant that if you wanted your browser to transform an XML document, you often had to use the unofficial `text/xsl` type. This situation was still true in 2021.
Examples of XSLT in Action
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 code changes the XML document above:
<?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>
After the XSLT runs, the new XML document looks like this:
<?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 for Web Pages
This XSLT code changes the XML into XHTML, which web browsers can display:
<?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 run this XSLT with the original XML, 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 then creates a web page that looks like this:
To make a web browser automatically use an XSLT file, you can add a special line to your XML document. For example, if your XSLT file is named "example2.xsl", you would add this to your XML:
<?xml-stylesheet href="example2.xsl" type="text/xsl" ?>
Even though `type="text/xsl"` isn't the official type, it's the one most browsers understood in 2009 and still do today.
XSLT Processors
An XSLT processor is a program that actually performs the transformations. Many different companies and groups have created XSLT processors.
- libxslt is a free software library. It's written in C, which makes it fast. It supports XSLT 1.0. Many Linux systems and macOS include a tool called `xsltproc` that uses libxslt. Web browsers like Safari and Chrome also use libxslt for XSL transformations.
- Microsoft has two XSLT 1.0 processors. One is part of MSXML, and another is in their .NET software.
- Saxon is a popular XSLT processor. It supports XSLT 3.0. It has both free and paid versions. Saxon can run on Java, JavaScript, and .NET.
- Xalan is another open-source XSLT 1.0 processor. It's from the Apache Software Foundation. A version of Xalan is included with Java.
- Web browsers like Safari, Chrome, Firefox, Opera, and Internet Explorer all support XSLT 1.0. They can transform XML files and show the results directly.
How Processors Perform
Many early XSLT processors were interpreters. This means they read and ran the code line by line. Newer processors often convert the XSLT code into another language first. This makes them run much faster.
Even older processors usually separate the reading and running steps. This means they can analyze the XSLT code once. Then, they can use that optimized version to transform many different XML documents. This is very helpful for websites that need to transform data quickly.
Modern XSLT processors use smart ways to speed things up. They can rearrange the code to make it more efficient. They also try to use less computer memory. This helps them work faster, especially with large files.
See also
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.
- XSL Formatting Objects – An XML-based language often created by XSLT. It's used to make formatted documents.