Iterating through nodes and displaying counter variable

admin

Administrator
Staff member
Hey all,

I have an .xml document that lists information about each of my DVDs in a collection. Here's the basic format of that .xml document:


<dvds>
<dvd>
<title></title>
<format></format>
<release_year></release_year>
<runtime></runtime>
<genre></genre> *may have more than one
<rating></rating>
<cover></cover> *cover art of movie case
</dvd>
</dvds>


I also have an .xsl document that parses my .xml and creates a HTML table that displays the .xml information. Here's the format of that:


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h2>My Library of DVDs</h2>
<table>
<tr>
<th>Number</th>
<th>Cover</th>
<th>Title</th>
<th>Genre</th>
<th>Rating</th>
<th>Runtime (minutes)</th>
<th>Release Year</th>
<th>Format</th>
</tr>
<xsl:for-each select="dvds/dvd">
<xsl:sort select="title" />
<tr>
<td>Number in iteration to go here!</td>
<td><xsl:copy-of select="cover"/></td>
<td><xsl:value-of select="title"/></td>
<td>
<xsl:choose>
<xsl:when test="count(genre) = 1">
<xsl:value-of select="genre"/>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="genre">
<xsl:value-of select="."/>
<br />
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</td>
<td><xsl:value-of select="rating"/></td>
<td><xsl:value-of select="runtime"/></td>
<td><xsl:value-of select="release_year"/></td>
<td><xsl:value-of select="format"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


How do I..
Loop through each DVD node in the .xml file
count each iteration
display that number which signifies each iteration in the loop


I'm pretty sure that it has something to do with an <xsl:for-each select="dvds/dvd"> and a counter variable, but I just can't seem to get it right. Is this something that can be accomplished with XSLT or is it better handled via JavaScript?

Please help and TIA!
 
Back
Top