XSLT - counting the number of xsl:if which are true...

Discussion in 'XML' started by Lizard, Oct 7, 2003.

  1. Lizard

    Lizard Guest

    OK, total newbie here, so this may be a mind-numbingly dumb question AND
    I may be phrasing it badly.

    I have an xsl:template which looks like this:

    <xsl:template match="LoanRecord">
    <hr>
    <xsl:number count="LoanRecord" format="1"/><br/>
    Loan ID:<xsl:value-of select="loan_no"> </xsl:value-of><br/>
    Calculated CLTV:<xsl:value-of select="format-number (curr_balance div
    orig_amount,'##%')"> </xsl:value-of>
    <xsl:if test="(number(curr_balance div orig_amount) &gt; 0.8)">
    <i> (High LTV)</i>
    </xsl:if>
    </hr>
    </xsl:template>

    In short, it prints some data from the input file, and calculates a
    value, and, if that value is above a certain value, it also outputs a
    warning message.

    This works.

    What I want, though, is to have several xsl:if statements in there, and,
    at the end, to tally all those who evaluated to true. However, there
    seems to be no way to easily keep a running count of things. I am slowly
    begining to suspect this is by design. I am also aware I haven't fully
    inhaled the full syntax and function library of xslt, much less grokked
    all the cool things which can be done with it, so I am wondering if I
    might be missing something. (I know I can easily count all the nodes
    which meet a given condition; what I want to do is, within a record,
    check for 'n' conditions and then count the total number of conditions
    satisfied.)
    Lizard, Oct 7, 2003
    #1
    1. Advertising

  2. Lizard

    Karthik.S Guest

    Have you tried <xsl:choose> ?

    Lizard wrote:
    > OK, total newbie here, so this may be a mind-numbingly dumb question AND
    > I may be phrasing it badly.
    >
    > I have an xsl:template which looks like this:
    >
    > <xsl:template match="LoanRecord">
    > <hr>
    > <xsl:number count="LoanRecord" format="1"/><br/>
    > Loan ID:<xsl:value-of select="loan_no"> </xsl:value-of><br/>
    > Calculated CLTV:<xsl:value-of select="format-number
    > (curr_balance div orig_amount,'##%')"> </xsl:value-of>
    > <xsl:if test="(number(curr_balance div orig_amount) &gt; 0.8)">
    > <i> (High LTV)</i>
    > </xsl:if>
    > </hr>
    > </xsl:template>
    >
    > In short, it prints some data from the input file, and calculates a
    > value, and, if that value is above a certain value, it also outputs a
    > warning message.
    >
    > This works.
    >
    > What I want, though, is to have several xsl:if statements in there, and,
    > at the end, to tally all those who evaluated to true. However, there
    > seems to be no way to easily keep a running count of things. I am slowly
    > begining to suspect this is by design. I am also aware I haven't fully
    > inhaled the full syntax and function library of xslt, much less grokked
    > all the cool things which can be done with it, so I am wondering if I
    > might be missing something. (I know I can easily count all the nodes
    > which meet a given condition; what I want to do is, within a record,
    > check for 'n' conditions and then count the total number of conditions
    > satisfied.)
    >
    Karthik.S, Oct 7, 2003
    #2
    1. Advertising

  3. What about this:

    <xsl:variable name="vHits">
    <xsl:if test="expression1">1</xsl:if>
    <xsl:if test="expression2">1</xsl:if>
    . . . . . . . . . .
    <xsl:if test="expressionN">1</xsl:if>
    </xsl:variable>

    <xsl:variable name="vhitCount" select="string-length($vHits)"/>


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL


    "Lizard" <> wrote in message
    news:...
    > OK, total newbie here, so this may be a mind-numbingly dumb question AND
    > I may be phrasing it badly.
    >
    > I have an xsl:template which looks like this:
    >
    > <xsl:template match="LoanRecord">
    > <hr>
    > <xsl:number count="LoanRecord" format="1"/><br/>
    > Loan ID:<xsl:value-of select="loan_no"> </xsl:value-of><br/>
    > Calculated CLTV:<xsl:value-of select="format-number (curr_balance div
    > orig_amount,'##%')"> </xsl:value-of>
    > <xsl:if test="(number(curr_balance div orig_amount) &gt; 0.8)">
    > <i> (High LTV)</i>
    > </xsl:if>
    > </hr>
    > </xsl:template>
    >
    > In short, it prints some data from the input file, and calculates a
    > value, and, if that value is above a certain value, it also outputs a
    > warning message.
    >
    > This works.
    >
    > What I want, though, is to have several xsl:if statements in there, and,
    > at the end, to tally all those who evaluated to true. However, there
    > seems to be no way to easily keep a running count of things. I am slowly
    > begining to suspect this is by design. I am also aware I haven't fully
    > inhaled the full syntax and function library of xslt, much less grokked
    > all the cool things which can be done with it, so I am wondering if I
    > might be missing something. (I know I can easily count all the nodes
    > which meet a given condition; what I want to do is, within a record,
    > check for 'n' conditions and then count the total number of conditions
    > satisfied.)
    >
    Dimitre Novatchev, Oct 7, 2003
    #3
  4. Lizard

    Lizard Guest

    Dimitre Novatchev wrote:

    > What about this:
    >
    > <xsl:variable name="vHits">
    > <xsl:if test="expression1">1</xsl:if>
    > <xsl:if test="expression2">1</xsl:if>
    > . . . . . . . . . .
    > <xsl:if test="expressionN">1</xsl:if>
    > </xsl:variable>
    >
    > <xsl:variable name="vhitCount" select="string-length($vHits)"/>
    >



    Looks interesting...I will try that. Thanks!
    Lizard, Oct 7, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vijay singh
    Replies:
    1
    Views:
    426
    Martin Honnen
    Nov 4, 2004
  2. Replies:
    1
    Views:
    3,571
    A. Bolmarcich
    May 27, 2005
  3. vaibhav
    Replies:
    3
    Views:
    1,125
    vaibhav
    Aug 14, 2006
  4. bdb112
    Replies:
    45
    Views:
    1,296
    jazbees
    Apr 29, 2009
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    183
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page