Introduction to XSLT - Part 2
Jean-François Perrot
- Precedence, import, include
- Priority of rules
- Implicit and explicit priority
- xsl:import
- xsl:include
- Use for DocBook processing
- xsl:variable, xsl:param, calling
templates
- Variables
- Parameters
- Named templates
- Example : a simple
loop
-
-
When several rules
are applicable to the same node, only one will be
applied.
In principle, the rule with higher priority will be chosen, and it
is
an error if more than one are found.
However, it is common practice to choose the last rule (having
the highest priority) in the stylesheet (rather than signalling an
error).
Some people consider that relying on this implementation-dependent
feature is wrong, others would promote it to a standard technique...
-
- Explicit with
the integer-valued
priority
attribute :
<xsl:template match="xyz" priority="2">
...
has higher priority than
<xsl:template match="xyz" priority="1">
...
- Implicit
priority (default priority) follows
counter-intuitive rules. It is safer not to rely on them.
The only valuable information is that default rules have lower
priority than written templates...
-
<xsl:import
href = "path-to-/myStylesheet.xsl" />
must be the first child of <xsl:stylesheet>
(the
root element)
- imports the set
of rules contained in
myStylesheet.xsl
into the stylesheet;
- these rules
have a lower priority than the
indigenous rules.
- If an imported
rule and an indigenous rule are both
applicable, the indigenous one is chosen, the imported one is overridden.
- However, it is
still possible to activate an imported
rule thanks to
<xsl:apply-imports/>
.
A simple example (source)
- XML
document
- Base
stylesheet, result
- Imported
stylesheet (extract from the base), importing
stylesheet, result
- importing stylesheet with reactivation, result
-
<xsl:include
href = "path-to-/myStylesheet.xsl" />
can appear anywhere at the top-level of the stylesheet (i.e. as a
direct child of the root).
- Contrary to the
import
case, included rules
have the same priority as indigenous ones.
- If an included
rule cr and an indigenous
rule dr are both applicable:
- in
principle, that is an error;
- in
practice, the last found one will be chosen.
- If the
inclusion is located before the
indigenous rule dr, then cr will be overriden,
with the same logic as in the the import
case. See here for an example.
- If the inclusion is located after the
indigenous rule dr, then cr will be activated,
and dr will be neutralized. See here for an example.
-
Combining
Stylesheets with Include and Import
-
(source)
-
... in XSLT are not
really variables, as their values cannot be changed.
They resemble constants from conventional programming languages.
Declaration :
<xsl:variable name="myVar" select="'myValue'" />
Usage : <xsl:value-of
select="$myVar"/>
The only way in which a variable can be changed is by declaring it
inside a for-each
loop, in which case its value will be updated for
every iteration.
Top-level variables (variables that are direct child nodes of the <xsl:stylesheet>
element) can never be changed.
-
are assigned a value
either from the process that invoked the
stylesheet (top-level parameter),
or from a <xsl:with-param>
or from a default value
(in which case it behaves as if it was a variable).
-
behave very much like
functions in conventional programming languages.
Recursive calls are allowed.
The <xsl:with-param>
construct implements
parmeter-passing.
-
Styleheet
Loop.xsl
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version='1.0'>
<xsl:output method="text" indent="yes" />
<xsl:variable name="dash" select="'---'" />
<xsl:template match="/">
<xsl:variable name="Max" select="value" />
<xsl:call-template name="Base">
<xsl:with-param name="x"
select="0"/>
<xsl:with-param name="y"
select="$Max"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="Base">
<xsl:param name="x"/>
<xsl:param name="y"/>
<xsl:if test="$x <= $y">
<xsl:value-of
select="concat($x,
$dash)"/><xsl:text> </xsl:text>
<xsl:call-template
name="Base">
<xsl:with-param name="x" select="$x+1"/>
<xsl:with-param name="y" select="$y"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Demo
jfp$ more value.xml
<?xml version='1.0'?>
<value>5</value>
jfp$ xsltproc Loop.xsl value.xml
0---
1---
2---
3---
4---
5---
jfp$