[xsl] Problems calling template from within sorted <xsl:for-each> loop.

Subject: [xsl] Problems calling template from within sorted <xsl:for-each> loop.
From: Ryan.Kelly@xxxxxxxxxxx
Date: Fri, 14 Jun 2002 14:50:55 +0100
I am attempting to translate the following XML fragment into the HTML
below.

Sorting criteria:

1. decision_num (descending)
2. seq_num (ascending)

--------------------------------------------------------------

<list>
    <rule id="f" seq_num="1" decision_num="1"/>
    <rule id="g" seq_num="2" decision_num="1"/>
    <rule id="a" seq_num="1" decision_num="4"/>
    <rule id="d" seq_num="1" decision_num="2"/>
    <rule id="b" seq_num="2" decision_num="4"/>
    <rule id="c" seq_num="3" decision_num="4"/>
    <rule id="e" seq_num="2" decision_num="2"/>
</list>

--------------------------------------------------------------

<tr>
    <td>a</td>
    <td>1</td>
    <td>current</td>
</tr>
<tr>
    <td>b</td>
    <td>2</td>
    <td>current</td>
</tr>
<tr>
    <td>c</td>
    <td>3</td>
    <td>current</td>
</tr>
<tr>
    <td>d</td>
    <td>1</td>
    <td>previous</td>
</tr>
<tr>
    <td>e</td>
    <td>2</td>
    <td>previous</td>
</tr>
<tr>
    <td>f</td>
    <td>1</td>
    <td>previous - 1</td>
</tr>
<tr>
    <td>g</td>
    <td>2</td>
    <td>previous - 1</td>
</tr>

--------------------------------------------------------------

I'm having trouble with calculation of the value for the 3rd column
(current, previous etc...). This could very easily be solved in a
procedural programming language but it more difficult in XSLT. I attempted
to use a named template which called itself recursively in order to store
some state (would use variables in procedural). This attempts to walk
backwards through the node list, counting the number of decision_num
changes, until the start of the list is reached. It is then a simple to
display the correct value. The template was called from within an
<xsl:for-each> construct which was sorted according to the criteria. The
reason it doesn't work is that the "../policy_rule[$Pos]" select statements
obtain a positioned node from the originally ordered list of policy_rule
elements rather than the sorted list.

Can anyone help with an alternative solution?

<xsl:for-each select="../policy_rule">
    <xsl:sort select="@decision_no" data-type="number" order="descending"/>
    <xsl:sort select="@seq_no" data-type="number" order="ascending"/>

    <tr>
        <xsl:call-template name="StripeControl"/>

        <td class="BureauTableText" valign="top">
            <xsl:value-of select="@timestamp"/>&#160;
        </td>
        <td class="BureauTableText" valign="top">
            <xsl:value-of select="@code"/>&#160;
        </td>
        <td class="BureauTableText" valign="top">
            <xsl:value-of select="@text"/>&#160;
        </td>
        <td class="BureauTableText" valign="top">
            <xsl:value-of select="@primary_reason_ind"/>&#160;
        </td>
        <td class="BureauTableText" valign="top">

            <xsl:call-template name="CalcSystemDecision">
                <xsl:with-param name="PolicyRule" select="."/>
                <xsl:with-param name="Pos" select="position()"/>
                <xsl:with-param name="NumChanges">0</xsl:with-param>
            </xsl:call-template>

            &#160;
        </td>
    </tr>

</xsl:for-each>

<xsl:template name="CalcSystemDecision">
    <xsl:param name="PolicyRule"/>
    <xsl:param name="NumChanges"/>
    <xsl:param name="Pos"/>

    <xsl:value-of select="$Pos"/>

    <xsl:choose>
        <xsl:when test="$Pos = 1">
            <xsl:choose>
                <xsl:when test="$NumChanges = 0">
                    Current
                </xsl:when>
                <xsl:when test="$NumChanges = 1">
                    Previous
                </xsl:when>
                <xsl:otherwise>
                    -<xsl:value-of select="$NumChanges - 1"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>

            [<xsl:value-of select="$PolicyRules[$Pos]/@decision_no"/>
=<xsl:value-of select="$PolicyRules[$Pos - 1]/@decision_no"/>]

            <xsl:choose>
                <xsl:when test="$PolicyRules[$Pos]/@decision_no !
= $PolicyRules[$Pos - 1]/@decision_no">
                    <xsl:call-template name="CalcSystemDecision">
                        <xsl:with-param name="PolicyRule" select
="$PolicyRules"/>
                        <xsl:with-param name="Pos" select="$Pos - 1"/>
                        <xsl:with-param name="NumChanges" select
="$NumChanges + 1"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="CalcSystemDecision">
                        <xsl:with-param name="PolicyRule" select
="$PolicyRules"/>
                        <xsl:with-param name="Pos" select="$Pos - 1"/>
                        <xsl:with-param name="NumChanges" select
="$NumChanges"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:otherwise>
    </xsl:choose>

</xsl:template>

Thanks,
Ryan Kelly.



This message contains information from Equifax, Inc. which may be
confidential and privileged.  If you are not an intended recipient, please
refrain from any disclosure, copying, distribution or use of this
information and note that such actions are prohibited.  If you have
received this transmission in error, please notify by e:mail
postmaster@xxxxxxxxxxxx


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread