XSL calculation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sebastian Gunreben

    XSL calculation

    Hi,

    I spent quite some weeks seaching google groups for a solution,
    but unfortunatly I didn't find anything matching my problem.
    Perhaps some of you has an idea ...

    My XML file looks as follows:
    <Table>
    <Row AttA="3" AttB="-2" AttC="2" />
    <Row AttA="2" AttB="2" AttC="2" />
    <Row AttA="-1" AttB="-2" AttC="6" AttC="3" />
    ...
    </Table>

    Each row has an arbitrary number of attributes. On this attributes a
    function F(AttA, ..., AttC) calculates a value. The values should be
    accumulated in the output. E.g. the function F is defined as:
    if( AttA == 3 )
    value = AttB * AttC;
    else
    value = AttB + AttC;

    The output should look like:
    <table>
    <tr><td>-4</td></tr>
    <tr><td>0</td></tr> <!-- -4 + 4 -->
    <tr><td>4</td></tr><!-- -4 + 4 + 4 -->
    </table>

    To summarize my problem:
    I can calculate each row by itself, dependent of the function F.
    But I have some difficulties accumulating the results. Does the
    solution depend
    on the function F? Have you got any ideas?

    Best regards

    sebastian gunreben
    sepp@autip.de
  • Joris Gillis

    #2
    Re: XSL calculation

    > My XML file looks as follows:[color=blue]
    > <Table>
    > <Row AttA="3" AttB="-2" AttC="2" />
    > <Row AttA="2" AttB="2" AttC="2" />
    > <Row AttA="-1" AttB="-2" AttC="6" AttC="3" />
    > ...
    > </Table>
    >
    > Each row has an arbitrary number of attributes. On this attributes a
    > function F(AttA, ..., AttC) calculates a value. The values should be
    > accumulated in the output. E.g. the function F is defined as:
    > if( AttA == 3 )
    > value = AttB * AttC;
    > else
    > value = AttB + AttC;
    >
    > The output should look like:
    > <table>
    > <tr><td>-4</td></tr>
    > <tr><td>0</td></tr> <!-- -4 + 4 -->
    > <tr><td>4</td></tr><!-- -4 + 4 + 4 -->
    > </table>
    >[/color]
    Hi,

    You could use something like this:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

    <xsl:template match="Row[1]">
    <table>
    <xsl:call-template name="F"/>
    </table>
    </xsl:template>

    <xsl:template name="F">
    <xsl:param name="Subtotal" select="0"/>

    <xsl:variable name="fv">
    <xsl:choose>
    <xsl:when test="@AttA = 3">
    <xsl:value-of select="@AttB * @AttC"/>
    </xsl:when>
    <xsl:otherwis e>
    <xsl:value-of select="@AttB + @AttC"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

    <tr><td><xsl:va lue-of select="$fv + $Subtotal"/></td></tr>

    <xsl:for-each select="followi ng-sibling::*[position()=1]">
    <xsl:call-template name="F">
    <xsl:with-param name="Subtotal" select="$Subtot al + $fv"/>
    </xsl:call-template>
    </xsl:for-each>

    </xsl:template>

    </xsl:stylesheet>

    [color=blue]
    > Does the solution depend on the function F?[/color]

    Not in my sample xslt: you can adapt the function F without problem by modifying the code inside the 'xsl:variable' tag

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
    Ceterum censeo XML omnibus esse utendum

    Comment

    • Sebastian Gunreben

      #3
      Re: XSL calculation

      "Joris Gillis" <roac@pandora.b e> wrote in message news:<opsgwq6cp fyf9v9r@news.pa ndora.be>...[color=blue]
      > [ A very good solution ][/color]

      Thank you very much, your solution solved my problem.

      Regards,
      sebastian

      Comment

      • Jeff Kish

        #4
        Re: XSL calculation

        On 3 Nov 2004 12:30:12 -0800, seb@gunis.de (Sebastian Gunreben) wrote:
        [color=blue]
        >Hi,
        >
        >I spent quite some weeks seaching google groups for a solution,
        >but unfortunatly I didn't find anything matching my problem.
        >Perhaps some of you has an idea ...
        >
        >My XML file looks as follows:
        ><Table>
        > <Row AttA="3" AttB="-2" AttC="2" />
        > <Row AttA="2" AttB="2" AttC="2" />
        > <Row AttA="-1" AttB="-2" AttC="6" AttC="3" />
        > ...
        ></Table>
        >
        >Each row has an arbitrary number of attributes. On this attributes a
        >function F(AttA, ..., AttC) calculates a value. The values should be
        >accumulated in the output. E.g. the function F is defined as:
        >if( AttA == 3 )
        > value = AttB * AttC;
        >else
        > value = AttB + AttC;
        >
        >The output should look like:
        ><table>
        > <tr><td>-4</td></tr>
        > <tr><td>0</td></tr> <!-- -4 + 4 -->
        > <tr><td>4</td></tr><!-- -4 + 4 + 4 -->
        ></table>
        >
        >To summarize my problem:
        >I can calculate each row by itself, dependent of the function F.
        >But I have some difficulties accumulating the results. Does the
        >solution depend
        >on the function F? Have you got any ideas?
        >
        >Best regards
        >
        >sebastian gunreben
        >sepp@autip.d e[/color]
        Hi.

        What situation are you in as far as environment and technology.. what are your
        restrictions? Can you use a command line solution, a java class solution or
        what?
        Jeff Kish

        Comment

        Working...