XSL Transformations

XSL Transformations, or XSLT, is an XML-based language used for transforming XML documents. It grew out of the XSL work that also produced XSL-FO. As with XML and HTML, the XSLT specification is a Recommendation developed by the W3C.

The language is declarative, i.e. an XSLT stylesheet consists of a collection of template rules, which each specify what to add to the result tree when the XSLT processor finds a node in the source tree that meets certain conditions. (To avoid the system-specific issues that come with addressing file I/O, the XSLT spec speaks of transforming a source tree to a result tree, although virtually all XSLT processors begin by reading an input document into a source tree and finish by writing the result tree to an output document.) The document created from the result tree may be XML, but doesn't have to be; it can be HTML, RTF, TeX, delimited files, or any other text-based format.

The W3C's XPath language for addressing subsets of a document tree adds a great deal of power to an XSLT stylesheet's ability to specify the transformations to perform on an XML source tree.

The W3C finalized the XSLT 1.0 specification in 1999 and the XSLT 2.0 specification currently has 'Working Draft' status.

Example

Example XSLT Stylesheet:

<?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" 
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
    
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                <title>test1</title>
                <style type="text/css">
                    h1          { padding: 10px; padding-width: 100%; background-color: silver }
                    td, th      { width: 40%; border: 1px solid silver; padding: 10px }
                    td:first-child, th:first-child  { width: 20% } 
                    table       { width: 650px }
                </style>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="domains/*">
        <h1><xsl:value-of select="@ownedBy"/></h1>
        <p>The following host names are currently in use at
          <b><xsl:value-of select="local-name(.)"/></b>
        </p>
        <table>
            <tr><th>Host name</th><th>URL</th><th>Used by</th></tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
    
    <xsl:template match="host">
        <xsl:variable name="url"
          select="normalize-space(concat('http://', normalize-space(node()), '.', local-name(..)))"/>
        <tr>
            <td><xsl:value-of select="node()"/></td>
            <td><a href="{$url}"><xsl:value-of select="$url"/></a></td>
            <xsl:apply-templates select="use"/>
        </tr>
    </xsl:template>

    <xsl:template match="use">
        <td><xsl:value-of select="."/></td>
    </xsl:template>
        
</xsl:stylesheet> 

Example of incoming XML for above stylesheet:

<?xml version="1.0" encoding="UTF-8"?>

<domains>
    <sun.com ownedBy="Sun Microsystems Inc.">
        <host>
            www
            <use>World Wide Web site</use>
        </host>
        <host>
            java
            <use>Java info</use>
        </host>
    </sun.com>
    
    <w3.org ownedBy="The World Wide Web Consortium">
        <host>
            www
            <use>World Wide Web site</use>
        </host>
        <host>
            validator
            <use>web developers who want to get it right</use>
        </host>
    </w3.org>
</domains>

Output XHTML that this would produce:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta content="text/html;charset=UTF-8" http-equiv="Content-Type" />
    <title>test1</title>
    <style type="text/css">
                    h1          { padding: 10px; padding-width: 100%; background-color: silver }
                    td, th      { width: 40%; border: 1px solid silver; padding: 10px }
                    td:first-child, th:first-child  { width: 20% } 
                    table       { width: 650px }
    </style>
  </head>
  <body>
    <h1>Sun Microsystems Inc.</h1>
    <p>The following host names are currently in use at <b>sun.com</b></p>
    <table>
        <tr>
          <th>Host name</th>
          <th>URL</th>
          <th>Used by</th>
        </tr>
        <tr>
          <td>www</td>
          <td><a href="http://www.sun.com">http://www.sun.com</a></td>
          <td>World Wide Web site</td>
        </tr>
        <tr>
          <td>java</td>
          <td><a href="http://java.sun.com">http://java.sun.com</a></td>
          <td>Java info</td>
        </tr>
    </table>
    
    <h1>The World Wide Web Consortium</h1>
    <p>The following host names are currently in use at <b>w3.org</b></p>
    <table>
      <tr>
        <th>Host name</th>
        <th>URL</th>
        <th>Used by</th>
      </tr>
      <tr>
        <td>www</td>
        <td><a href="http://www.w3.org">http://www.w3.org</a></td>
        <td>World Wide Web site</td>
      </tr>
      <tr>
        <td>validator</td>
        <td><a href="http://validator.w3.org">http://validator.w3.org</a></td>
        <td>web developers who want to get it right</td>
      </tr>
    </table>
  </body>
</html>

See also

  • STX is intended as a high-speed, low memory consumption alternative to XSLT.
  • Smarty is a template engine written in PHP.

External links

  • Implementations for Java:
    • Xalan-Java (http://xml.apache.org/xalan-j/)
    • SAXON (http://saxon.sourceforge.net/) by Michael Kay
    • XT (http://www.blnz.com/xt/index.html) originally by James Clark
    • Oracle XSLT, in the Oracle XDK (http://www.oracle.com/technology/tech/xml/xdkhome.html)
  • Implementations for C or C++:
    • Xalan-C++ (http://xml.apache.org/xalan-c/)
    • libxslt (http://xmlsoft.org/XSLT/) the XSLT C library for GNOME
    • Sablotron (http://www.gingerall.com/charlie/ga/xml/p_sab.xml), which is integrated into PHP
  • Implementations for Perl:
    • XML::LibXSLT (http://search.cpan.org/~msergeant/XML-LibXSLT-1.57/LibXSLT.pm) is a Perl interface to the libxslt C library
  • Implementations for Python:
    • 4XSLT, in the 4Suite (http://4suite.org/) toolkit by Fourthought, Inc.
    • lxml (http://codespeak.net/lxml/) by Martijn Faassen is a Pythonic wrapper of the libxslt C library
  • Implementations for JavaScript:
    • Google AJAXSLT (http://goog-ajaxslt.sourceforge.net/) AJAXSLT is an implementation of XSL-T in JavaScript, intended for use in fat web pages, which are nowadays referred to as AJAX applications. Because XSL-T uses XPath, it is also an implementation of XPath that can be used independently of XSL-T.
  • Implementations for specific operating systems:
    • Microsoft's MSXML (http://msdn.microsoft.com/XML/XMLDownloads/default.aspx) library may be used in various Microsoft Windows application development environments and languages, such as .Net, Visual Basic, C, and JScript.
  • Implementations integrated into web browsers:

de:XSL Transformation fr:Extended stylesheet language transformations nl:Extensible Stylesheet Language Transformations ja:XSLT sv:XSLT zh:XSLT ru:XSLT

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools