Jump to content

XSLT

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SDaniel (talk | contribs) at 11:36, 24 August 2005 (XSL). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
XSL transformation processing
XSL transformation processing

XSL Transformations, or XSLT, is an XML-based language used, in conjunction with specialized processing software, for the transformation of XML documents. Although the process is referred to as "transformation," the original document is not changed; rather, a new XML document is created based on the content of an existing document. Then, the new document may be serialized (output) by the processor in standard XML syntax or in another format, such as HTML or plain text. XSLT is most often used to convert data between different XML schemas or to convert XML data into web pages or PDF documents.


XSLT evolved from the Extensible Stylesheet Language (XSL) development effort, led by James Clark in 19981999, which also produced XSL Formatting Objects (XSL-FO). The first XSLT specification was published as a Recommendation by the W3C in mid-1999, and work on a massively expanded second version, coordinated by Michael Kay, is ongoing.

Overview

The language is declarative — that is, an XSLT stylesheet consists of a collection of template rules, each of which 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 memory, network and 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"/>
    
    <!--XHTML document outline--> 
    <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>
    
    <!--Table headers and outline-->
    <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>
    
    <!--Table row and first two columns-->
    <xsl:template match="host">
        <!--Create variable for 'url', as it's used twice-->
        <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>

    <!--'Used by' column-->
    <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 (whitespace has been adjusted here for clarity):

<?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
          <td>World Wide Web site</td>
        </tr>
        <tr>
          <td>java</td>
          <td><a href=http://java.sun.com
          <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
        <td>World Wide Web site</td>
      </tr>
      <tr>
        <td>validator</td>
        <td><a href=http://validator.w3.org
        <td>web developers who want to get it right</td>
      </tr>
    </table>
  </body>
</html>

See also

  • Implementations for Java:
  • Implementations for C or C++:
  • Implementations for Perl:
  • Implementations for Python:
    • 4XSLT, in the 4Suite toolkit by Fourthought, Inc.
    • lxml by Martijn Faassen is a Pythonic wrapper of the libxslt C library
  • Implementations for JavaScript:
    • Google AJAXSLT 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 library may be used in various Microsoft Windows application development environments and languages, such as .Net, Visual Basic, C, and JScript.
    • Saxon.NET Project Weblog, an IKVM.NET-based port of Dr. Michael Kay's and Saxonica's Saxon Processor provides XSLT 2.0, XPath 2.0, and XQuery 1.0 support on the .NET platform.
  • Implementations integrated into web browsers: