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

Subject: Re: [xsl] What is the Core of XSLT?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sat, 29 Mar 2014 18:07:42 -0700
On Sat, Mar 29, 2014 at 5:35 PM, Abel Braaksma (Exselt) <abel@xxxxxxxxxx> wrote:
> 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 glad that you will be happy:  :)

Do you see any <xsl:choose> or <xsl:if> in the code below?

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="text"/>

 <xsl:param name="left"  select="3"/>
 <xsl:param name="right"  select="2"/>
 <xsl:param name="operator" select="'*'" />
 <xsl:template match="/">
   <xsl:call-template name="calculator2"/>
 </xsl:template>

 <xsl:template name="calculator2">
     <xsl:apply-templates select="self::node()[$operator = '*']" mode="mult"/>
     <xsl:apply-templates select="self::node()[$operator = '+']" mode="add"/>
     <xsl:apply-templates select=
     "self::node()[not($operator = '+') and not($operator = '*') ]"
mode="error"/>
 </xsl:template>

 <xsl:template match="/" mode="add">
   <xsl:value-of select="$left + $right"/>
 </xsl:template>

 <xsl:template match="/" mode="mult">
   <xsl:value-of select="$left * $right"/>
 </xsl:template>

 <xsl:template match="/" mode="error">Oops! Wrong operator!</xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document, the wanted,
correct result is produced:

6



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread