Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 18th, 2008, 08:29 AM
Registered User
 
Join Date: Apr 2008
Location: minsk, , Belarus.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default xslt when loop problem

Hello!
please help with "when" loop(see comments below)
cannot exit the loop despite matching the conditions

xml to be transformed:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test2.xsl"?>
<data>
    <item id="i1" sector="s1" subsector="u1">561</item>
    <item id="i10" sector="s3" subsector="u4">15</item>
    <item id="i22" sector="s2" subsector="u2">1234</item>
    <item id="i11" sector="s1" subsector="u2">123</item>
    <item id="i17" sector="s1" subsector="u3">165</item>
    <item id="i61" sector="s2" subsector="u1">346</item>
    <item id="i12" sector="s2" subsector="u5">3425</item>
    <item id="i2" sector="s3" subsector="u4">78</item>
    <item id="i14" sector="s3" subsector="u4">51</item>
    <item id="i21" sector="s1" subsector="u5">346</item>
    <item id="i39" sector="s3" subsector="u2">463</item>
    <item id="i44" sector="s2" subsector="u3">151</item>
    <item id="i89" sector="s1" subsector="u1">451</item>
    <item id="i81" sector="s2" subsector="u4">771</item>
    <item id="i36" sector="s2" subsector="u5">5654</item>
    <item id="i27" sector="s3" subsector="u3">362</item>
    <item id="i15" sector="s1" subsector="u5">234</item>
    <item id="i18" sector="s3" subsector="u2">73</item>
    <item id="i51" sector="s3" subsector="u5">567</item>
    <item id="i26" sector="s1" subsector="u4">17</item>
    <item id="i95" sector="s3" subsector="u5">67489</item>
    <item id="i13" sector="s1" subsector="u3">54</item>
    <item id="i71" sector="s1" subsector="u3">2</item>
    <item id="i23" sector="s2" subsector="u1">345</item>
    <item id="i7" sector="s2" subsector="u1">67</item>
    <item id="i80" sector="s1" subsector="u3">7754</item>
    <item id="i9" sector="s3" subsector="u4">343</item>
    <item id="i4" sector="s1" subsector="u2">51</item>
    <item id="i99" sector="s3" subsector="u5">5637</item>
    <item id="i16" sector="s1" subsector="u5">716</item>
    <item id="i85" sector="s2" subsector="u1">4561</item>
    <item id="i73" sector="s1" subsector="u3">46</item>
    <item id="i48" sector="s2" subsector="u1">82</item>
    <item id="i20" sector="s2" subsector="u2">5461</item>
    <item id="i66" sector="s1" subsector="u1">4526</item>
    <item id="i52" sector="s3" subsector="u5">73</item>
    <item id="i64" sector="s3" subsector="u4">56</item>
    <item id="i54" sector="s2" subsector="u4">827</item>
    <item id="i19" sector="s2" subsector="u4">1634</item>
    <item id="i25" sector="s3" subsector="u3">546</item>
    <item id="i50" sector="s1" subsector="u2">61</item>
    <item id="i63" sector="s2" subsector="u3">611</item>
    <item id="i42" sector="s2" subsector="u4">36</item>
    <item id="i67" sector="s1" subsector="u2">61</item>
    <item id="i98" sector="s1" subsector="u1">541</item>
    <item id="i76" sector="s1" subsector="u5">61</item>
    <item id="i62" sector="s3" subsector="u5">146</item>
    <item id="i68" sector="s2" subsector="u1">346</item>
    <item id="i82" sector="s1" subsector="u5">15</item>
    <item id="i83" sector="s2" subsector="u5">43</item>
    <item id="i3" sector="s3" subsector="u5">345</item>
    <item id="i6" sector="s1" subsector="u5">134</item>
    <item id="i30" sector="s1" subsector="u4">15</item>
    <item id="i100" sector="s2" subsector="u2">27</item>
    <item id="i101" sector="s1" subsector="u3">293</item>
    <item id="i102" sector="s4" subsector="u4">282</item>
    <item id="i103" sector="s3" subsector="u2">389</item>
    <item id="i104" sector="s2" subsector="u3">289</item>
    <item id="i105" sector="s3" subsector="u5">754</item>
    <item id="i106" sector="s2" subsector="u2">617</item>
    <item id="i107" sector="s3" subsector="u2">859</item>
    <item id="i108" sector="s2" subsector="u1">809</item>
    <item id="i109" sector="s2" subsector="u2">15</item>
    <item id="i110" sector="s2" subsector="u6">394</item>
    <item id="i111" sector="s1" subsector="u3">855</item>
    <item id="i112" sector="s2" subsector="u1">693</item>
    <item id="i113" sector="s3" subsector="u4">900</item>
    <item id="i114" sector="s1" subsector="u5">652</item>
    <item id="i115" sector="s1" subsector="u4">55</item>
    <item id="i116" sector="s1" subsector="u6">907</item>
    <item id="i117" sector="s2" subsector="u2">699</item>
    <item id="i118" sector="s2" subsector="u5">404</item>
    <item id="i119" sector="s4" subsector="u6">284</item>
    <item id="i120" sector="s4" subsector="u4">857</item>
    <item id="i121" sector="s3" subsector="u3">145</item>
    <item id="i122" sector="s3" subsector="u4">645</item>
    <item id="i123" sector="s1" subsector="u4">777</item>
    <item id="i124" sector="s1" subsector="u3">87</item>
    <item id="i125" sector="s3" subsector="u2">586</item>
    <item id="i126" sector="s1" subsector="u3">452</item>
    <item id="i127" sector="s2" subsector="u2">983</item>
    <item id="i128" sector="s2" subsector="u4">72</item>
    <item id="i129" sector="s2" subsector="u6">260</item>
    <item id="i130" sector="s2" subsector="u4">155</item>
    <item id="i131" sector="s3" subsector="u4">705</item>
    <item id="i132" sector="s1" subsector="u1">788</item>
    <item id="i133" sector="s3" subsector="u1">590</item>
    <item id="i134" sector="s3" subsector="u5">259</item>
    <item id="i135" sector="s2" subsector="u5">502</item>
    <item id="i136" sector="s3" subsector="u4">654</item>
    <item id="i137" sector="s3" subsector="u6">950</item>
    <item id="i138" sector="s4" subsector="u6">210</item>
    <item id="i139" sector="s2" subsector="u4">424</item>
    <item id="i140" sector="s1" subsector="u5">618</item>
    <item id="i141" sector="s4" subsector="u4">317</item>
    <item id="i142" sector="s2" subsector="u5">223</item>
    <item id="i143" sector="s1" subsector="u5">240</item>
    <item id="i144" sector="s1" subsector="u2">479</item>
    <item id="i145" sector="s2" subsector="u4">695</item>
    <item id="i146" sector="s3" subsector="u3">231</item>
    <item id="i147" sector="s2" subsector="u4">352</item>
    <item id="i148" sector="s4" subsector="u3">164</item>
    <item id="i149" sector="s3" subsector="u2">92</item>
    <item id="i150" sector="s4" subsector="u6">604</item>
    <item id="i151" sector="s1" subsector="u6">655</item>
    <item id="i152" sector="s3" subsector="u6">451</item>
    <item id="i153" sector="s3" subsector="u5">238</item>
    <item id="i154" sector="s3" subsector="u2">551</item>
    <item id="i155" sector="s4" subsector="u1">571</item>
    <item id="i156" sector="s4" subsector="u4">657</item>
    <item id="i157" sector="s4" subsector="u6">84</item>
    <item id="i158" sector="s2" subsector="u4">137</item>
    <item id="i159" sector="s2" subsector="u3">464</item>
    <item id="i160" sector="s1" subsector="u2">69</item>
    <item id="i161" sector="s4" subsector="u2">431</item>
    <item id="i162" sector="s3" subsector="u5">550</item>
    <item id="i163" sector="s4" subsector="u1">573</item>
    <item id="i164" sector="s1" subsector="u4">307</item>
    <item id="i165" sector="s2" subsector="u3">867</item>
    <item id="i166" sector="s3" subsector="u4">733</item>
    <item id="i167" sector="s3" subsector="u4">508</item>
    <item id="i168" sector="s1" subsector="u5">546</item>
    <item id="i169" sector="s4" subsector="u4">406</item>
    <item id="i170" sector="s1" subsector="u3">618</item>
    <item id="i171" sector="s3" subsector="u3">215</item>
    <item id="i172" sector="s1" subsector="u4">365</item>
    <item id="i173" sector="s3" subsector="u5">4</item>
    <item id="i174" sector="s3" subsector="u4">99</item>
    <item id="i175" sector="s2" subsector="u2">173</item>
    <item id="i176" sector="s3" subsector="u4">145</item>
    <item id="i177" sector="s2" subsector="u5">286</item>
    <item id="i178" sector="s4" subsector="u5">477</item>
    <item id="i179" sector="s1" subsector="u2">388</item>
    <item id="i180" sector="s4" subsector="u5">231</item>
    <item id="i181" sector="s3" subsector="u4">506</item>
    <item id="i182" sector="s3" subsector="u6">430</item>
    <item id="i183" sector="s1" subsector="u5">513</item>
    <item id="i184" sector="s2" subsector="u5">684</item>
    <item id="i185" sector="s2" subsector="u2">908</item>
    <item id="i186" sector="s2" subsector="u2">582</item>
    <item id="i187" sector="s4" subsector="u3">203</item>
    <item id="i188" sector="s3" subsector="u2">142</item>
    <item id="i189" sector="s3" subsector="u4">238</item>
    <item id="i190" sector="s3" subsector="u2">527</item>
    <item id="i191" sector="s2" subsector="u2">492</item>
    <item id="i192" sector="s2" subsector="u1">379</item>
    <item id="i193" sector="s2" subsector="u3">256</item>
    <item id="i194" sector="s3" subsector="u6">608</item>
    <item id="i195" sector="s1" subsector="u2">700</item>
    <item id="i196" sector="s1" subsector="u3">281</item>
    <item id="i197" sector="s3" subsector="u3">627</item>
    <item id="i198" sector="s2" subsector="u1">966</item>
    <item id="i199" sector="s2" subsector="u5">990</item>
    <item id="i200" sector="s1" subsector="u2">465</item>
    <item id="i201" sector="s2" subsector="u4">586</item>
    <item id="i202" sector="s1" subsector="u2">272</item>
    <item id="i203" sector="s2" subsector="u1">370</item>
    <item id="i204" sector="s2" subsector="u5">334</item>
    <item id="i205" sector="s2" subsector="u6">255</item>
    <item id="i206" sector="s2" subsector="u3">192</item>
    <item id="i207" sector="s2" subsector="u2">467</item>
    <item id="i208" sector="s4" subsector="u2">282</item>
    <item id="i209" sector="s2" subsector="u4">446</item>
    <item id="i210" sector="s2" subsector="u3">979</item>
    <item id="i211" sector="s1" subsector="u3">11</item>
    <item id="i212" sector="s3" subsector="u5">829</item>
    <item id="i213" sector="s2" subsector="u2">178</item>
    <item id="i214" sector="s4" subsector="u4">655</item>
    <item id="i215" sector="s2" subsector="u6">458</item>
    <item id="i216" sector="s3" subsector="u5">747</item>
    <item id="i217" sector="s3" subsector="u2">66</item>
    <item id="i218" sector="s3" subsector="u1">38</item>
    <item id="i219" sector="s3" subsector="u4">700</item>
    <item id="i220" sector="s2" subsector="u3">799</item>
    <item id="i221" sector="s1" subsector="u1">57</item>
    <item id="i222" sector="s3" subsector="u1">358</item>
    <item id="i223" sector="s1" subsector="u2">276</item>
    <item id="i224" sector="s3" subsector="u2">410</item>
    <item id="i225" sector="s4" subsector="u6">824</item>
    <item id="i226" sector="s3" subsector="u2">454</item>
    <item id="i227" sector="s2" subsector="u6">904</item>
    <item id="i228" sector="s3" subsector="u3">584</item>
    <item id="i229" sector="s3" subsector="u4">450</item>
    <item id="i230" sector="s1" subsector="u5">451</item>
    <item id="i231" sector="s2" subsector="u5">602</item>
    <item id="i232" sector="s3" subsector="u3">524</item>
    <item id="i233" sector="s1" subsector="u3">751</item>
    <item id="i234" sector="s2" subsector="u2">530</item>
    <item id="i235" sector="s1" subsector="u5">7</item>
    <item id="i236" sector="s2" subsector="u4">836</item>
    <item id="i237" sector="s3" subsector="u6">850</item>
    <item id="i238" sector="s2" subsector="u4">719</item>
    <item id="i239" sector="s4" subsector="u1">839</item>
    <item id="i240" sector="s2" subsector="u6">800</item>
    <item id="i241" sector="s4" subsector="u4">311</item>
    <item id="i242" sector="s1" subsector="u3">785</item>
    <item id="i243" sector="s2" subsector="u4">792</item>
    <item id="i244" sector="s2" subsector="u4">18</item>
    <item id="i245" sector="s2" subsector="u5">299</item>
    <item id="i246" sector="s3" subsector="u2">107</item>
    <item id="i247" sector="s3" subsector="u5">396</item>
    <item id="i248" sector="s2" subsector="u6">805</item>
    <item id="i249" sector="s1" subsector="u5">883</item>
    <item id="i250" sector="s4" subsector="u3">922</item>
    <item id="i251" sector="s2" subsector="u2">632</item>
    <item id="i252" sector="s2" subsector="u3">980</item>
    <item id="i253" sector="s1" subsector="u4">689</item>
    <item id="i254" sector="s2" subsector="u6">641</item>
    <item id="i255" sector="s4" subsector="u4">314</item>

    <sector id="s1">Sector 1</sector>
    <sector id="s2">Sector 2</sector>
    <sector id="s3">Sector 3</sector>

    <subsector id="u1">Subsector 1</subsector>
    <subsector id="u2">Subsector 2</subsector>
    <subsector id="u3">Subsector 3</subsector>
    <subsector id="u4">Subsector 4</subsector>
    <subsector id="u5">Subsector 5</subsector>
</data>
xsl transformation:

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


     <xsl:output method="html"/>


     <xsl:variable name="starspacer">**************</xsl:variable>
     <xsl:variable name="plusspacer">+++++++++++++</xsl:variable>
     <xsl:variable name="minusspacer">--------------------</xsl:variable>


     <xsl:variable name="sectorNumber" select="count(/data/sector)"/>
     <xsl:variable name="subSectorNumber" select="count(/data/subsector)"/>


     <xsl:template match="/">
          <html>
               <head>

                    <style type="text/css" media="print">h6 {page-break-before: always}</style>
               </head>
               <body>

                    <xsl:apply-templates select="/data/subsector[1]">
                    </xsl:apply-templates>
               </body>
          </html>
     </xsl:template>


     <xsl:template match="/data/subsector">
          <xsl:param name="subSectorPosition" select="1"/>
          <xsl:param name="sectorPosition" select="1"/>
          <xsl:param name="subSectorId" select="@id"/>
          <xsl:param name="sectorId" select="/data/sector[$sectorPosition]/@id"/>


          <xsl:if test="$subSectorPosition=1">
               <xsl:value-of select="$starspacer"/><br/>
               <xsl:value-of select="/data/sector[$sectorPosition]"/><br/>
               <xsl:value-of select="$starspacer"/><br/>
          </xsl:if>


          <xsl:value-of select="."/>
          <br/>
          <xsl:value-of select="$plusspacer"/><br/><br/>


          <div>
               <span>
                     id
               </span>
               <xsl:text> </xsl:text>
               <span>
                     value
               </span>
               <br/>
               <xsl:value-of select="$minusspacer"/>
               <br/>


               <xsl:for-each select="/data/item[$sectorId=@sector and $subSectorId=@subsector]">
                    <span>
                          <xsl:value-of select="@id"/>
                    </span>
                    <xsl:text> </xsl:text>
                    <span>
                          <xsl:value-of select="."/>
                    </span>
                    <br/>
               </xsl:for-each>


               <xsl:if test="/data/subsector[last()]!=current()">
                    <xsl:value-of select="$minusspacer"/>
               </xsl:if>
               <br/>
          </div>


          <xsl:choose>

               <xsl:when test="$subSectorPosition!=$subSectorNumber">
                    <xsl:apply-templates select="following::subsector[1]">
                         <xsl:with-param name="sectorPosition" select="number($sectorPosition)"/>
                         <xsl:with-param name="subSectorPosition" select="number($subSectorPosition+1)"/>
                    </xsl:apply-templates>
               </xsl:when>

               <!-- Moving to next Sector when reached the end of previous sector
               and setting subSectorPosition to 1 -->
               <xsl:when test="$subSectorPosition=$subSectorNumber and $sectorPosition!=$sectorNumber">
                    <xsl:apply-templates select="/data/subsector">
                         <xsl:with-param name="sectorPosition" select="number($sectorPosition+1)"/>
                         <xsl:with-param name="subSectorPosition" select="1"/>
                    </xsl:apply-templates>
               </xsl:when>


               <xsl:otherwise>
                    <xsl:text>it supposed to stop here</xsl:text><br/>
               </xsl:otherwise>
          </xsl:choose>

     </xsl:template>

</xsl:stylesheet>


Reply With Quote
  #2 (permalink)  
Old April 18th, 2008, 08:47 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Wow, I think you have tried to apply a good old procedural language to the wonderfully functional XSLT programming language.

Try something like this instead:

Code:
<xsl:for-each select="/data/sector">
  <xsl:variable name="sector" select="."/>

  <xsl:for-each select="/data/subSector">
    <xsl:variable name="subSector" select="."/>

    <xsl:variable name="item" select="/data/item[@sector = $sector/@id and @subSector = $subSector/@id]"/>

    Sector: <xsl:value-of select="$sector/@id">
    Sub Sector: <xsl:value-of select="$subSector/@id">
    Item ID: <xsl:value-of select="$item/@id">
    Item Text: <xsl:value-of select="$item/text()">

  </xsl:for-each>

</xsl:for-each>
/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested For-Each Loop In XSLT dghosh XSLT 10 July 2nd, 2008 04:34 AM
XSLT Loop? kwilliams XSLT 1 June 16th, 2008 06:04 PM
FOR LOOP IN XSLT im_himanshu2004 XSLT 0 October 10th, 2007 02:27 PM
javascript 'for' loop in xslt mummra1 XSLT 22 February 6th, 2007 04:26 PM
loop in xslt vazarshahy XSLT 6 June 7th, 2006 04:46 PM



All times are GMT -4. The time now is 01:56 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.