[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] When to use conditional constructions?


Subject: Re: [xsl] When to use conditional constructions?
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Sun, 30 Mar 2014 18:28:32 +0100

This raises the question as to how much of the logic to put in XSLT
instructions and how much in XPath expressions.

Dimitre, I think, uses a style of coding in which a great deal of logic goes
at the XPath level, leading to XPath expressions of 20 lines or more. Most
other XSLT users I've come across seem to try and avoid this. I suspect the
reasons are mainly to do with the difficulty of formatting it nicely, and poor
diagnostics if you get it wrong (the XSLT processor will typically only give
you the line number of the XSLT instruction). Personally I'm happy to go to
three or four lines for an XPath expression, but beyond that I start to feel
uncomfortable with it; can't really explain why.

So I'm comfortable using XPath conditionals in roughly the same contexts as I
would use the ternary conditional of C or Java: maximum of one line per
operand.

I do have a strong preferance for multiple template rules over xsl:choose. I
think this is mainly because it makes it easier to evolve the code as it
develops over time. Though a strong downside is that it can be very hard for
the reader of the code to work out which templates are going to fire under
which circumstances.

At the heart of this is that XSLT is a two-language system; it isn't fully
composable, in that XSLT constructs can't be called from XPath constructs.
This creates a strong incentive to break your code up into functions, which
can be called easily enough from either context.

Michael Kay
Saxonica




On 30 Mar 2014, at 13:25, David Rudel <fwqhgads@xxxxxxxxx> wrote:

> On Sun, Mar 30, 2014 at 1:07 AM, Dimitre Novatchev <dnovatchev@xxxxxxxxx>
wrote:
>
>> This is the full proof that XSLT conditional instructions can be
>> eliminated in any version of XSLT.
>>
>> BTW, I have quite a lot of experience writing complex transformations
>> without any XSLT conditional instructions. :)
>>
>> Cheers,
>> Dimitre Novatchev
>>
>
> Dimitre, as typical, your posts are very insightful. I'm now wondering
> how much I should consider avoiding conditionals in my own work.
>
> This provoked a general question I have for experiences XSLT
> programmers who frequently use <xsl:apply-templates> or some other
> method to avoid what would normally (in a more imperative regime) be
> done using conditional constructions.
>
> Have you found that, in general, it is best to avoid conditional
> constructs? If so, could you share what advantages you have found by
> doing so?
>
> Does this behavior extend to XPath's "if" statement as well, or do you
> see that as a different beast? (In other words, do you find that the
> advantages you obtain by avoiding XSL's conditionals do not apply as
> much when considering cases that can be addressed using XPath
> instead?)
>
> Are there specific cases where conditional constructs should be
> favored? (E.g., in <xsl:iterate> to allow Tail Call optimization?)
>
> As I have confessed in the past, for my work the thing I value most
> about XSLT is its support of XPath. I still tend to use imperative
> programming, so my scripts tend to look like the XSLT equivalent of a
> Java program that uses a single class.
>
> -David
>
> --
>
> "A false conclusion, once arrived at and widely accepted is not
> dislodged easily, and the less it is understood, the more tenaciously
> it is held." - Cantor's Law of Preservation of Ignorance.


Current Thread
Keywords