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

Re: [xsl] "if-condition-return something" idiom : is it possible in XSLT ?


Subject: Re: [xsl] "if-condition-return something" idiom : is it possible in XSLT ?
From: "Kerry, Richard richard.kerry@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 16 Jun 2014 12:59:48 -0000

>Have you considered using xsl:choose?

Yes - ish.
The conditions are functions and may not be independent (they aren't), and
there are more calculations done between each one than I've shown here.  I
tend to view (*) xsl:choose as similar to C's "switch", which needs to do a
single calculation to work out which branch to run.  As I am working through a
relatively complex conversion and testing process I'm not sure I can make it
work.  I will have another look.

Appreciatively,
Richard.


(*)  I may well be wrong in this.....

________________________________________
From: Imsieke, Gerrit, le-tex gerrit.imsieke@xxxxxxxxx
[xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx]
Sent: 16 June 2014 13:21
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] "if-condition-return something" idiom : is it possible in
XSLT ?

Have you considered using xsl:choose?

On 16.06.2014 14:17, Kerry, Richard richard.kerry@xxxxxxxx wrote:
>
> In C/C++ programming the following idiom is quite common:
>
> if (condition1)
>    return value1;
>
> if (condition2)
>    return value2;
>
> if (condition3)
>    return value3;
>
> return value4;
>
> In other words each of these will test a condition and if it is satisfied
execution will stop and some value will be returned to the caller.  The run
stops with the first condition that succeeds.
>
> I've tried to do something similar in XSLT and am getting somewhat
contradictory indications of whether or not it is working.  Please can someone
advise whether this should work, or if there is a similar form that I can
use.
>
> What I have is like the following template, to be called by name.
>
> <xsl:template name="test.1">
>    <xsl:param name="string" select="''"/>
>
>     <xsl:if test="$check.1 = 0">
>       <xsl:value-of select="$value.1" />
>     </xsl:if>
>
>     <xsl:if test="$check.2 = 0">
>       <xsl:value-of select="$value.2" />
>     </xsl:if>
>
>     <xsl:if test="$check.3 = 0">
>       <xsl:value-of select="$value.3" />
>     </xsl:if>
>
>    <xsl:value-of select="$value.4" />
> </xsl:template>
>
> My theory of why it might work is based on knowing that the following will
work:
>
> <xsl:template name="test.2">
>    <xsl:param name="string" select="''"/>
>
>    <xsl:value-of select="$value.4" />
> </xsl:template>
>
> I say "will work" in the sense that if the above template is called (by
name) it will return the value from value.4.  What I'm not sure of is whether
that should work if the "returning" value-of is a level down, in an xsl:if, as
in the other template (test.1).
> It is only a theory of why it might work.  Practice may indicate otherwise,
and somewhere in the spec might be a reason.
>
> Can someone please advise whether I can do this, or how to get broadly
equivalent behaviour ?
>
> Regards,
> Richard.
>
>
>
> Richard Kerry
> BNCS Engineer, SI SOL Telco & Media Vertical Practice
> T: +44 (0)20 3618 2669
> M: +44 (0)7812 325518
> G300, Stadium House, Wood Lane, London, W12 7TA
> richard.kerry@xxxxxxxx
>
>
> This e-mail and the documents attached are confidential and intended solely
for the addressee; it may also be privileged. If you receive this e-mail in
error, please notify the sender immediately and destroy it. As its integrity
cannot be secured on the Internet, the Atos group liability cannot be
triggered for the message content. Although the sender endeavours to maintain
a computer virus-free network, the sender does not warrant that this
transmission is virus-free and will not be liable for any damages resulting
from any virus transmitted.
>
>

--
Gerrit Imsieke
Geschdftsf|hrer / Managing Director
le-tex publishing services GmbH
Weissenfelser Str. 84, 04229 Leipzig, Germany
Phone +49 341 355356 110, Fax +49 341 355356 510
gerrit.imsieke@xxxxxxxxx, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschdftsf|hrer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vvckler


Current Thread
Keywords