Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested for-each loops in XSLT not working

I cant seem to get this nested for loop to work correctly, I want to print all of the tracks on the EP in the row with the EP name and other details. Everything from the first for-each loop displays correctly but nothing is pulled through for the for-each loop to get the tracks.

Here is my XML

<dalehoward>

<ep>
    <name>Letters EP</name>
    <year>2012</year>
    <label>Static Audio</label>
    <image>letters.jpg</image>

        <tracks>
        <track number="1">
            <tname>Letters</tname>
            <length>6.35</length>
        </track>
        <track number="2">
            <tname>Later</tname>
            <length>7.56</length>
        </track>
            <track number="3">
            <tname>'89 Flava</tname>
            <length>7:38</length>
        </track>
        <track number="4">
            <tname>Safe Presentation</tname>
            <length>7.55</length>
        </track>
        </tracks>
</ep>



<ep>
    <name>Inner City EP</name>
    <year>2012</year>
    <label>Lost My Dog</label>
    <image>innercity.jpg</image>

    <tracks>
    <track number="1">
            <tname>C'Mon</tname>
            <length>7.15</length>
        </track>
        <track number="2">
            <tname>Koppabird</tname>
            <length>6.27</length>
        </track>
        <track number="3">
            <tname>Inner City</tname>
            <length>8:50</length>
        </track>
        <track number="4">
            <tname>You Can</tname>
            <length>8:16</length>
        </track>
        <tracks>
</ep>
<dalehoward>

and here is the XSLT

<xsl:variable name="imagefolder" select="'xml/images/'" />
<xsl:template match="/">


<html>
<body>
<h2>My CD Collection</h2>
<table border="1" width="100%">
  <tr bgcolor="#9acd32">
    <th style="text-align:left">Title</th>
    <th style="text-align:left">Year</th>
    <th style="text-align:left">Label</th>
    <th style="text-align:left">Tracks</th>
    <th style="text-align:left">Artwork</th>
  </tr>
  <xsl:for-each select="dalehoward/ep">
  <tr>
    <td><xsl:value-of select="name"/></td>
    <td><xsl:value-of select="year"/></td>
    <td><xsl:value-of select="label"/></td>
    <td>testtext<xsl:for-each select="dalehoward/ep/tracks">
        <xsl:value-of select="tname"/><br />
        <xsl:value-of select="length"/> <br /><br />
        </xsl:for-each></td>
    <td><img width="150px" height="150px"><xsl:attribute name="src">
             <xsl:copy-of select="$imagefolder"/>
             <xsl:value-of select="image"/>
             </xsl:attribute></img></td>
  </tr>
  </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Thanks a lot in advance for any help

like image 835
user3614312 Avatar asked May 07 '14 23:05

user3614312


People also ask

How do you write a for loop in XSLT?

You create an XSLT loop with the <xsl:for-each> tag. The value of the select attribute in this tag is an XPath expression that allows you to specify the data element to loop through. XPath expressions are constructed like file paths in an operating system, the forward slash (/) selects subdirectories.

How do you break out of a loop in XSLT?

There is no such a thing as break in an XSLT for-each loop. xsl:if/@test is pretty much all you can do. The other way would be to include this condition within the for-each/@select.

What is current group () in XSLT?

Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*

What is the use of for each in XSLT?

The <xsl:for-each> element allows you to do looping in XSLT.


1 Answers

The outer loop puts you in the context of ep. The context of the inner loop needs to be established from there (or as an absolute path, starting from the root) - so change:

<xsl:for-each select="dalehoward/ep/tracks">

to:

<xsl:for-each select="tracks/track">
like image 194
michael.hor257k Avatar answered Oct 26 '22 23:10

michael.hor257k