Reputation: 595
I have the following structure for a movies xml file
<movies>
<movie>
<genre>Drama</genre>
<genre>Thriller</genre>
</movie>
....
</movies>
Shouldn't this snippet for selecting every value of genre for each movie tag work ?
<xsl:template match="/">
<html>
<body>
<h2>MOVIES</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Genre</th>
</tr>
<xsl:for-each select="movies/movie">
<tr>
<td>
<xsl:for-each select="movies/movie/genre">
<xsl:value-of select="genre"/>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
It shows nothing as ouput. Can anyone please point out the mistake ?
Upvotes: 1
Views: 274
Reputation: 243599
As simple as this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<table><xsl:apply-templates/></table>
</xsl:template>
<xsl:template match="movie">
<tr>
<xsl:apply-templates select="node()|@*"/>
</tr>
</xsl:template>
<xsl:template match="genre"><td><xsl:apply-templates/></td></xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<movies>
<movie>
<genre>Drama</genre>
<genre>Thriller</genre>
</movie>
</movies>
the wanted, correct result is produced:
<table>
<tr>
<td>Drama</td>
<td>Thriller</td>
</tr>
</table>
Upvotes: 1
Reputation: 18092
While sierrasdetandil's answer already gave you the solution for your problem at hand I'd like to provide an alternative using apply-templates
instead of for-each
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<html>
<body>
<h2>MOVIES</h2>
<table>
<tr><th>Genre</th></tr>
<xsl:apply-templates />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="movies">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="movie">
<tr>
<td>
<xsl:apply-templates />
</td>
</tr>
</xsl:template>
<xsl:template match="genre">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
Upvotes: 2
Reputation: 461
It's not working because of the selector in the nested xsl:for-each
: the outer one iterates through movies/movie
, so in the inner one you already are in a movie
element, but you're selecting movies/movie/genre
; that means you're looking for /movies/movie/movies/movie/genre
. The xsl:value-of
uses an incorrect selector as well.
The code should be (untested):
<xsl:for-each select="movies/movie">
<tr>
<td>
<!-- context element here is movie -->
<xsl:for-each select="genre">
<!-- context element here is genre -->
<xsl:value-of select="."/>
<!-- Separate genres with a comma -->
<xsl:if test="not(position() = last())">, </xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
By the way (off-topic), the bgcolor
attribute was deprecated in HTML 4 (a long time ago) :)
You should use style
instead:
<tr style="background-color: #9acd32;">
Upvotes: 2