Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 16th, 2006, 05:00 AM
Registered User
 
Join Date: May 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default nested for-each

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>
 
Old May 16th, 2006, 05:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old May 16th, 2006, 06:09 AM
Registered User
 
Join Date: May 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>
 
Old May 16th, 2006, 06:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

(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
 
Old May 16th, 2006, 06:37 AM
Registered User
 
Join Date: May 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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" />
    [list]
    <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>
 
Old May 16th, 2006, 06:42 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old May 16th, 2006, 06:47 AM
Registered User
 
Join Date: May 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great! removed the whole path before 'questionchoice' and it worked! :) Thanks so much for your quick response. I really appreciate it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested query deb1980 ASP.NET 2.0 Basics 2 April 10th, 2007 04:38 AM
nested for hastikeyvan Classic ASP Professional 1 July 27th, 2006 06:28 PM
Nested If Statement KCTin Visual Basic 2005 Basics 3 December 28th, 2005 01:53 PM
Nested If pans Access 4 December 10th, 2004 12:39 AM
Nested Do Until Loop goels Access VBA 2 September 30th, 2004 08:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.