RE: [xsl] Alternatives to "Stopping" a for-each

Subject: RE: [xsl] Alternatives to "Stopping" a for-each
From: <trond.huso@xxxxxx>
Date: Thu, 14 Feb 2013 14:35:24 +0000
Hi Michael and Serhiy,

Thanks for all your suggestions.

Michael: yes, I am fully aware of for-each as a typical procedural language
for-each.

Just to explain:
The line 7-11-6-12 has come out because:
7 = first ranked horse, 11= second ranked horse, 6=third ranked horse, 12=4th
ranked horse + the winner of that race.

I get the winner of the current race from these xpaths:
<xsl:variable name="winner" select="Winners/Winner/@StartNo" />
                    <xsl:variable name="starts" select="Starts/Start" />
                    <xsl:variable name="startsnos"
select="Starts/Start/StartNo" />

As I see it, option/alternative a is not an option.
It seems that option b could be the solution

That means it must look in the lines of (Still a work in progress as you can
see):
<xsl:template name="foo">
        <xsl:param name="start"/>
        <xsl:param name="winner" />

        <xsl:if test="$start != $winner">
            <xsl:call-template name="foo">
                <xsl:with-param name="current" select="$start - 1"/>
                <xsl:with-param name="winner" select="$winner" />
            </xsl:call-template>
        </xsl:if>

    </xsl:template>

I'm not looking at xslt3.0 even though I am assuming that my saxon version
could use it (I am on 9.4 I believe).

Trond

-----Original Message-----
From: Michael Kay [mailto:mike@xxxxxxxxxxxx]
Sent: 14. februar 2013 14:44
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Alternatives to "Stopping" a for-each

Don't think of a for-each as a loop. Think of it as a functional mapping
operation, where you apply a function (the body of the for-each) to every item
in a sequence - quite possibly in parallel. There's no concept that the system
doesn't start processing the second item until it has finished processing the
first.

Your alternatives are

(a) decide up-front which items in the sequence to process, and select them in
the select expression of the for-each

(b) if you need to make a dynamic decision, replace the for-each with a
recursion.

(c) or in XSLT 3.0, xsl:iterate gives you a "sequential" equivalent of
for-each where the way each item is processed can depend on the results of
processing previous items (and an xsl:break that terminates processing)

Michael Kay
Saxonica

On 14/02/2013 13:10, trond.huso@xxxxxx wrote:
> I have to create the following list based on xml-structure below. It is a
list of ranked horses, and where the last number (if there are more than one
number) is the winning horse.
>
> Rangering: 1. avd: 7-11-6-12 (str. 4), 2. avd: 9 (str. 1), 3. avd: 8, 4.
avd: 4-8 (str. 5), 5. avd: 6 (str. 10).
>
> The example uses the bold part of the text.
>
> So the idea is that I am sorting the ranking, looping the structure. When I
hit the winning horse, I shall stop the loop.
> Now I know that you cannot stop a for-each in xslt, so what is the best way
to this then?
>
>
>
> <Start>
>                              <StartNo>1</StartNo>
>                              <Horse>
>                                  <RegNo>N-04-0514</RegNo>
>                                  <Name>Rise Stjerna</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>8279</LicenseNo>
>                                  <FirstName>Kristine</FirstName>
>                                  <LastName>Kvasnes</LastName>
>                              </Driver>
>                              <Distance>2140</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>9</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>2</StartNo>
>                              <Horse>
>                                  <RegNo>N-07-0133</RegNo>
>                                  <Name>Bovis Rambo</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>79</LicenseNo>
>                                  <FirstName>Thor</FirstName>
>                                  <LastName>Borg</LastName>
>                              </Driver>
>                              <Distance>2140</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>6</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>3</StartNo>
>                              <Horse>
>                                  <RegNo>N-02-0165 </RegNo>
>                                  <Name>Edne Kongen</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>2014063</LicenseNo>
>                                  <FirstName>Oda</FirstName>
>                                  <LastName>Verdal</LastName>
>                              </Driver>
>                              <Distance>2140</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>11</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>4</StartNo>
>                              <Horse>
>                                  <RegNo>S-08-0191</RegNo>
>                                  <Name>Jfrvsx Kristina</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>17044</LicenseNo>
>                                  <FirstName>Xystein</FirstName>
>                                  <LastName>Tjomsland</LastName>
>                              </Driver>
>                              <Distance>2160</Distance>
>                              <Scratched>true</Scratched>
>                              <Ranking>0</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>5</StartNo>
>                              <Horse>
>                                  <RegNo>N-07-0107</RegNo>
>                                  <Name>Magnums Amadeus</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>5697</LicenseNo>
>                                  <FirstName>Arild</FirstName>
>                                  <LastName>Beres</LastName>
>                              </Driver>
>                              <Distance>2160</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>5</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>6</StartNo>
>                              <Horse>
>                                  <RegNo>578001020080549</RegNo>
>                                  <Name>Willblesen</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>30399</LicenseNo>
>                                  <FirstName>Rudolf</FirstName>
>                                  <LastName>Roland</LastName>
>                              </Driver>
>                              <Distance>2160</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>3</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>7</StartNo>
>                              <Horse>
>                                  <RegNo>N-05-0660</RegNo>
>                                  <Name>Lill Ronja</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>1537</LicenseNo>
>                                  <FirstName>Eirik</FirstName>
>                                  <LastName>Hxitomt</LastName>
>                              </Driver>
>                              <Distance>2160</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>1</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>8</StartNo>
>                              <Horse>
>                                  <RegNo>N-04-0365</RegNo>
>                                  <Name>Per Even</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>65285</LicenseNo>
>                                  <FirstName>Vidar</FirstName>
>                                  <LastName>Tjomsland</LastName>
>                              </Driver>
>                              <Distance>2160</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>7</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>9</StartNo>
>                              <Horse>
>                                  <RegNo>N-05-0179</RegNo>
>                                  <Name>Es Glimten</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>5040</LicenseNo>
>                                  <FirstName>Johan Herbjxrn</FirstName>
>                                  <LastName>Undem</LastName>
>                              </Driver>
>                              <Distance>2160</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>10</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>10</StartNo>
>                              <Horse>
>                                  <RegNo>N-06-0047</RegNo>
>                                  <Name>Pola Linus</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>2127</LicenseNo>
>                                  <FirstName>Kristian</FirstName>
>                                  <LastName>Malmin</LastName>
>                              </Driver>
>                              <Distance>2180</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>8</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>11</StartNo>
>                              <Horse>
>                                  <RegNo>N-03-0261</RegNo>
>                                  <Name>Odintroll</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>8345</LicenseNo>
>                                  <FirstName>Ole Johan</FirstName>
>                                  <LastName>Xstre</LastName>
>                              </Driver>
>                              <Distance>2180</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>2</Ranking>
>                          </Start>
>                          <Start>
>                              <StartNo>12</StartNo>
>                              <Horse>
>                                  <RegNo>N-07-0209</RegNo>
>                                  <Name>Tallas</Name>
>                              </Horse>
>                              <Driver>
>                                  <LicenseNo>5740</LicenseNo>
>                                  <FirstName>Jan Rune</FirstName>
>                                  <LastName>Gaustad</LastName>
>                              </Driver>
>                              <Distance>2180</Distance>
>                              <Scratched>false</Scratched>
>                              <Ranking>4</Ranking>
>                          </Start>
>
>
> Best regards,
>
> Trond Husx
> System Developer
> www.ntb.no

Current Thread