XSL transformation

from Wikipedia, the free encyclopedia
XSL transformations
XSLT example
File extension : .xsl, .xslt
MIME type : application / xslt + xml
Developed by: World Wide Web Consortium
Type: Style sheet language
Extended by: XML
Standard (s) : 1.1 (Recommendation) ,
2.0 (Recommendation)
3.0 (Recommendation)


XSLT , short XSLT is a programming language for the transformation of XML -documents. It is part of the Extensible Stylesheet Language (XSL) and is a Turing-complete language.

XSLT was published as a recommendation by the World Wide Web Consortium (W3C) on October 8, 1999. XSLT is based on the logical tree structure of an XML document and is used to define conversion rules. XSLT programs, so-called XSLT stylesheets , are themselves structured according to the rules of the XML standard.

The stylesheets are read in by special software, the XSLT processors , which use these instructions to convert one or more XML documents into the desired output format. XSLT processors are also integrated in many modern web browsers , such as Opera (version 9 or higher ), Firefox and Internet Explorer version 5 (version 6 or higher with full XSLT 1.0 support).

XSLT is a subset of XSL , along with XSL-FO and XPath .

history

Originally, a DSSSL- like language in XML syntax was to be created with XSL . It quickly became clear, however, that such a language actually consists of three interacting, but independently usable parts:

  • A language for describing a document as a tree with formatting instructions and style information: XSL Formatting Objects ( XSL-FO )
  • A language for transforming any XML document into another tree, e.g. B. one in XSL-FO : XSLT
  • A language for addressing parts of trees: XPath

The declarative , functional- applicative language XSLT is a. emerged from DSSSL . XSLT was originally developed by James Clark (XSLT 1.0, XSLT 1.1), Michael Kay is responsible for the current further development . Since January 23, 2007, XSLT 2.0 has been a "Recommendation" of the W3C and has replaced the version XSLT 1.0, which has been valid since 1999. In the meantime, there is also a version XSLT 3.0 as a candidate recommendation from November 2015 by the W3C, which is already supported by the Saxon parser.

functionality

Simplified representation of the functionality of XSLT - An XML document is converted into a new document (.xml, .xhtml, .html, .txt, ...) using an XSLT document and a processing processor.

The XSLT language describes the conversion of an XML derivative (also called XML dialect or XML application), in the form of an XML document , into another document, called a transformation. The resulting document mostly conforms to XML syntax, but other text files and even binary files can also be created.

For this purpose, the XML documents are viewed as a logical tree : The source trees of the documents to be transformed and the target trees of the documents to be generated that result from the transformation.

A transformation consists of a number of individual transformation rules called templates . A template has an XPath- based pattern that describes which nodes it applies to, and a content that determines how the template creates its part of the target tree.

An XSLT document contains at least the following languages ​​and XML-based technologies:

  • XML as the basis for XSLT
  • XSLT itself (XML-based)
  • XPath / XSLT patterns
  • The language (s) of the source document (XML-based, e.g. DocBook )
  • The language (s) of the target document (often XML-based, e.g. XML , XSLT itself, but also HTML or RELAX NG , text format also possible)
  • XML namespaces to differentiate between the individual languages ​​used

As well as from XSLT 2.0:

Template Rules and Conflict Resolution

Template rules are always used when a certain condition is met. The following example includes the content of all titles ("title" tags) in "em" tags, regardless of where they appear in the document, and leaves the rest untouched.

<xsl:template match="//title">
  <em>
    <xsl:apply-templates/>
  </em>
</xsl:template>

<xsl:apply-templates/> transforms the child elements of the current element using all applicable rules.

Conflict Resolution is required when a node matches several template rules at the same time. In such cases, the following rules apply.

  • Imported rules have lower priority.
  • If a priority attribute is given, it is taken into account.
  • More specific patterns have higher priority than less specific ones. For example, the test of whether any node exists is less specific than a test for a node with a specific name.
  • If there are several rules with equal rights, this is an error and is output as a message by the XSLT parser.

Sorting and conditional output

To sort items, a tag can be combined for-eachwith one sort. The loop then does not run in the order of the nodes of the original document, but in alphabetical or numerical order. In the following example, all book elements are sorted in ascending order according to their price.

<xsl:for-each select="buch">
  <xsl:sort select="preis" order="ascending" />
</xsl:for-each>

With or without order="ascending"the order is always the same (ascending), since this is the default setting. With order="descending"you get a descending order.

XSLT offers both binary decisions using the tag ifand multiple decisions using the tag choose. The following example outputs an asterisk if and only if the attribute of nationalitythe element has authorthe value "US".

<xsl:if test="author/@nationality='U.S.'">*</xsl:if>

The combination of the tags chooseand whencan be used to make several distinctions between cases.

<xsl:choose>
  <xsl:when test="...">...</xsl:when>
  <xsl:when test="...">...</xsl:when>
  <xsl:when test="...">...</xsl:when>
  <xsl:otherwise>...</xsl:otherwise>
</xsl:choose>

Output text unchanged

The tag <xsl:text>can be used to allow XSLT to output any text , whereby escaping (the output of “<” and “&” as “& lt;” and “& amp;”) can be switched off. The content can be in normal text or in a CDATA section. The advantage of the CDATA section is that its content is not interpreted, so it can also contain XML syntax.

For example generated

<xsl:text disable-output-escaping="yes">
  <![CDATA[ ### Hier darf < & >>< beliebiger Text stehen ### ]]>
</xsl:text>

the edition:

### Hier darf < & >>< beliebiger Text stehen ###

Applications

XML languages ​​and their scope

Two important areas of application for XSLT are:

  • POP (Presentation Oriented Publishing) describes the transformation for the purpose of presentation. Using different stylesheets, the data can be converted into XHTML , Formatting Objects (XSL-FO) , SVG , SMIL , DocBook and many other formats. The target document does not necessarily have to be an XML document. The original semantic markup is replaced by a style-related markup.
  • MOM ( Message Oriented Middleware ) describes the transformation for the purpose of data exchange. Since XML only forms a language concept for developing languages, it is not enough for two systems (e.g. programs) to master XML in order to be able to exchange data with one another. You must use the same XML-based language. In the frequent case in which the systems do not use the same XML-based language, XSLT is often used to develop translators from one language into the other with the help of transformations.

Examples of POP are:

  • Statistical data are available as XML (e.g. from the database) and are processed as
  • For a website, the XHTML files or XML files are initially written without a layout and are then given the layout with the help of a transformation. The advantage is similar to that of a classic CMS : In order to change the layout, it is not necessary to change all XHTML pages; instead you change the stylesheet and transform it again. For example, the XML publishing system Apache Cocoon uses this method. Since modern web browsers support XSLT, it is possible to publish the source XML and have it transformed at the last moment with XSLT on the client computer, provided that all expected users have such a browser (e.g. in company networks ). Otherwise the source XML should already be transformed on the server and the result published.

Alternatives to XSLT

DSSSL

A possible alternative to XSLT is DSSSL , which can also be viewed as a forerunner of XSLT. The biggest advantage of XSLT over DSSSL is its XML-based syntax. This means that every XML editor can automatically also be used for XSLT, and the syntax rules of XML (well-formedness, validity) also apply to XSLT. This makes it possible for XSLT to process itself, so you can develop transformations to create and edit transformations.

MetaMorphosis

MetaMorphosis is also inspired by DSSSL. In contrast to XSLT, it is a "target-driven" processor. The target tree to be created is constructed. The rules do not describe how the input file should be processed, but rather how the output file should be constructed. The process begins with a virtual rule "! Begin" with which the transformation process begins. The transformation rules describe how the nodes should be filled. In doing so, queries are formulated in the source trees as well as in the target trees that have been set up until then. The query language is a set-oriented expression that can read out and also set all properties of the nodes. Generated nodes can subsequently be manipulated as required.

Other features of MetaMorphosis support industrial use: flexible memory management for very large documents, creation of auxiliary data structures (getindex, putindex) for quick access, plug-in architecture, API for C ++ , C # , Java , the possibility of implementing dedicated frontends and backends (in C ++ as well as in the MetaMorphosis scripting language).

Template Attribute Language

As part of the application server Zope was Template Attribute Language (TAL) developed; TAL templates are (depending on the output format) [X] HTML or XML documents, the content of which can be dynamically replaced by attributes and thus combine the prototype and the template used. The <xsl:for-each>attribute roughly corresponds to the element tal:repeat; the TAL Expression Syntax is used as the expression syntax . If no HTML or XML element is suitable as a carrier for the attribute, <tal:beliebigerBezeichner>elements can also be used.

Programming languages

A fundamental alternative to XSLT is the possibility of writing such transformations in any programming language (e.g. Java, C ++ or Perl ). However, if certain requirements are met, XSLT can provide a certain guarantee for well-formed and possibly even valid target documents.

It is also possible to couple both: The actual transformation then takes place via XSLT, but self-defined functions for string manipulation are called within the XSLT stylesheet. The program (e.g. written in Ruby or Python ) then consists of these self-defined functions and the call of an XSLT function that receives the stylesheet and its own functions as parameters.

Differentiation from CSS

CSS is not an alternative to XSLT because CSS is a formatting language . CSS merely describes how the components of a tree (acoustically or visually) are to be formatted, while XSLT can radically change the tree or, as a result, provide documents that are not based on XML. With XSLT you can e.g. B. automatically create tables of contents, indexes, link lists and complex calculations, but not with CSS. With XSLT it is also possible to generate results that use CSS as the formatting language.

Code example

The following XSLT creates a simple table of contents for an XHTML page with headings:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:html="http://www.w3.org/1999/xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="html"
>

    <xsl:output
        method="xml"
        doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
        doctype-public="-//W3C//DTD XHTML 1.1//EN"
    />

    <xsl:template match="html:body">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <h1><xsl:apply-templates select="//html:title//text()" /></h1>
            <h2 id="t-o-c">Inhaltsverzeichnis</h2>
            <ul>
                <li><a href="#t-o-c">Inhaltsverzeichnis</a></li>
                <xsl:for-each select=".//html:h2">
                    <li>
                        <xsl:if test="not(@id)">
                            <xsl:message>Achtung: Kann ohne Id keinen Link erzeugen</xsl:message>
                        </xsl:if>
                        <a href="#{@id}"><xsl:apply-templates/></a>
                    </li>
                </xsl:for-each>
            </ul>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

See also: XML , XSL-FO , XPath

XSLT processors

Most common web browsers (including Mozilla Firefox , Internet Explorer , Google Chrome , Opera ) now support XSLT. The following is a list of other known XSLT-capable processors and libraries.

  • Saxon (Java and .NET, non-validating variant, free with restrictions, supports XSLT 3.0, XPath 3.0 and XQuery 1.1)
  • TransforMiiX (C ++, free)
  • Xalan -Java (Java, free)
  • Xalan -C ++ (C ++, free)
  • libxslt / xsltproc (C, free)
  • XT (Java, originally by James Clark, free)
  • XSLTengine (Java, multiplatform, not free, many extensions)
  • Sablotron (C ++, multiplatform, free)
  • Microsoft XML Core Services (Windows, not free)
  • Unicorn XSLT Processor (Windows, not free, useful additional tools)
  • WebSphere Application Server Feature Pack for XML (Extension of WebSphere , not free, supports XSLT 2.0, XPath 2.0 and XQuery 1.0)

literature

Web links

Wikibooks: XSLT  - learning and teaching materials

Individual evidence

  1. w3.org
  2. Universal Turing Machine in XSLT. Unidex, Inc. (English).
  3. XSL Transformations (XSLT) Version 1.0, W3C Proposed Recommendation, in: archive.org. October 8, 1999, archived from the original on June 16, 2006 ; accessed on February 12, 2017 (English).
  4. XSLT 3.0
  5. Saxon 9.8 product comparison (Feature Matrix)