XSLT
Purpose¶
Saxon-HE processor with stylesheet caching.
The open-source implementation of XSLT 3.0, XPath 2.0 and 3.1, and XQuery 3.1. This provides the “basic” conformance level of these languages; it also supports some optional features of the specifications such as serialization and support for XQuery modules.
Methods¶
Binding name: p6.xslt
process¶
Process the given xml
with the supplied xslt
and applying the supplied parameters
, storing the compiled xslt
in the cache with the supplied id
.
Syntax
String p6.xslt.process(String id, String xslt, String xml, Map<String, Object> parameters = null)
Results of the transformation are returned as a String.
Parameter: parameters
Allowed parameter types are:
- Boolean
- Integer
- Long
- Short
- Character
- Byte
- String
- Double
- Float
- BigDecimal
- BigInteger
- URI
Internationalisation
Since 6.9.9
Translation can be easily handled using the $i18n
function.
In order to use this function you need to define the p6
namespace.
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p6="http://platform6.io"
exclude-result-prefixes="p6"
>
The locale
, if not provided ‘EN’ will be used as the default.
Examples
Translate using the default locale
<xsl:template match="/path/to/text()"><xsl:value-of select="p6:i18n(.)"/></xsl:template>
Translate using a specific locale
<xsl:template match="/path/to/text()"><xsl:value-of select="p6:i18n(., 'fr')"/></xsl:template>
Translate using a specific locale and prefix
<xsl:template match="/path/to/text()"><xsl:value-of select="p6:i18n('statuses', ., 'fr')"/></xsl:template>
<xsl:template match="/path/to/text()"><xsl:value-of select="p6:i18n(concat('statuses.', .), 'fr')"/></xsl:template>
Note
Subsequent calls to process()
will search for and use the cached instance of the compiled XSLT giving much greater performance.
The cache will expire entries after 10 minutes of inactivity.
Warning
The parameter id
is used to cache the compiled xslt
.
To improve performance and avoid compiling multiple times the same xslt
please reuse the id
when possible.
The cache is global to the instance and not split by app key.
Examples
Without params
def results = p6.xslt.process("myTransform", p6.resource.get('STYLESHEET'), p6.resource.get('XMLIN'))
println results
Resource: STYLESHEET
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/hello-world">
<HTML>
<HEAD><TITLE/></HEAD>
<BODY>
<H1><xsl:value-of select="greeting"/></H1>
<xsl:apply-templates select="greeter"/>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="greeter">
<DIV>from <I><xsl:value-of select="."/></I></DIV>
</xsl:template>
</xsl:stylesheet>
Resource: XMLIN
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<hello-world>
<greeter>An XSLT Programmer</greeter>
<greeting>Hello, World!</greeting>
</hello-world>
With params
def params = [
"PARAM_INTEGER": 42,
"PARAM_STRING": "Amalto",
"PARAM_BOOLEAN": true
]
def results = p6.xslt.process("myTransform", p6.resource.get('STYLESHEET'), p6.resource.get('XMLIN'))
println results
Resource: STYLESHEET
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="PARAM_INTEGER"/>
<xsl:param name="PARAM_STRING"/>
<xsl:param name="PARAM_BOOLEAN"/>
<xsl:template match="/hello-world">
<HTML>
<HEAD><TITLE/></HEAD>
<BODY>
<H1><xsl:value-of select="greeting"/></H1>
<xsl:apply-templates select="greeter"/>
<ul>
<li>Int: <xsl:value-of select="$PARAM_INTEGER"/></li>
<li>String: <xsl:value-of select="$PARAM_STRING"/></li>
<li>Boolean: <xsl:value-of select="$PARAM_BOOLEAN"/></li>
</ul>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="greeter">
<DIV>from <I><xsl:value-of select="."/></I></DIV>
</xsl:template>
</xsl:stylesheet>
Resource: STYLESHEET
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<hello-world>
<greeter>An XSLT Programmer</greeter>
<greeting>Hello, World!</greeting>
</hello-world>