XSLT

From Wikipedia, the free encyclopedia
  (Redirected from Xslt)
Jump to: navigation, search
XSLT
Paradigm(s) Declarative
Developer W3C
Stable release 2.0
Major implementations libxslt, Saxon, Xalan
Influenced by DSSSL

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents,[1] or other objects such as HTML for web pages, plain text or into XSL Formatting Objects which can then be converted to PDF, PostScript and PNG.[2]

The original document is not changed; rather, a new document is created based on the content of an existing one.[3] Typically, input documents are XML files, but anything from which the processor can build an XQuery and XPath Data Model can be used, for example relational database tables, or geographical information systems.[1]

XSLT is a Turing-complete language, meaning it can perform any computation that can be performed by a computer.[4][5]

Contents

History [edit]

XSLT is influenced by functional languages,[6] and by text-based pattern matching languages like SNOBOL and awk. Its most direct predecessor is DSSSL, which did for SGML what XSLT does for XML.[7]

  • XSLT 1.0: XSLT was part of the World Wide Web Consortium (W3C)'s Extensible Stylesheet Language (XSL) development effort of 1998–1999, a project that also produced XSL-FO and XPath. Some members of the standards committee that developed XSLT, including James Clark, the editor, had previously worked on DSSSL. XSLT 1.0 was published as a W3C recommendation in November 1999.
  • XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001,[8] the XSL working group joined forces with the XQuery working group to create XPath 2.0,[9] with a richer data model and type system based on XML Schema. The most recent version is XSLT 2.0,[10] developed under the editorship of Michael Kay. It reached recommendation status in January 2007.[11] As of 2010, however, XSLT 1.0[12] is still widely used, since 2.0 is not supported natively in web browsers or for environments like LAMP.
  • XSLT 3.0: has W3C Working Draft status as of July 2012. The main new features are streaming transformations, and improvements to the modularity of large stylesheets.[13]

Concept [edit]

Overview [edit]

Diagram of the basic elements and process flow of Extensible Stylesheet Language Transformations.

The XSLT processor takes one or more XML source documents, plus one or more XSLT stylesheet modules, and processes them to produce 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 [edit]

The XSLT language is declarative:[14] 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:[15] 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 either to create nodes in the result tree, or to process more nodes in the source tree in the same way as the root node. Output derives from the result tree.

Processor implementations [edit]

  • AltovaXML: supports XSLT 1.0 and 2.0 and XQuery. Allows command line operations and interfaces to COM, Java, and .NET[16]
  • libxslt is a free library released under the MIT License that can be reused in commercial applications. It is based on libxml and implemented in C for speed and portability.[17]
  • MSXML and .NET. MSXML includes an XSLT 1.0 processor. From MSXML 4.0 it includes the command line utility msxsl.exe.[27]
  • Saxon: an XSLT and XQuery processor with open-source and proprietary versions for stand-alone operation and for Java, JavaScript[28] and .NET.
  • Xalan: an open source XSLT 1.0 processor from the Apache Software Foundation available stand-alone and for Java and C++.
  • Web browsers: Safari, Chrome, Firefox, Opera and Internet Explorer all support XSLT 1.0. None supports XSLT 2.0 natively, although the third party Saxon-CE product can provide this functionality.[29] Unlike some browsers, Chrome does not support XSLT when applied to local files for security considerations related to Gmail.[30]

Performance [edit]

Most early XSLT processors were interpreters. More recently, 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.[31] This separation is reflected in the design of XSLT processing APIs (such as JAXP).

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 optimization techniques found in functional programming languages and database query languages, such as static rewriting of an expression tree (e.g., to move calculations out of loops), and lazy pipelined evaluation to reduce the memory footprint of 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 significantly more efficient (in both space and time) than general-purpose DOM implementations.

XSLT and streaming [edit]

XSLT 1.0 and XSLT 2.0 were not designed to allow XML streaming of the input to output. The whole input document had to be read into memory before it could be processed.[32]

The XSLT 3.0 specification (currently - in 2013 - a working draft) is designed to allow XML streaming to reduce latency between the input and the output. This is especially important when processing documents too large to fit in memory, or when transformations are chained together in XML Pipelines, for example in XProc.

XSLT and XPath [edit]

XSLT uses XPath to identify subsets of the source document tree and perform calculations. XPath also provides a range of functions, which XSLT itself further augments.

XSLT 1.0 uses XPath 1.0. XSLT 2.0 uses XPath 2.0. Both specifications were published on the same date.

XSLT and XQuery compared [edit]

XSLT functionalities overlap with those of 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 [edit]

The <output> element can optionally take the attribute media-type, which allows one to set the media type (or MIME type) for the resulting output, for example: <xsl:output output="xml" media-type="application/xml"/>. The XSLT 1.0 recommendation recommends the more general attribute types text/xml and application/xml since for a long time there was no registered media type for XSLT. During this time text/xsl became the de facto standard. In XSLT 1.0 it was not specified how the media-type values should be used.

With the release of the XSLT 2.0, the W3C recommended the registration of the MIME media type application/xslt+xml[33] and it was later registered with the Internet Assigned Numbers Authority[34]

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[35] 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.[36]

XSLT examples [edit]

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 to XML) [edit]

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) [edit]

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.

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:[37]

<?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 [edit]

  • XProc - a W3C Standard pipeline language which can be used to combine XSLT transformations together with other operations like XQuery, XInclude or HTTP requests
  • 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
  • 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.
  • Context Framework - a Java-based web framework using XSLT as sole templating language.
  • Omnimark - another structured text processing language (and a proprietary software)
  • Xs3p - is an XSLT stylesheet that generates XHTML documentation from XML Schema Definition language (XSD) schema.

References [edit]

  1. ^ a b "Transformation". 2012-09-19. 
  2. ^ "XML Output Method". 2012-09-19. 
  3. ^ "Introduction". XSL Transformations (XSLT) Version 1.0 W3C Recommendation. W3C. 16 November 1999. Retrieved November 7, 2012. 
  4. ^ Kepser, Stephan. "A Simple Proof for the Turing-Completeness of XSLT and XQuery". Proceedings of Extreme Markup Languages. Retrieved 28 October 2012. 
  5. ^ "Universal Turing Machine in XSLT". Unidex. Retrieved 28 October 2012. 
  6. ^ Dimitre Novatchev. "Higher-Order Functional Programming with XSLT 2.0 and FXSL". ExtremeMarkupLanguages. Retrieved August 8, 2009. 
  7. ^ "A Proposal for XSL". W3C. Retrieved November 7, 2012. 
  8. ^ http://www.w3.org/TR/xslt11/
  9. ^ http://www.w3.org/TR/xpath20/
  10. ^ http://www.w3.org/TR/xslt20/
  11. ^ "XML and Semantic Web W3C Standards Timeline". 2012-02-04. 
  12. ^ http://www.w3.org/TR/xslt
  13. ^ "XSL Transformations (XSLT) Version 3.0". W3C. Retrieved 21 January 2013. 
  14. ^ "Discover the Wonders of XSLT: XSLT Quirks". "XSLT is a very specialized language with a distinct declarative flavor." 
  15. ^ "XSLT Definitions". "XSLT declarations define a set of rules and guidelines that are applied during processing according to a predefined algorithm." 
  16. ^ "AltovaXML - XSLT 1.0/2.0 Engine, XQuery Engine, XML Validator". Retrieved 23 October 2012. 
  17. ^ "The XSLT C library for GNOME: libxslt". Retrieved 23 November 2012. 
  18. ^ "The XSLT C library for GNOME: The xsltproc tool". Retrieved 23 November 2012. 
  19. ^ "xsltproc man page". Retrieved 23 November 2012. 
  20. ^ "New package: libxslt". Retrieved 23 November 2012. 
  21. ^ "The WebKit Open Source Project - XSLT". Retrieved 2009-10-25. 
  22. ^ "The XML C parser and toolkit of Gnome: Python and bindings". Retrieved 23 November 2012. 
  23. ^ "XML::LibXSLT - Interface to the GNOME libxslt library". CPAN. Retrieved 23 November 2012. 
  24. ^ "libxslt-ruby". Retrieved 23 November 2012. 
  25. ^ "libxml". Retrieved 23 November 2012. 
  26. ^ "libxml++". sourceforge.net. Retrieved 23 November 2012. 
  27. ^ "Command Line Transformation Utility (msxsl.exe)". Microsoft. Retrieved 22 October 2012. 
  28. ^ "Saxon Client Edition 1.0". Saxonica. Retrieved 14 August 2012. 
  29. ^ Saxonica. "About Saxon-CE". Retrieved 2012-06-16. 
  30. ^ "Security in Depth: Local Web Pages". Retrieved 2012-08-05. 
  31. ^ Saxon: Anatomy of an XSLT processor - Article describing implementation & optimization details of a popular XSLT processor.
  32. ^ Kay, Michael. "A Streaming XSLT Processor". Balisage: The Markup Conference 2010 Proceedings. Retrieved 15 February 2012. 
  33. ^ "XSL Transformations (XSLT) Version 2.0". W3C. Retrieved 19 October 2012. 
  34. ^ "Application Media Types". IANA. Retrieved 19 October 2012. 
  35. ^ "XSLT Requirements for Viewing XML in a Browser". Microsoft. Retrieved 19 October 2012. 
  36. ^ Kay, Michael (2008). XSLT 2.0 and XPath 2.0 Programmer's Reference. Wiley. p. 100. ISBN 978-0-470-19274-0. 
  37. ^ "XSL Transformations (XSLT) Version 1.0: W3C Recommendation – Embedding Stylesheets". W3C. 16 November 1999. Retrieved 2009-01-06. 

Further reading [edit]

External links [edit]

Documentation
Tutorials
Mailing lists
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