Loading ...
Sorry, an error occurred while loading the content.
 

Variable scope query!

Expand Messages
  • Nitin Jain
    Hi! I will appritiate if anybody could spare some time and reply to this query. The XML structure looks like: CA
    Message 1 of 3 , Feb 8, 2002
      Hi!

      I will appritiate if anybody could spare some time and
      reply to this query.

      The XML structure looks like:
      <List>
      <reports>
      <Currency>CA</Currency>
      <ChequeDate>05-16-2001</ChequeDate>
      <DepositAmt>12.0</DepositAmt>
      </reports>
      <reports>
      <Currency>CA</Currency>
      <ChequeDate>05-16-2001</ChequeDate>
      <DepositAmt>14.0</DepositAmt>
      </reports>
      <reports>
      <Currency>CA</Currency>
      <ChequeDate>05-20-2001</ChequeDate>
      <DepositAmt>18.0</DepositAmt>
      </reports>
      <reports>
      <Currency>CA</Currency>
      <ChequeDate>05-20-2001</ChequeDate>
      <DepositAmt>16.0</DepositAmt>
      </reports>
      <reports>
      <Currency>US</Currency>
      <ChequeDate>05-16-2001</ChequeDate>
      <DepositAmt>12.0</DepositAmt>
      </reports>
      <reports>
      <Currency>US</Currency>
      <ChequeDate>05-16-2001</ChequeDate>
      <DepositAmt>14.0</DepositAmt>
      </reports>
      </List>

      And the requirement is to generate the report like
      this:

      Currency: CA
      Date Amount
      05-16-2001 12.0
      05-16-2001 14.0
      SubTotal 26.0

      Date Amount
      05-20-2001 18.0
      05-20-2001 16.0
      SubTotal 34.0
      Total 60.0

      Currency: US
      Date Amount
      05-16-2001 12.0
      05-16-2001 14.0
      SubTotal 26.0
      Total 26.0

      The XML data is presorted.( wrt currency type and date
      )

      So for this I have a subTotal variable which I
      declared before the
      template. And when I loop within reports, after
      printing the first subTotal
      correctly in the second (and subsequent) subTotals it
      prints incorrect
      values.
      And the reason is that I try to reset the variable to
      zero when a new date
      entry is found. But to my surprise the variable is not
      resseted.

      Please suggest what is a workaround.

      Thanks and Regards,
      Nitin Jain


      __________________________________________________
      Do You Yahoo!?
      Send FREE Valentine eCards with Yahoo! Greetings!
      http://greetings.yahoo.com
    • G. Ken Holman
      ... Fine. ... You are approaching the problem incorrectly. Variables in XSLT do not vary ... they are fixed once they are bound. Your philosophy when
      Message 2 of 3 , Feb 8, 2002
        At 2002-02-08 09:58 -0800, Nitin Jain wrote:
        >The XML structure looks like:
        >...
        >And the requirement is to generate the report like
        >this:
        >...
        >The XML data is presorted.( wrt currency type and date
        >)

        Fine.

        >So for this I have a subTotal variable which I
        >declared before the
        >template. And when I loop within reports, after
        >printing the first subTotal
        >correctly in the second (and subsequent) subTotals it
        >prints incorrect
        >values.
        >And the reason is that I try to reset the variable to
        >zero when a new date
        >entry is found. But to my surprise the variable is not
        >resseted.

        You are approaching the problem incorrectly.

        Variables in XSLT do not vary ... they are fixed once they are bound.

        Your philosophy when approaching this problem must be declarative, asking
        the processor to do the summing for you instead of trying to do it yourself.

        Below is a working solution in text ... I'll leave it with you to figure
        out the formatting and use of XSLFO since the nature of this question is an
        XSLT question.

        The nature of the problem is that this is a double-level grouping problem
        ... the symmetrical nature of each level is shown in the code. Sorry I
        don't have time to add comments, I'm hoping my choice of variables explains
        how the algorithm works.

        In essence, variables at each level maintain the list of interesting
        reports for the summarization.

        By providing exactly what you have and exactly what you are looking for,
        you made it easy to volunteer an answer ... thank you.

        I hope this helps.

        ..................... Ken

        T:\ftemp>type nitin.xml
        <?xml version="1.0" encoding="utf-8"?>
        <List>
        <reports>
        <Currency>CA</Currency>
        <ChequeDate>05-16-2001</ChequeDate>
        <DepositAmt>12.0</DepositAmt>
        </reports>
        <reports>
        <Currency>CA</Currency>
        <ChequeDate>05-16-2001</ChequeDate>
        <DepositAmt>14.0</DepositAmt>
        </reports>
        <reports>
        <Currency>CA</Currency>
        <ChequeDate>05-20-2001</ChequeDate>
        <DepositAmt>18.0</DepositAmt>
        </reports>
        <reports>
        <Currency>CA</Currency>
        <ChequeDate>05-20-2001</ChequeDate>
        <DepositAmt>16.0</DepositAmt>
        </reports>
        <reports>
        <Currency>US</Currency>
        <ChequeDate>05-16-2001</ChequeDate>
        <DepositAmt>12.0</DepositAmt>
        </reports>
        <reports>
        <Currency>US</Currency>
        <ChequeDate>05-16-2001</ChequeDate>
        <DepositAmt>14.0</DepositAmt>
        </reports>
        </List>

        T:\ftemp>type nitin.xsl
        <?xml version="1.0" encoding="utf-8"?>
        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">

        <xsl:output method="text"/>

        <xsl:template match="/">
        <xsl:variable name="reports" select="/List/reports"/>
        <xsl:for-each select="$reports">
        <xsl:if test="generate-id(.)=
        generate-id($reports[Currency=current()/Currency])">
        Currency: <xsl:value-of select="Currency"/>
        <xsl:variable name="dates"
        select="$reports[Currency=current()/Currency]"/>
        <xsl:for-each select="$dates">
        <xsl:if test="generate-id(.)=
        generate-id($dates[ChequeDate=current()/ChequeDate])">
        <xsl:text>
        Date Amount
        </xsl:text>
        <xsl:variable name="cheques"
        select="$dates[ChequeDate=current()/ChequeDate]"/>
        <xsl:for-each select="$cheques">
        <xsl:value-of select="ChequeDate"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="DepositAmt"/>
        <xsl:text>
        </xsl:text>
        </xsl:for-each>
        <xsl:text>Subtotal: </xsl:text>
        <xsl:value-of select="sum($cheques/DepositAmt)"/>
        <xsl:text>
        </xsl:text>
        </xsl:if>
        </xsl:for-each>
        <xsl:text>Total: </xsl:text>
        <xsl:value-of select="sum($dates/DepositAmt)"/>
        <xsl:text>
        </xsl:text>
        </xsl:if>
        </xsl:for-each>
        </xsl:template>

        </xsl:stylesheet>

        T:\ftemp>xt nitin.xml nitin.xsl

        Currency: CA
        Date Amount
        05-16-2001 12.0
        05-16-2001 14.0
        Subtotal: 26

        Date Amount
        05-20-2001 18.0
        05-20-2001 16.0
        Subtotal: 34
        Total: 60

        Currency: US
        Date Amount
        05-16-2001 12.0
        05-16-2001 14.0
        Subtotal: 26
        Total: 26

        T:\ftemp>


        --
        Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO - Feb 18-22, 2002

        G. Ken Holman mailto:gkholman@...
        Crane Softwrights Ltd. http://www.CraneSoftwrights.com/f/
        Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
        ISBN 0-13-065196-6 Definitive XSLT & XPath
        ISBN 1-894049-08-X Practical Transformation Using XSLT and XPath
        ISBN 1-894049-07-1 Practical Formatting Using XSLFO
        XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
        articles, training(instructor-live,Internet-live,web/CD,licensed)
        Next public training: 02-02-11,12,14,15,18,21,03-04,05,06,08,11,
        - 04-08,09,10,12,05-14,15,06-04,07
      • imjnitin
        Hi Ken, Thanks for taking time out and explaining the solution in such great detail. I was able to solve the problem. I had one more approach but your s was
        Message 3 of 3 , Feb 12, 2002
          Hi Ken,
          Thanks for taking time out and explaining the solution in such great
          detail. I was able to solve the problem.
          I had one more approach but your's was very simple and efficient to
          use.

          Thank you very much!

          Regards,
          Nitin Jain

          --- In XSL-FO@y..., "G. Ken Holman" <gkholman@c...> wrote:
          > At 2002-02-08 09:58 -0800, Nitin Jain wrote:
          > >The XML structure looks like:
          > >...
          > >And the requirement is to generate the report like
          > >this:
          > >...
          > >The XML data is presorted.( wrt currency type and date
          > >)
          >
          > Fine.
          >
          > >So for this I have a subTotal variable which I
          > >declared before the
          > >template. And when I loop within reports, after
          > >printing the first subTotal
          > >correctly in the second (and subsequent) subTotals it
          > >prints incorrect
          > >values.
          > >And the reason is that I try to reset the variable to
          > >zero when a new date
          > >entry is found. But to my surprise the variable is not
          > >resseted.
          >
          > You are approaching the problem incorrectly.
          >
          > Variables in XSLT do not vary ... they are fixed once they are
          bound.
          >
          > Your philosophy when approaching this problem must be declarative,
          asking
          > the processor to do the summing for you instead of trying to do it
          yourself.
          >
          > Below is a working solution in text ... I'll leave it with you to
          figure
          > out the formatting and use of XSLFO since the nature of this
          question is an
          > XSLT question.
          >
          > The nature of the problem is that this is a double-level grouping
          problem
          > ... the symmetrical nature of each level is shown in the code.
          Sorry I
          > don't have time to add comments, I'm hoping my choice of variables
          explains
          > how the algorithm works.
          >
          > In essence, variables at each level maintain the list of
          interesting
          > reports for the summarization.
          >
          > By providing exactly what you have and exactly what you are looking
          for,
          > you made it easy to volunteer an answer ... thank you.
          >
          > I hope this helps.
          >
          > ..................... Ken
          >
          > T:\ftemp>type nitin.xml
          > <?xml version="1.0" encoding="utf-8"?>
          > <List>
          > <reports>
          > <Currency>CA</Currency>
          > <ChequeDate>05-16-2001</ChequeDate>
          > <DepositAmt>12.0</DepositAmt>
          > </reports>
          > <reports>
          > <Currency>CA</Currency>
          > <ChequeDate>05-16-2001</ChequeDate>
          > <DepositAmt>14.0</DepositAmt>
          > </reports>
          > <reports>
          > <Currency>CA</Currency>
          > <ChequeDate>05-20-2001</ChequeDate>
          > <DepositAmt>18.0</DepositAmt>
          > </reports>
          > <reports>
          > <Currency>CA</Currency>
          > <ChequeDate>05-20-2001</ChequeDate>
          > <DepositAmt>16.0</DepositAmt>
          > </reports>
          > <reports>
          > <Currency>US</Currency>
          > <ChequeDate>05-16-2001</ChequeDate>
          > <DepositAmt>12.0</DepositAmt>
          > </reports>
          > <reports>
          > <Currency>US</Currency>
          > <ChequeDate>05-16-2001</ChequeDate>
          > <DepositAmt>14.0</DepositAmt>
          > </reports>
          > </List>
          >
          > T:\ftemp>type nitin.xsl
          > <?xml version="1.0" encoding="utf-8"?>
          > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          > version="1.0">
          >
          > <xsl:output method="text"/>
          >
          > <xsl:template match="/">
          > <xsl:variable name="reports" select="/List/reports"/>
          > <xsl:for-each select="$reports">
          > <xsl:if test="generate-id(.)=
          > generate-id($reports[Currency=current
          ()/Currency])">
          > Currency: <xsl:value-of select="Currency"/>
          > <xsl:variable name="dates"
          > select="$reports[Currency=current
          ()/Currency]"/>
          > <xsl:for-each select="$dates">
          > <xsl:if test="generate-id(.)=
          > generate-id($dates[ChequeDate=current
          ()/ChequeDate])">
          > <xsl:text>
          > Date Amount
          > </xsl:text>
          > <xsl:variable name="cheques"
          > select="$dates[ChequeDate=current
          ()/ChequeDate]"/>
          > <xsl:for-each select="$cheques">
          > <xsl:value-of select="ChequeDate"/>
          > <xsl:text> </xsl:text>
          > <xsl:value-of select="DepositAmt"/>
          > <xsl:text>
          > </xsl:text>
          > </xsl:for-each>
          > <xsl:text>Subtotal: </xsl:text>
          > <xsl:value-of select="sum($cheques/DepositAmt)"/>
          > <xsl:text>
          > </xsl:text>
          > </xsl:if>
          > </xsl:for-each>
          > <xsl:text>Total: </xsl:text>
          > <xsl:value-of select="sum($dates/DepositAmt)"/>
          > <xsl:text>
          > </xsl:text>
          > </xsl:if>
          > </xsl:for-each>
          > </xsl:template>
          >
          > </xsl:stylesheet>
          >
          > T:\ftemp>xt nitin.xml nitin.xsl
          >
          > Currency: CA
          > Date Amount
          > 05-16-2001 12.0
          > 05-16-2001 14.0
          > Subtotal: 26
          >
          > Date Amount
          > 05-20-2001 18.0
          > 05-20-2001 16.0
          > Subtotal: 34
          > Total: 60
          >
          > Currency: US
          > Date Amount
          > 05-16-2001 12.0
          > 05-16-2001 14.0
          > Subtotal: 26
          > Total: 26
          >
          > T:\ftemp>
          >
          >
          > --
          > Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO - Feb 18-22, 2002
          >
          > G. Ken Holman mailto:gkholman@C...
          > Crane Softwrights Ltd. http://www.CraneSoftwrights.com/f/
          > Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
          > ISBN 0-13-065196-6 Definitive XSLT & XPath
          > ISBN 1-894049-08-X Practical Transformation Using XSLT and XPath
          > ISBN 1-894049-07-1 Practical Formatting Using XSLFO
          > XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
          > articles, training(instructor-live,Internet-live,web/CD,licensed)
          > Next public training: 02-02-11,12,14,15,18,21,03-04,05,06,08,11,
          > - 04-08,09,10,12,05-14,15,06-04,07
        Your message has been successfully submitted and would be delivered to recipients shortly.