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

Re: [xsl] What is the Core of XSLT?


Subject: Re: [xsl] What is the Core of XSLT?
From: "Abel Braaksma (Exselt)" <abel@xxxxxxxxxx>
Date: Sun, 30 Mar 2014 01:35:49 +0100

It looks like I didn't make myself very clear, sorry about that. I meant
processing atomic values, instead of processing nodes. You asked for an
example, here it goes ;)

Suppose you have this great calculator, that can add or multiply to values:

<xsl:param name="left" />
<xsl:param name="right" />
<xsl:param name="operator" />

<xsl:template name="calculator">
    <xsl:choose>
        <xsl:when test="$operator = '*'">
            <xsl:value-of select="$left * $right" />
        </xsl:when>
        <xsl:when test="$operator = '+'">
            <xsl:value-of select="$left + $right" />
        </xsl:when>
        <xsl:otherwise>Oops! Wrong operator!</xsl:otherwise>
     </xsl:choose>
</xsl:template>

I don't think (but would be happy to be proven wrong) that this can be
implemented in XSLT 1.0 without xsl:choose or xsl:if. However, in XSLT
2.0 you can do this (other solutions possible):

<xsl:template name="calculator">
    <xsl:variable name="operator-node">
        <operator><xsl:value-of select="$operator" /></operator>
    </xsl:variable>
    <xsl:apply-templates select="$operator-node/operator" />
</xsl:template>

<xsl:template match="operator[text() = '*']">
    <xsl:value-of select="$left * $right" />
</xsl:template>

<xsl:template match="operator[text() = '+']">
    <xsl:value-of select="$left + $right" />
</xsl:template>

<xsl:template match="operator">
    <xsl:text>Oops! Wrong operator!</xsl:text>
</xsl:template>


I am not 100% sure if every string or other atomic operation can be
represented by using a temporary result tree fragment, but if that is
possible (and it probably is), we can add the exslt:node-set function to
the required XSLT 1.0 instructions and indeed, we can than get rid of
xsl:if and xsl:choose.

Cheers,

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


On 30-3-2014 1:07, Dimitre Novatchev wrote:
> On Sat, Mar 29, 2014 at 4:13 PM, Abel Braaksma (Exselt) <abel@xxxxxxxxxx> wrote:
>> I don't think you can do everything
>> with XSLT 1.0 that you can do now with a subset that does not include
>> xsl:if (xsl:choose can be implemented in xsl:ifs).
> If I understand this correctly, it says that <xsl:choose> cannot be
> implemented if there is no <xsl:if> instruction available.
>
> But it can.
>
> An <xsl:choose> with N outcomes can be expressed in XSLT 2 as a single:
>
>   <xsl:apply-templates select="my:special-node"/>
>
> and N templates of the form:
>
>  <xsl:template match="my:special-node[condition]">
>
>    <!-- The wanted code here -->
>  <xsl:template>
>
>
> In XSLT 1.0 we sometimes can only specify the condition within a
> predicate in the "select" attribute of the <xsl:apply-templates>
> instruction.
>
> Thus, an <xsl:choose> with N outcomes can be implemented using N
> <xsl:apply-templates> instructions of the form:
>
>    <xsl:apply-templates select="my:special-node[conditionK]"/>
>
> and N templates -- exactly as in the case for XSLT 2.0  above.
>
> 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


Current Thread
Keywords