This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


This seems to be a well-disguised grouping problem. You are grouping by
decision-num as well as sorting: in the first group you are outputting
"current", in the second group "previous", in the third group "previous
- 1". So I think you can use standard grouping techniques to solve the
problem. Doing it the preceding-sibling way:

<xsl:for-each select="rule[not(@decision-num =
preceding-sibling::rule/@decision-num)]">
  <xsl:sort select="@decision-num ... descending...
  <xsl:variable name="dseq" select="position()">
  <xsl:for-each select="../rule[@decision-num =
current()/@decision-num]">
    <xsl:sort select="@seq-num">
    <td><xsl:value-of select="@id ...
    <td><xsl:value-of select="@seq-num ...
    <xsl:choose>
      <xsl:when test="$dseq=1">current</xsl:when>
      <xsl:when test="$dseq=1">previous</xsl:when>
      ...

Michael Kay
Software AG
home: Michael.H.Kay@ntlworld.com
work: Michael.Kay@softwareag.com 

> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com 
> [mailto:owner-xsl-list@lists.mulberrytech.com] On Behalf Of 
> Ryan.Kelly@equifax.com
> Sent: 14 June 2002 14:51
> To: xsl-list@lists.mulberrytech.com
> Subject: [xsl] Problems calling template from within sorted 
> <xsl:for-each> loop.
> 
> 
> 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@equifax.com.
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]