Subject: RE: [xsl] Problems calling template from within sorted <xsl:for-each> loop. From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx> Date: Fri, 14 Jun 2002 16:06:45 +0100 |
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@xxxxxxxxxxxx work: Michael.Kay@xxxxxxxxxxxxxx > -----Original Message----- > From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx > [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of > Ryan.Kelly@xxxxxxxxxxx > Sent: 14 June 2002 14:51 > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > 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"/>  > </td> > <td class="BureauTableText" valign="top"> > <xsl:value-of select="@code"/>  > </td> > <td class="BureauTableText" valign="top"> > <xsl:value-of select="@text"/>  > </td> > <td class="BureauTableText" valign="top"> > <xsl:value-of select="@primary_reason_ind"/>  > </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> > >   > </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 > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Problems calling template, David Carlisle | Thread | Re: [xsl] Problems calling template, Ryan . Kelly |
Re: [xsl] Problems calling template, Ryan . Kelly | Date | Re: [xsl] Problems calling template, David Carlisle |
Month |