Re: [xsl] R: [xsl] IQuestion about XSLT and Math Operations

Subject: Re: [xsl] R: [xsl] IQuestion about XSLT and Math Operations
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 04 Dec 2005 20:48:02 -0500
At 2005-12-05 00:23 +0100, Villani Alessandro wrote:
Hi, thank you for your answer, but i think i explain my problem in a bad
way.

I'm sorry I misunderstood ...


I say exactly r11/c1, i want to use in my next math operations the value
that i'll found in that child (r11/c1) of xsl file.
I hope i explain my problem in a right way.

You did originally, I didn't look at your data closely enough.


Excuse me for my poor english ;)

No apology necessary ... reviewing your question it was I who jumped at the meaning. I'm the one who's sorry.


At 2005-12-04 13:48 +0100, you wrote:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
     <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
     <xsl:template match="parameters">
          <xsl:param name="r50">
                <xsl:value-of select="r3*r2"/>

The above isn't really necessary since you are using it only once.


          </xsl:param>
          <elab>
                <tab0>
                     <r10>
                          <c0>null</c0>
                          <c1>
                               <xsl:value-of select="r2"/>
                          </c1>
                          <c2><xsl:value-of select="$r50"/></c2>

You can just do:


<c2><xsl:value-of select="r3 * r2"/></c2>

But I talk more to this below.

                     </r10>
                     <r11>
                          <c0>
                               <xsl:value-of select="r2"/>
                          </c0>
                          <c1>
                               <xsl:value-of select="r2 - r6"/>
                          </c1>
                          <c2>null</c2>
                     </r11>
                     <r12>
                          <c0>null</c0>
                          <c1>
                               <xsl:value-of select="r2 - r6"/>
                          </c1>
                          <c2>
                          How I can perform, if possible , this type of
operation: [value of r11/c1 + value of r12/c1] whithout use xsl:param.
</c2>
                     </r12>

As Kamal said, just recalculate ... in a functional language such as XSLT it is very common to revisit and recalculate. Your requirement is, unambiguously:


<c2><xsl:value-of select="2 * ( r2 - r6 )"/></c2>

BTW, where did you get "r6" from? I only see three input parameters.

Remember that <xsl:param> is only for parameters passed to the template. And, since you do not have a template name on this template, I as a reader anticipate that somewhere you need the flexibility to have an <xsl:apply-templates> with an <xsl:with-param> child in order to override your default value for $r50. I'm guessing that you want to use <xsl:variable> to bind the result of a calculation. I find in my stylesheets I will use <xsl:variable> to bind the results of expressions for use in multiple places for the purposes of maintenance, not performance.

For example, consider the following ... I've maintained your $r50 because you might be using it elsewhere in the template that you don't reveal.

     <xsl:template match="parameters">
          <xsl:variable name="r50">
                <xsl:value-of select="r3*r2"/>
          </xsl:variable>
          <xsl:variable name="diff" select="r2 - r6"/>
          <elab>
                <tab0>
                     <r10>
                          <c0>null</c0>
                          <c1>
                               <xsl:value-of select="r2"/>
                          </c1>
                          <c2><xsl:value-of select="$r50"/></c2>
                     </r10>
                     <r11>
                          <c0>
                               <xsl:value-of select="r2"/>
                          </c0>
                          <c1>
                               <xsl:value-of select="$diff"/>
                          </c1>
                          <c2>null</c2>
                     </r11>
                     <r12>
                          <c0>null</c0>
                          <c1>
                               <xsl:value-of select="$diff"/>
                          </c1>
                          <c2>
                               <xsl:value-of select="2 * $diff"/>
                          </c2>
                     </r12>

Then ... if you change the expression of the difference, you merely change it once in the variable binding rather than having to think of the maintenance of changing the difference expression everywhere where you may have used it. The execution time savings probably isn't measurable in human terms, but the maintenance time saving and robustness probably is measurable in time saved in the future when having to change the code and keep things consistent.

My XSLT students come to class fearing variables because they don't vary, yet they learn that variables can be very useful constructs in writing maintainable stylesheets.

I hope this helps more than my last attempt.

. . . . . . . . . Ken

--
Upcoming XSLT/XSL-FO hands-on courses:  Denver,CO March 13-17,2006
World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Aug'05  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread