Subject: nested for-each
Posted By: sassymonix Post Date: 5/16/2006 5:00:17 AM
Hi, I'm very new to XLS so can anybody please help me? Does a nest for-each loop work with XLS? Like the one below? I can't get it to work....
Any help would be greatly appreciated! Thanks!

<table width="100%" border="1">
  <THEAD>
    <TR>
       <TD width="50%"><B>QUESTIONS</B></TD>
    </TR>
  </THEAD>
  <TBODY>
      <xsl:for-each select="program/modules/module/moduleartifact/lesson/lessonartifact/evaluation/objective/Question">
    <TR>    
       <TD width="50%"><xsl:value-of select="@asks" /></TD>     
    </TR>
    <xsl:for-each select="program/modules/module/moduleartifact/lesson/lessonartifact/evaluation/objective/Question/QuestionChoice">
    <TR>    
        <TD width="50%"><xsl:value-of select="@choice" /></TD>     
    </TR>
    </xsl:for-each>
    </xsl:for-each>    
  </TBODY>
</table>
Reply By: mhkay Reply Date: 5/16/2006 5:21:21 AM
Yes, nested for-each loops work. But you need to understand that a for-each instruction changes the context node, and that any path expression within the for-each (including the select expression of a nested for-each) selects relative to the new context node. If that's not the effect you want, use an absolute path expression starting with "/" (the root of the document) or starting with a variable $x that you have bound outside the outer for-each loop.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: sassymonix Reply Date: 5/16/2006 6:09:43 AM
Ok, so if my xml file looks like this...what should I do with my XSL template above so that I can display the question choices below the question?


<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<program id="528" type="1" number="CONV-030106-004" name="Basic Pricing" description="imported for fsl" defaultcorrectresponse="" defaultincorrectresponse="">
<modules>
<module name="FSL Basic Pricing" description="imported for fsl" isaccessrandom="true" isnotscheduled="true" finishinweeks="0" finishindays="0">
<moduleartifact displayorder="1" type="3">
<lesson name="Certification Test" description="imported for fsl" randomaccess="true" managerverification="false" coachingrequired="false">
<lessonartifact displayorder="1" type="4">
<page pageid="602" name="Welcome" url="/programs/P0000528/html/welcome_basic_pricing.htm" />
</lessonartifact>
<lessonartifact displayorder="2" type="4">
<page pageid="603" name="Instructions" url="/programs/P0000528/html/instructions_template.htm" />
</lessonartifact>
<lessonartifact displayorder="0" type="5">
<evaluation evaltype="8" name="Randomized Test" description="imported for fsl" passingMark="85" responsePass="" responseFail="">
<objective name="LO-01" description="" questionCount="26">
<Question qstNumber="CONV-030106-004_LO-01_Q001" asks="FSLD uses the ____________index as an indicator for determining interest rates on Adjustable Rate Mortgage (ARM) products." refAlias="" refLink="" correctResponse="" incorrectResponse="" displayOrder="0" qstType="2">
<QuestionChoice choice="T-Bill" correctAnswer="false" displayOrder="1" />
<QuestionChoice choice="COFI" correctAnswer="false" displayOrder="2" />
<QuestionChoice choice="LIBOR" correctAnswer="true" displayOrder="3" />
<QuestionChoice choice="Prime Rate" correctAnswer="false" displayOrder="4" />
</Question>
<Question qstNumber="CONV-030106-004_LO-01_Q002" asks="What equals a fully indexed interest rate on ARM loans?" refAlias="" refLink="" correctResponse="" incorrectResponse="" displayOrder="0" qstType="2">
<QuestionChoice choice="Margin + Index" correctAnswer="true" displayOrder="1" />
<QuestionChoice choice="Rate + Index" correctAnswer="false" displayOrder="2" />
<QuestionChoice choice="Margin + Rate" correctAnswer="false" displayOrder="3" />
<QuestionChoice choice="Rate + Cap" correctAnswer="false" displayOrder="4" />
</Question>
<Question qstNumber="CONV-030106-004_LO-01_Q003" asks="Where allowed, FSLD offers borrowers two methods of reducing or avoiding prepayment penalties.  What are they?" refAlias="" refLink="" correctResponse="" incorrectResponse="" displayOrder="0" qstType="2">
<QuestionChoice choice="Buy-out" correctAnswer="false" displayOrder="1" />
<QuestionChoice choice="Buy-down" correctAnswer="false" displayOrder="2" />
<QuestionChoice choice="Both of the above" correctAnswer="true" displayOrder="3" />
</Question>
<Question qstNumber="CONV-030106-004_LO-01_Q004" asks="1 point = _____%" refAlias="" refLink="" correctResponse="" incorrectResponse="" displayOrder="0" qstType="2">
<QuestionChoice choice="1" correctAnswer="true" displayOrder="1" />
<QuestionChoice choice=".01" correctAnswer="false" displayOrder="2" />
</Question>
<Question qstNumber="CONV-030106-004_LO-01_Q005" asks="Which of the following is NOT a cost involved in the origination of a loan?" refAlias="" refLink="" correctResponse="" incorrectResponse="" displayOrder="0" qstType="2">
<QuestionChoice choice="Employee Compensation" correctAnswer="false" displayOrder="1" />
<QuestionChoice choice="Supplies" correctAnswer="false" displayOrder="2" />
<QuestionChoice choice="Donuts" correctAnswer="true" displayOrder="3" />
<QuestionChoice choice="Electricity" correctAnswer="false" displayOrder="4" />
</Question>
</objective>
</evaluation>
</lessonartifact>
</lesson>
</moduleartifact>
</module>
</modules>
</program>
Reply By: mhkay Reply Date: 5/16/2006 6:18:50 AM
(a) please tell your teachers that people who respond to XSLT questions are getting bored with this particular student exercise. It's time they thought of something more interesting to set their students.

(b) I don't write code for people (other than paying customers): if you have specific questions or problems (like your original question) then I will try to give an answer. But "please write a stylesheet for me because I can't do it" is out of scope.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: sassymonix Reply Date: 5/16/2006 6:37:07 AM
oh sorry...it wasn't my intention for you to create the whole stylesheet. i thought you just had to edit a line or two in the stylesheet i posted above. here it is again...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head>
<xsl:for-each select="program/modules/module">
<title><xsl:value-of select="@name" /></title>
</xsl:for-each>
</head>
<body>

<xsl:for-each select="program/modules/module/moduleartifact/lesson/lessonartifact/evaluation/objective/Question">
<xsl:value-of select="@asks" />
    <ul>
    <xsl:for-each select="program/modules/module/moduleartifact/lesson/lessonartifact/evaluation/objective/Question/QuestionChoice">
    <li>
        <xsl:value-of select="@choice" />
    </li>
    </xsl:for-each>    
    </ul>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Reply By: mhkay Reply Date: 5/16/2006 6:42:23 AM
I haven't attempted to look at the great swadge of XML you posted, but instinctively, the context node from your outer for-each is a "Question" element, so I would expect the inner for-each to say select="QuestionChoice" - that is, processing each QuestionChoice that's a child of the current Question.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: sassymonix Reply Date: 5/16/2006 6:47:21 AM
Great! removed the whole path before 'questionchoice' and it worked! :) Thanks so much for your quick response. I really appreciate it.

Go to topic 44274

Return to index page 286
Return to index page 285
Return to index page 284
Return to index page 283
Return to index page 282
Return to index page 281
Return to index page 280
Return to index page 279
Return to index page 278
Return to index page 277