Re: [xsl] When to use conditional constructions?

Subject: Re: [xsl] When to use conditional constructions?
From: "Abel Braaksma (Exselt)" <abel@xxxxxxxxxx>
Date: Mon, 31 Mar 2014 19:33:42 +0200
On 31-3-2014 18:32, Graydon wrote:
> <xsl:template
> match="section[heading][body/table][body/para[following-sibling:table]][not(body/para[preceding-sibling:table])]">....</xsl:template>
>
> <xsl:template
> match="section[heading][body/table][body/para[following-sibling:table]][body/para[preceding-sibling:table]]">....</xsl:template>
>
> <xsl:template
> match="section[heading][body/table][not(body/para)]]">....</xsl:template>

Just a suggestion, but wouldn't this become more readable if you split
it using templates? Something like the following (not tested):

<xsl:template match="section[heading][body/table]">
    <!-- sections with tables -->
    <xsl:apply-templates
select="self::section[body/para[following-sibling::table]]"
mode="normal-section" />
    <xsl:apply-templates select="self::section[not(body/para)]"
mode="no-better-idea" />
</xsl:template>

<xsl:template match="section" mode="no-better-idea">
    ... do something ...
</xsl:template>

<xsl:template match="section[not(body/para[preceding-sibling::table])]"
mode="normal-section">
    <!-- stuff with paras and tables, but no table before para -->
</xsl:template>

<xsl:template match="section[body/para[preceding-sibling::table]]"
mode="normal-section">
    <!-- stuff with paras and tables, tables before paras -->
</xsl:template>

Not sure this effectively makes it easier for programmers or other
readers of your code, but it removes the redundant selections.

Alternatively, see my other mail for how to use function items to
achieve something similar, or to use functions, as others have already
shown. The (slight) advantage of function items is that it gives less
overhead in writing them, but conversely, when they become larger, they
tend to become less readable.

Cheers,

Abel Braaksma
Exselt XSLT 3.0 processor
http://exselt.net

Current Thread