XSLT

From miki
Revision as of 17:51, 13 December 2016 by Mip (talk | contribs) (→‎Miscellaneous)
Jump to navigation Jump to search

XSLT stands for Extensible Stylesheet Language Transformations.

Links

Gnome library
exslt (extensions)
Extensions supported in libxml (xsltproc processor)

Overview

Currently there are two versions of the XSLT standards: XSLT 1.0 and XSLT 2.0.

XSLT 1.0
On linux, the standard XSLT 1.0 processor is xsltproc (from package xsltproc, libxml2 library).
In XSLT 1.0, most functions come from XSLT extensions (like http://exslt.org/).


XSLT 2.0
  • On linux, the standard XSLT 2.0 processor is saxonb-xslt (from package libsaxonb-java).
  • XSLT 2.0 introduces XQuery, XPath 2.0, and XSLT functions (with prefix fn:). See here for a detailed list of features.

Extensions

From w3.org [1]:

  • The element extension mechanism allows namespaces to be designated as extension namespaces. When a namespace is designated as an extension namespace and an element with a name from that namespace occurs in a template, then the element is treated as an instruction rather than as a literal result element.

Extensions available in the XSLT processor

Using xsltproc (package xsltproc [2], we can get the list of built-in extensions with:

xsltproc --dumpextensions 
# Registered XSLT Extensions
# --------------------------
# Registered Extension Functions:
# {http://exslt.org/math}atan2
# {http://exslt.org/strings}align
# ...
exslt.org/crypto
  • "Hidden" extensions that provides basic crypto functions (apparently from patch [3], introduced in July 5 2004 [4])
  • See also [5]
  • CAUTION — These extensions are not available if using a custom implementation of Python like Anaconda
xsltproc --dumpextensions|grep crypto
# {http://exslt.org/crypto}rc4_decrypt
# {http://exslt.org/crypto}md4
# {http://exslt.org/crypto}sha1
# {http://exslt.org/crypto}md5
# {http://exslt.org/crypto}rc4_encrypt
Example of use:
<xsl:template match="rootnode">
<xsl:value-of select="crypto:md5('ahahah')"/>
</xsl:template>

Quick reference

xsl:attribute

Input Output
<link site="www.stackoverflow.com"/>
<a href="http://www.stackoverflow.com">Click here</a>

Use

<xsl:template match="link">
    <a>
        <xsl:attribute name="href">
            <xsl:text>http://</xsl:text><xsl:value-of select="@site"/>
        </xsl:attribute>
        <xsl:text>Link</xsl:text>
    </a>
</xsl:template>

Or shorter using curly braces {...} [6]:

<xsl:template match="link">
    <a href="http://{@site}">Click here</a>
</xsl:template>

Also for:

Tips

Output tags like <?fileVersion 4.0.0 ?>

Use a xsl:text tag with attribute disable-output-escaping="yes": [7]:

<xsl:template match="/">
<xsl:text disable-output-escaping="yes">&lt;?fileVersion 4.0.0?&gt;</xsl:text>
</xsl:template>

This will produce:

<?fileVersion 4.0.0?>

Generate a random UID

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:data="http://exslt.org/dates-and-times"
    xmlns:math="http://exslt.org/math"
    extension-element-prefixes="date math"
    version='1.0'>
<!-- .... -->
<xsl:variable name="noncels" select="floor(math:random()*800)+100"/>
<xsl:variable name="noncems" select="data:seconds(date:date-time())-date:seconds('1970-01-01T00:00:00')"/>
<!-- .... -->
<xsl:value-of select="concat($noncems,noncels+0)"/>
</xsl:stylesheet>

Examples

TBC

Miscellaneous

  • <xsl:param name="MYPARAM"> in <xsl:stylesheet> node declares a global parameter.
After this declaration we can use $MYPARAM to get the value of the parameter. If it is not declared, we can only use $MYPARAM if it is passed as a parameter (e.g. via command line xsltproc -param MYPARAM "'myparamvalu'"), otherwise an error is triggered.