Subject: Nested For-Each Loop In XSLT
Posted By: dghosh Post Date: 7/2/2008 1:02:04 AM
I need to work with a nested for-each loop in XSLT. Below is the code. Please note that I am using grouping here.

<xsl:key name="list" match="video_card" use="id"/>

<xsl:for-each select="(//video_card)[generate-id(.)=generate-id(key('list', id))]/id">

<div class="show_config_list">
   <xsl:for-each select="key('list', .)">
      <xsl:sort select="config_name"/>
      <input type="checkbox" class="config_list" value="{config_id}" name="cfg[][id]" id="cfg_{position()}" checked="checked" />
   </xsl:for-each>
</div>

If you notice, the ID keeps repeating for different groups of video cards. In other words, for every group of video cards, the ID would start at cfg_1 followed by cfg_2, cfg_3...

This is undesirable. I need it to be cfg_1 to cfg_n where n='no. of configs'.

Is there a way of doing this?

Reply By: Volder Reply Date: 7/2/2008 1:29:51 AM
ok, I see that what I have answered is not your problem.

Can you give us sample input and desired output - so it would be easier to understand your problem?

Reply By: samjudson Reply Date: 7/2/2008 2:32:55 AM
I understand you're problem, but as is often the case without seeing what the input XML looks like it is difficult to propose a solution.

/- Sam Judson : Wrox Technical Editor -/
Reply By: dghosh Reply Date: 7/2/2008 2:42:10 AM
quote:
Originally posted by samjudson

I understand you're problem, but as is often the case without seeing what the input XML looks like it is difficult to propose a solution.

/- Sam Judson : Wrox Technical Editor -/



Sorry about that. Here is an example:

<video_cards>
 <video_card>
  <id>43</id>
  <name>GeForce 8600M GT    </name>
  <VRAM>256</VRAM>
  <config_id>839</config_id>
  <config_name>M76_MacBook Pro 17&quot; 2.4 GHz Core 2 Duo GeForce 8600M GT (lab)</config_name>
 </video_card>
 <video_card>
  <id>2</id>
  <name>kHW_ATIr520Item</name>
  <VRAM>128</VRAM>
  <config_id>413</config_id>
  <config_name>iMac 20&quot; 2GHz (lab)</config_name>
 </video_card>
 <video_card>
  <id>2</id>
  <name>kHW_ATIr520Item</name>
  <VRAM>128</VRAM>
  <config_id>389</config_id>
  <config_name>iMac 20&quot; 2.16GHz (ek)</config_name>
 </video_card>
 <video_card>
  <id>5</id>
  <name>kHW_ATIr520Item</name>
  <VRAM>256</VRAM>
  <config_id>422</config_id>
  <config_name>MacBook Pro 15&quot; 2.33GHz Dual (lab)</config_name>
 </video_card>
 <video_card>
  <id>19</id>
  <name>kHW_ATIr580Item</name>
  <VRAM>512</VRAM>
  <config_id>534</config_id>
  <config_name>M43_3GHz(QuadXeon)_2GB_RadeonX1900_750</config_name>
 </video_card>
 <video_card>
  <id>9</id>
  <name>kHW_ATIrv360M11Item</name>
  <VRAM>128</VRAM>
  <config_id>629</config_id>
  <config_name>PowerBook 15&quot; 1.67 5400 (lab) G4</config_name>
 </video_card>
 <video_card>
  <id>9</id>
  <name>kHW_ATIrv360M11Item</name>
  <VRAM>128</VRAM>
  <config_id>33</config_id>
  <config_name>PowerBook 15&quot; 1.67GHz (lab) G4</config_name>
 </video_card>
 <video_card>
  <id>10</id>
  <name>kHW_ATIrv380Item</name>
  <VRAM>128</VRAM>
  <config_id>414</config_id>
  <config_name>iMac 20&quot; PPC 2.1GHz (lab) G5-1</config_name>
 </video_card>
 <video_card>
  <id>6</id>
  <name>kHW_IntelGMA950Item</name>
  <VRAM>0</VRAM>
  <config_id>456</config_id>
  <config_name>MacBook 13&quot; 2GHz core 2 duo (lab)</config_name>
 </video_card>
 <video_card>
  <id>20</id>
  <name>kHW_NVidiaGeForce7300GTItem</name>
  <VRAM>256</VRAM>
  <config_id>597</config_id>
  <config_name>M43a EVT 3GHz Seagate750 (lab)</config_name>
 </video_card>
 <video_card>
  <id>20</id>
  <name>kHW_NVidiaGeForce7300GTItem</name>
  <VRAM>256</VRAM>
  <config_id>596</config_id>
  <config_name>M43a 3GHz 2GB NVIDIAGF7300GT</config_name>
 </video_card>
 <video_card>
  <id>20</id>
  <name>kHW_NVidiaGeForce7300GTItem</name>
  <VRAM>256</VRAM>
  <config_id>537</config_id>
  <config_name>M43a EVT 3GHz (lab)</config_name>
 </video_card>
 <video_card>
  <id>20</id>
  <name>kHW_NVidiaGeForce7300GTItem</name>
  <VRAM>256</VRAM>
  <config_id>531</config_id>
  <config_name>M43A 2.66GHz 4GB CT</config_name>
 </video_card>
 <video_card>
  <id>20</id>
  <name>kHW_NVidiaGeForce7300GTItem</name>
  <VRAM>256</VRAM>
  <config_id>521</config_id>
  <config_name>m43a_3GHz_2GB_NVidiaGF7300GT</config_name>
 </video_card>
 <video_card>
  <id>8</id>
  <name>kHW_NVidiaQuadroFX4500Item</name>
  <VRAM>512</VRAM>
  <config_id>411</config_id>
  <config_name>PowerMac 2.5GHz Quad (lab) G5-1</config_name>
 </video_card>
 <video_card>
  <id>8</id>
  <name>kHW_NVidiaQuadroFX4500Item</name>
  <VRAM>512</VRAM>
  <config_id>412</config_id>
  <config_name>MacPro 3GHz Quad Dual (lab)-1</config_name>
 </video_card>
 <video_card>
  <id>8</id>
  <name>kHW_NVidiaQuadroFX4500Item</name>
  <VRAM>512</VRAM>
  <config_id>583</config_id>
  <config_name>MacPro 3GHz Seagate 750GB new</config_name>
 </video_card>
 <video_card>
  <id>8</id>
  <name>kHW_NVidiaQuadroFX4500Item</name>
  <VRAM>512</VRAM>
  <config_id>695</config_id>
  <config_name>MacPro 2x3GHz Quad-Core NVIDIA Quadro FX 4500</config_name>
 </video_card>
 <video_card>
  <id>27</id>
  <name>NVIDIA GeForce 7600 GT</name>
  <VRAM>256</VRAM>
  <config_id>496</config_id>
  <config_name>iMac 24&quot; 2.33GHz Core 2 Duo (lab)</config_name>
 </video_card>
</video_cards>


Reply By: samjudson Reply Date: 7/2/2008 3:07:37 AM
I think because position() will always return the position in the inner loop you will likely have to use xsl:number as an alternative.

Do the id numbers have to be sequential, as xsl:number will output the id's in the original video_card's order?

/- Sam Judson : Wrox Technical Editor -/
Reply By: dghosh Reply Date: 7/2/2008 3:16:43 AM
quote:
Originally posted by samjudson

I think because position() will always return the position in the inner loop you will likely have to use xsl:number as an alternative.

Do the id numbers have to be sequential, as xsl:number will output the id's in the original video_card's order?

/- Sam Judson : Wrox Technical Editor -/



I do need the config ids to be sequential, in the sense that the order does not matter but if there are 15 distinct configs involved, under various different video cards, I need the final ids for configs to range from cfg_1 to cfg_15.

I don't quite follow the use of xsl:number here. Could you kindly elaborate? Thanks.

Reply By: Volder Reply Date: 7/2/2008 3:20:25 AM
The main problem here could be that the same config_id could be used for different videocards - and the OP probably wants the same cfg_N for such a case.

@dghosh, please, confirm or reject my supposition.

Reply By: Volder Reply Date: 7/2/2008 3:28:23 AM
quote:
I don't quite follow the use of xsl:number here. Could you kindly elaborate?

<input type="checkbox" class="config_list" value="{config_id}" name="cfg[][id]" checked="checked">
  <xsl:attribute name="id">
    <xsl:number/>
  </xsl:attribute>
</input>


Reply By: dghosh Reply Date: 7/2/2008 3:29:16 AM
quote:
Originally posted by Volder

The main problem here could be that the same config_id could be used for different videocards - and the OP probably wants the same cfg_N for such a case.

@dghosh, please, confirm or reject my supposition.





If you meant that there could be multiple cfg_<n> for multiple video cards, then you are correct. That is the problem. I need it to be starting at cfg_1 and ending at cfg_n across all the video cards.

Eg:

For Video Card Group 1: cfg_1, cfg_2, cfg_3
For Video Card Group 2: cfg_1, cfg_2
For Video Card Group 3: cfg_1

Here, note that I have 3 cfg_1(s) and 2 cfg_2(s)

Instead I want the following:

For Video Card Group 1: cfg_1, cfg_2, cfg_3
For Video Card Group 2: cfg_4, cfg_5
For Video Card Group 3: cfg_6

Reply By: Volder Reply Date: 7/2/2008 4:31:39 AM
so is the solution from my last post fine with you?

Reply By: dghosh Reply Date: 7/2/2008 4:34:53 AM
quote:
Originally posted by Volder

quote:
I don't quite follow the use of xsl:number here. Could you kindly elaborate?

<input type="checkbox" class="config_list" value="{config_id}" name="cfg[][id]" checked="checked">
  <xsl:attribute name="id">
    <xsl:number/>
  </xsl:attribute>
</input>






That is exactly what I was looking for. Thank you very much.


Go to topic 71762

Return to index page 1