Jump to content

XSLT

From Niidae Wiki

Template:Short description Template:Infobox programming language Template:Infobox file format XSLT (Extensible Stylesheet Language Transformations) is a language originally designed for transforming XML documents into other XML documents,<ref name="Transformation">Template:Cite web</ref> or other formats such as HTML for web pages, plain text, or XSL Formatting Objects. These formats can be subsequently converted to formats such as PDF, PostScript, and PNG.<ref>Template:Cite web</ref> Support for JSON and plain-text transformation was added in later updates to the XSLT 1.0 specification.

XSLT 3.0 implementations support Java, .NET, C/C++, Python, PHP and NodeJS. An XSLT 3.0 JavaScript library can also be hosted within the web browser. Modern web browsers also include native support for XSLT 1.0.<ref name="Used for">Template:Cite web</ref>

The XSLT document transformation specifies how to transform an XML document into new document (usually XML, but other formats, such as plain text are supported).<ref>Template:Cite web</ref> Typically, input documents are XML files, but anything from which the processor can build an XQuery and XPath Data Model can be used, such as relational database tables or geographical information systems.<ref name="Transformation"/>

While XSLT was originally designed as a special-purpose language for XML transformation, the language is Turing-complete, making it theoretically capable of arbitrary computations.<ref>XSLT Version 2.0 Is Turing-Complete: A Purely Transformation Based Proof</ref>

History

[edit]

XSLT is influenced by functional languages,<ref>Template:Cite web</ref> 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.<ref>Template:Cite web</ref>

  • XSLT 2.0: after an abortive attempt to create a version 1.1 in 2001,<ref>Template:Cite web</ref> the XSL working group joined forces with the XQuery working group to create XPath 2.0,<ref>Template:Cite web</ref> with a richer data model and type system based on XML Schema. Building on this is XSLT 2.0,<ref>Template:Cite web</ref> developed under the editorship of Michael Kay, which reached recommendation status in January 2007.<ref>Template:Cite web</ref> The most important innovations in XSLT 2.0 include:
    • String manipulation using regular expressions
    • Functions and operators for manipulating dates, times, and durations
    • Multiple output documents
    • Grouping (creating hierarchic structure from flat input sequences)
    • A richer type system and stronger type checking
  • XSLT 3.0: became a W3C Recommendation on 8 June 2017. The main new features are:<ref name=new-in-3>Template:Cite web</ref>
    • Streaming transformations: in previous versions the entire input document had to be read into memory before it could be processed,<ref>

Template:Cite web</ref> and output could not be written until processing had finished. XSLT 3.0 allows XML streaming which is useful for processing documents too large to fit in memory or when transformations are chained in XML Pipelines.

    • Packages, to improve the modularity of large stylesheets.
    • Improved handling of dynamic errors with, for example, an xsl:try instruction.
    • Support for maps and arrays, enabling XSLT to handle JSON as well as XML.
    • Functions can now be arguments to other (higher-order) functions.

Design and processing model

[edit]
File:XSLT en.svg
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 stylesheets, and processes them to produce one or multiple output documents.<ref>Template:Citation</ref><ref>Template:Cite web</ref> In contrast to widely implemented imperative programming languages like C, XSLT is declarative.<ref>Template:Cite web</ref> The basic processing paradigm is pattern matching.<ref name=IBM>Template:Cite web</ref> 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.

A typical processor behaves as follows. First, assuming a stylesheet has already been read and prepared, the processor builds a source tree from the input XML document. It then processes the source tree's root node, finds the best-matching template for that node in the stylesheet, and evaluates the template's contents. Instructions in each template generally direct the processor to either create nodes in the result tree, or to process more nodes in the source tree in the same way as the root node. Finally the result tree is serialized as XML or HTML text.

XPath

[edit]

Template:Details 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, while XSLT 2.0 uses XPath 2.0. XSLT 3.0 will work with either XPath 3.0 or 3.1. In the case of 1.0 and 2.0, the XSLT and XPath specifications were published on the same date. With 3.0, however, they were no longer synchronized; XPath 3.0 became a Recommendation in April 2014, followed by XPath 3.1 in February 2017; XSLT 3.0 followed in June 2017.

XQuery compared

[edit]

Template:Details 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 a 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.<ref>Template:Cite web</ref>

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 in 2007 the registration of the MIME media type application/xslt+xml<ref>Template:Cite web</ref> and it was later registered with the Internet Assigned Numbers Authority.<ref>Template:Cite web</ref>

Pre-1.0 working drafts of XSLT used text/xsl in their embedding examples, and this type was implemented and continued to be promoted by Microsoft in Internet Explorer<ref>Template:Cite web</ref> and MSXML circa 2012. 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 were obliged to use this unregistered media type.<ref>Template:Cite book</ref>

Examples

[edit]

These examples use the following incoming XML document:<syntaxhighlight lang="xml"> <?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> </syntaxhighlight>

Example 1 (transforming XML to XML)

[edit]

This XSLT stylesheet provides templates to transform the XML document:<syntaxhighlight lang="xml"> <?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> </syntaxhighlight>

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

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

 <name username="JS1">John</name>
 <name username="MI1">Morka</name>

</root> </syntaxhighlight>

Example 2 (transforming XML to XHTML)

[edit]

Processing the following example XSLT file <syntaxhighlight lang="xml"> <?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>

Persons

    <xsl:apply-templates select="person"> <xsl:sort select="family-name" /> </xsl:apply-templates>
     </body>
   </html>
 </xsl:template>
 <xsl:template match="person">
  • <xsl:value-of select="family-name"/><xsl:text>, </xsl:text><xsl:value-of select="name"/>
  • </xsl:template> </xsl:stylesheet> </syntaxhighlight> with the XML input file shown above results in the following XHTML (whitespace has been adjusted here for clarity): <syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Testing XML Example</title> </head> <body>

    Persons

    • Ismincius, Morka
    • Smith, John
     </body>
    

    </html> </syntaxhighlight> 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 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:<ref name=w3cxslt>Template:Cite web</ref>

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

    In this example, text/xsl is technically incorrect according to the W3C specifications<ref name=w3cxslt /> (which say the type should be application/xslt+xml), but it is the only media type that is widely supported across browsers as of 2009, and the situation is unchanged in 2021.

    Processor implementations

    [edit]

    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.<ref>Saxon: Anatomy of an XSLT processor – Article describing implementation & optimization details of a popular XSLT processor.</ref> 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)<ref>Template:Cite journal</ref> than general-purpose DOM implementations.

    In June 2014, Debbie Lockett and Michael Kay introduced an open-source benchmarking framework for XSLT processors called XT-Speedo.<ref>Template:Cite journal</ref>

    See also

    [edit]

    References

    [edit]

    Template:Reflist

    Further reading

    [edit]
    • XSLT by Doug Tidwell, published by O’Reilly (Template:ISBN)
    • XSLT Cookbook by Sal Mangano, published by O’Reilly (Template:ISBN)
    • XSLT 2.0 Programmer's Reference by Michael Kay (Template:ISBN)
    • XSLT 2.0 and XPath 2.0 Programmer's Reference by Michael Kay (Template:ISBN)
    • XSLT 2.0 Web Development by Dmitry Kirsanov (Template:ISBN)
    • XSL Companion, 2nd Edition by Neil Bradley, published by Addison-Wesley (Template:ISBN)
    • XSLT and XPath on the Edge (Unlimited Edition) by Jeni Tennison, published by Hungry Minds Inc, U.S. (Template:ISBN)
    • XSLT & XPath, A Guide to XML Transformations by John Robert Gardner and Zarella Rendon, published by Prentice-Hall (Template:ISBN)
    • XSL-FO by Dave Pawson, published by O'Reilly (Template:ISBN)
    [edit]

    Template:Wikibooks Template:Commons category

    Documentation
    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.

    Template:XSL Template:W3C Standards

    Template:Authority control