XSLT

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search
XSLT
File:XSLT.svg
Filename extension .xsl, .xslt
Internet media type application/xslt+xml[1]
Developed by World Wide Web Consortium
Type of format Stylesheet language
Extended from XML
Standard(s) 1.0 (Recommendation),
2.0 (Recommendation)

Template:Infobox W3C Standard XSLT (XSL Transformations) is a declarative, XML-based language used for the transformation of XML documents into other XML documents. The original document is not changed; rather, a new document is created based on the content of an existing one.[2] The new document may be serialized (output) by the processor in standard XML syntax or in another format, such as HTML or plain text.[3] XSLT is often used to convert XML data into HTML or XHTML documents for display as a web page: the transformation may happen dynamically either on the client or on the server, or it may be done as part of the publishing process. It is also used to create output for printing or direct video display, typically by transforming the original XML into XSL Formatting Objects to create formatted output which can then be converted to a variety of formats, a few of which are PDF, PostScript, AWT and PNG. XSLT is also used to translate XML messages between different XML schemas, or to make changes to documents within the scope of a single schema, for example by removing the parts of a message that are not needed.

Origins

XSLT is developed by the World Wide Web Consortium (W3C). The most recent version is XSLT 2.0[4], which reached W3C recommendation status on 23 January 2007. As of 2008, however, XSLT 1.0[5] is still more widely used and implemented.

Originally, XSLT was part of the W3C's Extensible Stylesheet Language (XSL) development effort of 1998–1999, a project that also produced XSL Formatting Objects and the XML Path Language, XPath. The editor of the first version was James Clark. XSLT 1.0 was published as a Recommendation by the W3C on 16 November 1999. After an abortive attempt to create a version 1.1 in 2001[6], the XSL working group joined forces with the XQuery working group to create XPath 2.0[7], with a richer data model and type system based on XML Schema. XSLT 2.0, developed under the editorship of Michael Kay, was built on this foundation in 2002–2006.

As a language, XSLT is influenced by functional languages,[8] and by text-based pattern matching languages like SNOBOL and awk. Its most direct predecessor was ISO DSSSL, a language that performed the same function for full SGML that XSLT performs for XML[9]. Many of the standards committee that developed XSLT had previously worked on DSSSL, including James Clark. XSLT can also be considered Turing-complete[10][11][12][13] template processor.

Most of this article is applicable to both XSLT versions; any differences are noted in the text.

Overview

File:XSLT en.svg
Diagram of the basic elements and process flow of Extensible Stylesheet Language Transformations.

The XSLT processing model involves:

  • one or more XML source documents;
  • one or more XSLT stylesheet modules;
  • the XSLT template processing engine (the processor); and
  • one or more result documents.

The XSLT processor ordinarily takes two input documents[14]—an XML source document, and an XSLT stylesheet—and produces an output document. The XSLT stylesheet contains a collection of template rules: instructions and other directives that guide the processor in the production of the output document.

Template rule processing

The XSLT language is declarative—rather than listing an imperative sequence of actions to perform in a stateful environment, template rules only define how to handle a node matching a particular XPath-like pattern, if the processor should happen to encounter one, and the contents of the templates effectively comprise functional expressions that directly represent their evaluated form: the result tree, which is the basis of the processor's output.

The processor follows a fixed algorithm: Assuming a stylesheet has already been read and prepared, the processor builds a source tree from the input XML document. It then starts by processing the source tree's root node, finding in the stylesheet the best-matching template for that node, and evaluating the template's contents. Instructions in each template generally direct the processor to either create nodes in the result tree, or process more nodes in the source tree in the same way as the root node. Output is derived from the result tree.

XSLT uses the halfway-to-even ("banker's") method when rounding fractional numbers to integers.

Processor implementations

Template:Main article XSLT processor implementations fall into two main categories: server-side, and client-side.

Although client-side XSLT processing has been available in Microsoft's Internet Explorer 6.x since 2001 (W3C draft in Internet Explorer 5.x since 1999, or even earlier, but in a form that was incompatible with the final W3C specifications) and in Netscape 7.x since 2002 (partially in Netscape 6.x since 2000)[15], adoption has been slower because of the widespread deployment of older and alternative browsers without XSLT support. For similar reasons, adoption of XSLT 2.0 in such environments remains limited. See the Comparison of layout engines (XML)#XSL technologies.

XSLT processors may be delivered as standalone products, or as components of other software including web browsers, application servers, frameworks such as Java and .NET, or even operating systems. For example, Windows XP comes with the MSXML3 library, which includes an XSLT 1.0 processor. Earlier versions may be upgraded and there are many alternatives. See the external links section. Another example is GNOME desktop environment 2.x which includes libxslt, an open source and complete implementation of the XSLT 1.0 specification.[16][17] The libxslt library is also used in other software, such as WebKit layout engine.[18] One of the open source XSLT 1.0 processors is also The Apache Xalan Project.[19]

Performance

The performance of XSLT processors has steadily improved as the technology has become more mature, although the very first processor, James Clark's xt, was unbeaten for several years.[20]

Most of the earlier XSLT processors were interpreters; in more recent products, code generation is increasingly common, using portable intermediate languages such as Java bytecode or .NET Common Intermediate Language as the target. However, even the interpretive products generally offer separate analysis and execution phases, allowing an optimized expression tree to be created in memory and reused to perform multiple transformations: this gives substantial performance benefits in online publishing applications where the same transformation is applied many times per second to different source documents.[21] This separation is reflected in the design of XSLT processing APIs such as JAXP (Java API for XML Processing).

Early XSLT processors had very few optimizations; stylesheet documents were read into Document Object Models and the processor would act on them directly. XPath engines were also not optimized. Increasingly, however, XSLT processors use the kind of optimization techniques found in functional programming languages and database query languages, notably static rewriting of the expression tree for example to move calculations out of loops, and lazy pipelined evaluation to reduce the use of memory for intermediate results and allow "early exit" when the processor can evaluate an expression such as following-sibling::*[1] without a complete evaluation of all subexpressions. Many processors also use tree representations that are much more efficient (in both space and time) than general purpose DOM implementations.

XSLT and XPath

XSLT relies upon the W3C's XPath language for identifying subsets of the source document tree, as well as for performing calculations. XPath also provides a range of functions, which XSLT itself further augments. This reliance upon XPath adds a great deal of power and flexibility to XSLT.

XSLT 1.0 uses XPath 1.0. Similarly, XSLT 2.0 relies on XPath 2.0; both specifications were published on the same date.

XSLT and XQuery compared

XSLT capabilities overlap with XQuery, which was initially conceived as a query language for large collections of XML documents.

The XSLT 2.0 and XQuery 1.0 standards were developed by separate working groups within W3C, working together to ensure a common approach where appropriate. They share the same data model, type system, and function library, and both include XPath 2.0 as a sublanguage.

The two languages, however, are rooted in different traditions and serve the needs of different communities. XSLT was primarily conceived as a stylesheet language whose primary goal was to render XML for the human reader on screen, on the web (as web template language), or on paper. XQuery was primarily conceived as a database query language in the tradition of SQL.

Because the two languages originate in different communities, XSLT is stronger in its handling of narrative documents with more flexible structure, while XQuery is stronger in its data handling, for example when performing relational joins.[citation needed]

XSLT media types

As of 2009, there is no MIME/Internet media type registered for XSLT.

The XSLT 1.0 Recommendation (1999) says "The MIME media types text/xml and application/xml should be used for XSLT stylesheets. It is possible that a media type will be registered specifically for XSLT stylesheets; if and when it is, that media type may also be used." It goes on to use text/xml in an example of how to embed a stylesheet with the xml-stylesheet processing instruction.

RFC 3023 points out potential technical problems with text/* types in general, and proposes application/xslt+xml as an ideal media type for XSLT. The XSLT 2.0 Recommendation (January 2007) includes a formal application to register this media type. However, at the time of writing (January 2009) the process of registration has not yet been completed, and RFC 3023 warns that "... this media type should not be used until such registration has been completed."

Pre-1.0 working drafts of XSLT used text/xsl in their embedding examples, and this type was implemented and continues to be promoted by Microsoft in Internet Explorer and MSXML. It is also widely recognized in the xml-stylesheet processing instruction by other browsers. In practice, therefore, users wanting to control transformation in the browser using this processing instruction are obliged to use this unregistered media type.[22]

XSLT examples

Sample of incoming 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 (transforming XML)

This XSLT stylesheet provides templates to transform 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>

Its evaluation results in a new XML document, having another structure:

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

Example 2 (transforming XML to XHTML)

Processing the following example XSLT file

<?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>

with the XML input file shown above results in the following XHTML (whitespace has been adjusted here for clarity):

<?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 generates the output below when rendered in a web browser.

File:Xslt ex2.png
Rendered XHTML generated from an XML input file and an XSLT transformation.

In order for a web browser to be able automatically to apply an XSL transformation to an XML document on display, an XML stylesheet processing instruction can be inserted into XML. So, for example, if the stylesheet in Example 2 above were available as "example2.xsl", the following instruction could be added to the original incoming XML:[23]

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

(In this example, text/xsl is technically incorrect according to the W3C specifications, but it is the only media type that is widely supported across current (2009) browsers.)

See also

  • XSLT elements - a list of some commonly used XSLT structures
  • XML transformation language - any computer language designed specifically to transform an input XML document into an output XML document that satisfies some specific goal
  • Extensible Stylesheet Language - a family of languages of which XSLT is a member
  • XQuery and XSLT compared
  • XML template engine - including a list of XSLT processor implementations
  • XSL formatting objects or XSL-FO - An XML-based language for documents, usually generated by transforming source documents with XSLT, consisting of objects used to create formatted output
  • Identity transform - a starting point for filter chains that add or remove data elements from XML trees in a transformation pipeline
  • Apache Cocoon - a Java-based framework for processing data with XSLT and other transformers.
  • Omnimark - another structured text processing language (and a proprietary software)

References

  1. XSL Transformations (XSLT) Version 2.0
  2. XSL Transformations (XSLT)
  3. See e. g., http://www.w3.org/TR/xslt#output, specifying alternate output methods.
  4. http://www.w3.org/TR/xslt20/
  5. http://www.w3.org/TR/xslt/
  6. http://www.w3.org/TR/xslt11/
  7. http://www.w3.org/TR/xpath20/
  8. Dimitre Novatchev. "Higher-Order Functional Programming with XSLT 2.0 and FXSL". ExtremeMarkupLanguages. http://conferences.idealliance.org/extreme/html/2006/Novatchev01/EML2006Novatchev01.html. Retrieved August 8 2009. 
  9. http://www.w3.org/TR/NOTE-XSL.html
  10. Kepser, Stephan. (2004).
  11. A Simple Proof for the Turing-Completeness of XSLT and XQuery. International Digital Enterprise Alliance.
  12. Universal Turing Machine in XSLT
  13. www.refal.net/~korlukov/tm/
  14. Typically, documents are XML files, but the specifications avoid excluding other representations, such as in-memory DOM trees or other conforming input streams.
  15. Mozilla Developer Center (2007-01-21). "XSLT in Gecko / Browser Differences". https://developer.mozilla.org/en/XSLT_in_Gecko/Browser_Differences. Retrieved 2009-10-25. 
  16. The GNOME Project (2002-06-26). "GNOME 2.0 Release Notes - XML". http://library.gnome.org/misc/release-notes/2.0/#libxml. Retrieved 2009-10-25. 
  17. "The XSLT C library for GNOME - libxslt". http://xmlsoft.org/XSLT/. Retrieved 2009-10-25. 
  18. "The WebKit Open Source Project - XSLT". http://webkit.org/projects/xslt/index.html. Retrieved 2009-10-25. 
  19. "The Apache Xalan Project". http://xalan.apache.org/. Retrieved 2009-10-25. 
  20. XML.com: XSLT Processor Benchmarks
  21. Saxon: Anatomy of an XSLT processor - An article describing the implementation and optimization details of a popular Java-based XSLT processor.
  22. Kay, Michael (2008). XSLT 2.0 and XPath 2.0 Programmer's Reference. Wiley. p. 100. ISBN 978-0-470-19274-0. 
  23. "XSL Transformations (XSLT) Version 1.0: W3C Recommendation - Embedding Stylesheets". W3C. 16 November 1999. http://www.w3.org/TR/xslt#section-Embedding-Stylesheets. Retrieved 2009-01-06. 

External links

For implementations, see XML template engine.
Documentation
Mailing lists
Books
  • XSLT by Doug Tidwell, published by O’Reilly (ISBN 0-596-00053-7)
  • XSLT Cookbook by Sal Mangano, published by O’Reilly (ISBN 0-596-00974-7)
  • XSLT 2.0 Programmer's Reference by Michael Kay (ISBN 0-764-56909-0)
  • XSLT 2.0 and XPath 2.0 Programmer's Reference by Michael Kay (ISBN 978-0-470-19274-0)
  • XSLT 2.0 Web Development by Dmitry Kirsanov (ISBN 0-13-140635-3)
  • XSL Companion, 2nd Edition by Neil Bradley, published by Addison-Wesley (ISBN 0-201-77083-0)
  • XSLT and XPath on the Edge (Unlimited Edition) by Jeni Tennison, published by Hungry Minds Inc, U.S. (ISBN 0-7645-4776-3)
  • XSLT & XPath, A Guide to XML Transformations by John Robert Gardner and Zarella Rendon, published by Prentice-Hall (ISBN 0-13-040446-2)
  • XSL-FO by Dave Pawson, published by O'Reilly (ISBN 978-0-596-00355-5)
XSLT code libraries
  • EXSLT is a widespread community initiative to provide extensions to XSLT.
  • FXSL is a library implementing support for Higher-order functions in XSLT. FXSL is written in XSLT itself.
  • The XSLT Standard Library xsltsl, provides the XSLT developer with a set of XSLT templates for commonly used functions. These are implemented purely in XSLT, that is they do not use any extensions. xsltsl is a SourceForge project.
  • Kernow A GUI for Saxon that provides a point and click interface for running transforms.
  • xslt.js – Transform XML with XSLT JavaScript library that transforms XML with XSLT in the browser.
Further examples

ca:Extensible Stylesheet Language Transformations cs:XSLT da:XSLT de:XSL Transformation es:Extensible Stylesheet Language Transformations eu:XSLT fa:تبدیلات اکس‌اس‌ال fr:Extensible Stylesheet Language Transformations ko:XSLT id:Extensible Stylesheet Language Transformations is:XSLT it:XSL Transformations lt:XSLT hu:Extensible Stylesheet Language Transformations ms:XSLT nl:Extensible Stylesheet Language Transformations ja:XSL Transformations no:XSLT pl:XSL Transformations pt:XSLT ru:XSLT fi:XSLT sv:XSLT vi:XSLT tr:XSLT uk:XSL Transformations bat-smg:XSLT zh:XSLT