Re: [xsl] best practices for using XSLT modes

Subject: Re: [xsl] best practices for using XSLT modes
From: "Eliot Kimber ekimber@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 5 Dec 2019 14:50:58 -0000
Not really about modes, but I would replace the choice that acts on different
@val values with templates applied to the @val attribute, i.e.:

        <xsl:template match="a">
          <val><xsl:apply-templates select="@val"/></val>

     <xsl:template match="@val[. ge 0]">
        <xsl:value-of select="@val || ': positive'"/>

     <xsl:template match="@val[. lt  0]">
        <xsl:value-of select="@val || ': negative"/>

Note that I handle the bug in the original in that it would produce no result
when @val is "0" (zero).

The use of templates rather than xsl:choose makes the code cleaner, I think,
puts the focus at the template level on the @val attribute, which is the focus
of the business logic, and enables extension and override. For example, if you
want a value of exactly zero to have a different result, you could do that by
adding an override template. The original use of xsl:choose would require
overriding the entire template for the <a> element.

You could make this use modes by putting the @val templates in a specific mode
rather than in the default mode as I have it, which might be appropriate if
this handling of @val was neither universal nor limited to just the <a>
element (if it was just the <a> element you could just add that to the match


Eliot Kimber

o;?On 12/4/19, 11:54 PM, "Mukul Gandhi gandhi.mukul@xxxxxxxxx"
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

    Hi Mike,

    On Wed, Dec 4, 2019 at 4:14 PM Michael Kay mike@xxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

    ([mukul] I assume, you mean 'mode' here) helps to modularise the logic,
and in particular, for someone reading the code, if apply-templates uses a
specific mode then it reduces the effort needed to find the template rules
that might get invoked -- which is a significant part of the effort of
debugging stylesheets.

    In fact, this was the reason why I wrote an example mentioning modes.
Thanks for the explanation.

    then you wonder whether a conditional (e.g. xsl:choose) might not be more

    In fact, I had a third example in my mind as well where I wanted to use
xsl:choose to produce a similar result. Its mentioned below (this is schema
aware, and I use the Saxon option -val:strict),

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="";

        <xsl:output method="xml" indent="yes"/>

                 <xs:element name="root">
                             <xs:element name="a" maxOccurs="unbounded">
                                     <xs:attribute name="val"

        <xsl:template match="root">
              <xsl:apply-templates select="a"/>

        <xsl:template match="a">
            <xsl:when test="@val gt 0">
              <val><xsl:value-of select="@val"/>: positive</val>
            <xsl:when test="@val lt 0">
              <val><xsl:value-of select="@val"/>: negative</val>


    The above stylesheet, when given the same input XML, produces output,

       <val>-1: negative</val>
       <val>-4: negative</val>
       <val>5: positive</val>
       <val>3: positive</val>
       <val>2: positive</val>

    (The order of "val" elements in this output, is different than what were
produced by my first two stylesheets. This could be fixed, if required)

    An interesting recent experience: we have a test driver for running XSLT
tests, that's written in XSLT. The test assertions might take the form:

             <assert>/res = 'Success'</assert>


                <assert>/res = 'Success'</assert>
                <error code="XTSE0020"/>

    Now, firstly, we use a mode for evaluating assertions, that's separate
from the mode used for processing other things in the test catalog. A good
reason for that is that all the template rules in that mode return a boolean
(indicating test success of failure), and the apply-templates call has to know
that a boolean result is expected. The rules in a mode have to have equivalent
pre- and post- conditions in terms of things like the expected parameters and
the required return type, because they are interchangeable as far as the
caller is concerned.

    Secondly, I found it useful recently to split the mode for evaluating
assertions into two: one mode handles the case where the test produces a
normal result, the other handles the case where it produces a dynamic error,
and we issue a different apply-templates instruction for the two cases. The
reason for this is that the two modes have different default behaviour (the
mode that handles error outcomes returns "false" for all assertions except the
one with match="error"): this change eliminated a lot of logic. Perhaps the
same effect could have been achieved using a generic template rule delegating
to specific template rules using next-match; but in this scenario where we're
basically handling a two-dimensional decision tree (X - what is the outcome of
a test?, Y - what is the assertion being tested?) then using modes for one of
the dimensions and match patterns for the other can be useful.

    Thanks, for sharing these experiences. Its enlightening to know.

    if you're writing complex stylesheets, modes are a powerful tool at your
disposal and can be used with care to great effect.

    I agree.

    Mukul Gandhi

    XSL-List info and archive
    (by email <>)

Current Thread