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 December 21st, 2006, 06:58 PM
Registered User
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Nested For-Each

I'm relatively new to XSL and am stumped why this loop is not working. Any help would be appreciated:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" indent="yes"
 doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>

 <xsl:template match="/">
  <html>
   <head>
    <title>History in the Making</title>
    <script src="sorttable.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css"></link>
   </head>
   <body BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF">
    <table class="sortable" id="unique_id" cellpadding="0" cellspacing="0" border="0">
    <tbody>
    <xsl:for-each select="pinpoints/pinpoint">
     <tr>
     <td>
     <xsl:if test="@filterType=''">
     None
     </xsl:if>
     <xsl:if test="@filterType!=''">
     <xsl:value-of select="@filterType"/>
     </xsl:if>
     </td>
     <td><xsl:value-of select="startdate"/></td>
     <td><xsl:value-of select="@name"/></td>
     <td><xsl:value-of select="description/background"/></td>
     <td><xsl:value-of select="description/impact"/></td>
     <td>
     <img>
     <xsl:attribute name="src">
      <xsl:value-of select="iimages/iimage"/>
     </xsl:attribute>
     </img>
     </td>
     <td>
     <xsl:if test="urls/url=''">
     None
     </xsl:if>
     <xsl:if test="urls/url!=''">
     <a>
     <xsl:attribute name="href">
     <xsl:value-of select="urls/url"/>
     </xsl:attribute>
     URL
     </a>
     </xsl:if>
     </td>
     <td>
     <xsl:if test="audioclips/audioclip=''">
     None
     </xsl:if>
     <xsl:if test="audioclips/audioclip!=''">
     <a>
     <xsl:attribute name="href">
     <xsl:value-of select="audioclips/audioclip"/>
     </xsl:attribute>
     Audio Clip
     </a>
     </xsl:if>
     </td>
     <td>
     <xsl:if test="videoclips/videoclip=''">
     None
     </xsl:if>
     <xsl:if test="videoclips/videoclip!=''">
     <a>
     <xsl:attribute name="href">
     <xsl:value-of select="videoclips/videoclip"/>
     </xsl:attribute>
     Video Clip
     </a>
     </xsl:if>
     </td>
     <td>
     <xsl:if test="links/link=''">
     <br />
     </xsl:if>
     <xsl:if test="links/link!=''">
     <a>
     <xsl:attribute name="href">
     <xsl:value-of select="links/link"/>
     </xsl:attribute>
     Link
     </a>
     </xsl:if>
     </td>
     </tr>
    </xsl:for-each>
    </tbody>
    </table>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>

The problem that I'm having is with links. The XML source:

 <pinpoint name="1800: Smallpox vaccination in the U.S." icon=""
  filterType="PHM" isKeyEvent="Y">
    <startdate>01/01/1800</startdate>
    <enddate></enddate>
    <description>
      <background>
        <p>The smallpox vaccination was introduced in the United
        States in 1800 by Benjamin Waterhouse. However, legislative
        attempts to make vaccinations compulsory in America did not
        succeed until 1855 and enforcement did not begin until
        1872.</p>
      </background>
      <impact>
        <p>The smallpox vaccine, along with the establishment of
        vaccination laws and improvements in sanitation, water
        quality and hygiene, eventually led to the eradication of
        the deadly smallpox disease and decreased mortality rates
        due to disease.</p>
      </impact>
    </description>
    <indepthfilters>
      <filter></filter>
    </indepthfilters>
    <urls>
      <url></url>
    </urls>
    <bgimages>
      <bgimage>Dr. Benjamin Waterhouse.jpg</bgimage>
    </bgimages>
    <iimages>
      <iimage></iimage>
    </iimages>
    <audioclips>
      <audioclip></audioclip>
    </audioclips>
    <videoclips>
      <videoclip></videoclip>
    </videoclips>
    <links>
      <link>1792: Marine Hospital Service</link>
      <link>1840: Public School Movement</link>
    </links>
  </pinpoint>

has more than one <link>, I need both links to show in the table.

Appreciate any help you can give.
 
Old December 21st, 2006, 07:47 PM
Registered User
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well,

I solved my own problem. The solution had to do with the naming in the for-each loop. Rather than using the full path to the element, I just used .

Worked like a charm.

Thanks for anybody that took the time to have a look.
 
Old December 21st, 2006, 08:17 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't see any nested for-each in your code. However, you need a nested for-each if you are going to output multiple links, something like replacing

<td>
     <xsl:if test="links/link=''">
     <br />
     </xsl:if>
     <xsl:if test="links/link!=''">
     <a>
     <xsl:attribute name="href">
     <xsl:value-of select="links/link"/>
     </xsl:attribute>
     Link
     </a>
     </xsl:if>
     </td>

with

<xsl:for-each select="links/link">
  <a href="{.}">Link</a>
  <xsl:if test="position()!=last()"><br/></xsl:if>
</xsl:for-each>

ditto for videoclips, audioclips, etc.

Generally your code is about five times the length it needs to be.

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
Finding nested elements XSLT 1? Budbertzerofluff XSLT 2 November 15th, 2008 02:45 PM
Nested For-Each Loop In XSLT dghosh XSLT 10 July 2nd, 2008 04:34 AM
Pretty nested printing with XSLT atteeela XSLT 1 August 23rd, 2007 02:07 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM





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