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 January 14th, 2006, 09:07 PM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default using multiple for-each

I'm new to XSL but picking it up pretty good. Neat functionality.

I am having a problem using multiple for-each tags. Here's my xml document. What I need to do it loop on each <question> and each <answer>. There may or may not be multiple <question> tags, will always have multiple <answer> tags in the <question>.

Any assistance is appreciated.

<response>
<id>239</id>
<message>Start</message>
<question>
<prompt>'question text goes here'</prompt>
<type>text</type>
<answer>One</answer>
<answer>Two</answer>
<answer>Three</answer>
<answer>Four</answer>
<answer>Five</answer>
</question>
<question>
<prompt>'question text goes here'</prompt>
<type>text</type>
<answer>One</answer>
<answer>Two</answer>
<answer>Three</answer>
<answer>Four</answer>
<answer>Five</answer>
</question>
</response>


 
Old January 14th, 2006, 09:57 PM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I finally figured it out. My problem was more with the xpath than anything. I had '/' after the select in the for-each. That was the cause for the error.

Here is the xsl code if anyone has the same issues.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="response">
<table>
<tr><td>
<xsl:value-of select="query-id"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="response-message"/>
</td>
</tr>
<xsl:for-each select="question">
<tr>
<td>
<xsl:value-of select="prompt"/>
</td>
</tr>
<xsl:for-each select="answer">
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

 
Old January 15th, 2006, 12:04 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's difficult to see what you're doing wrong when you don't say what you're doing. The most common mistake in this area is probably failing to understand that for-each changes the context node, and that within the for-each, relative path expressions are relative to the context node established by the for-each. Code like the following should work:

<xsl:for-each select="question">
  ...
  <xsl:for-each select="answer">

Incidentally, they are elements, not tags. An element usually has two tags, a start tag and an end tag. So if you have two xsl:for-each elements, you have four xsl:for-each tags...



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Datasets from multiple Data Sources SQL_Hacker Reporting Services 1 June 18th, 2008 02:25 PM
Importing Multiple files in Multiple tables Versi Suomi Access 6 June 1st, 2005 08:47 AM
Multiple ADO multiple user login Oracle9i jhay0721 Pro VB Databases 1 April 4th, 2005 11:23 AM
Multiple Joins in Multiple Table Search query pookster Access 4 September 23rd, 2004 03:04 PM
Updating multiple Rows from multiple fields in ASP vdm_nana SQL Server ASP 0 April 1st, 2004 04:26 AM





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