[Very simple newbiequestion] Selecting childnodes with foreach and apply templat

I have an XML which looks like this:

<dvdcollection>
<dvd id="1">
<title>Text</title>
<releasedate>Text</releasedate>
<runningtime>Text</runningtime>
<rating>Text</rating>
<actors>
<actor>Text</actor>
<actor>Text</actor>
<actor>Text</actor>
</actors>
</dvd>
</dvdcollection>

I'm presenting the data in a simple html-table with XSLT and want to print all the actors in one <td> with <br /> after each i use this XSLT:

<xsl:template match="actors">
<xsl:for-each select=".">
<xsl:value-of select="actor"/><br />
</xsl:for-each>
</xsl:template>

and in a for-each loop I use <td><xsl:apply-templates select="actors"/></td> as the tutorial on apply-templates on w3schools shows. I can display the title and other elements by just using <xsl:value-of select="title"/> but with this code I only get the first <actor> element in the tree. :(

What am I missing?

Ive tried having two foreach loops inside eachother but that didn't work and I've heard apply-templates is better in many ways. Please help.
[1361 byte] By [LiquidShadows] at [2007-11-19 3:50:01]
# 1 Re: [Very simple newbiequestion] Selecting childnodes with foreach and apply templat
<xsl:for-each select=".">

"." selects the current node, so you are running a for-each on the set of nodes that contains only the current node. But the current node is just one node, and it's already the current node, so the for-each does exactly nothing and might as well be removed.

<xsl:value-of select="actor"/>

At the point you have this line actors is the current node, so "actor" selects all three actor elements. But value-of is a only accepts a single node, so it just uses the first of the three selected nodes.

Better would be to replace the for-each with an apply-templates on the actor elements

<xsl:apply-templates select="actor"/>

And make a template for actor
<xsl:template match="actor">
<xsl:value-of select="text()"/>
<br/>
</xsl:template>
khp at 2007-11-10 3:27:45 >
# 2 Re: [Very simple newbiequestion] Selecting childnodes with foreach and apply templat
Thanks for clearing that up. My finished xsl looks like this and works like a charm.

<?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 style="font-family: Arial; font-size: 0.9em">

<h2>Andreas DVD-samling</h2>

<table border="0" cellspacing="0" cellpadding="3">

<tr style="background: black; color: white; text-align: left;">
<th>Titel</th>
<th>Utgivningsr</th>
<th>Speltid</th>
<th>Betyg</th>
<th>Skdespelare</th>
<th>Omslag</th>
</tr>

<xsl:for-each select="dvdcollection/dvd">
<xsl:sort select="runningtime" data-type="number" order="descending" />
<xsl:choose>
<xsl:when test="position() mod 2 = 1">
<tr style="background: white; color: black;">
</xsl:when>
<xsl:otherwise>
<tr style="background: gray; color: white;">
</xsl:otherwise>
</xsl:choose>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="releasedate"/><xsl:if test="releasedate > 2000">**</xsl:if></td>
<td><xsl:value-of select="runningtime"/></td>
<td><xsl:value-of select="rating"/><xsl:if test="rating = 5">*</xsl:if></td>
<td><xsl:for-each select="actors"><xsl:apply-templates select="actor"/></xsl:for-each></td>
<xsl:variable name="pos" select="concat(@id, '.jpg')" />
<td><img src="images/{$pos}" /></td>
</tr>
</xsl:for-each>

<tr style="background: black; color: white; text-align: right;">
<th colspan="6">Snittbetyg <xsl:value-of select="sum(dvdcollection/dvd/rating) div count(dvdcollection/dvd/rating)"/></th>
</tr>

</table>

<p>Det finns <xsl:value-of select="count(dvdcollection/dvd)"/> filmer i listan</p>
<p>* Hgsta betyg ** Slppt efter 2000</p>

</body>
</html>
</xsl:template>

<xsl:template match="actor">
<xsl:value-of select="text()"/><br/>
</xsl:template>

</xsl:stylesheet>
LiquidShadows at 2007-11-10 3:28:43 >
# 3 Re: [Very simple newbiequestion] Selecting childnodes with foreach and apply templat
Hi ..

I have the following problem regarding childnodes and xsl:for-each select

My XML looks like this :

<DATA>
<CASE>
<INSTRUCTION Type="Deleted">
<OPERATION>Blah blah</OPERATION>
<FEEDBACK>feedback1</FEEDBACK>
<FEEDBACK>feedback2</FEEDBACK>
<FEEDBACK>feedback3</FEEDBACK>
<FEEDBACK>feedback4</FEEDBACK>
<FEEDBACK>feedback5</FEEDBACK>
<TIME>time blah bah</TIME>
</INSTRUCTION>
<INSTRUCTION Type="Stable">
<OPERATION>Blah blah</OPERATION>
<FEEDBACK>feedback1</FEEDBACK>
<FEEDBACK>feedback2</FEEDBACK>
<FEEDBACK>feedback3</FEEDBACK>
<FEEDBACK>feedback4</FEEDBACK>
<FEEDBACK>feedback5</FEEDBACK>
<TIME>time blah bah</TIME>
</INSTRUCTION>
<INSTRUCTION Type="Modified">
<OPERATION>Blah blah</OPERATION>
<FEEDBACK>feedback1</FEEDBACK>
<FEEDBACK>feedback2</FEEDBACK>
<FEEDBACK>feedback3</FEEDBACK>
<FEEDBACK>feedback4</FEEDBACK>
<FEEDBACK>feedback5</FEEDBACK>
<TIME>time blah bah</TIME>
</INSTRUCTION>
</CASE>
</DATA>

And I basically want to use XSLT to represent this data in X-HTML format.

<xsl:for-each select="Instruction[@Type !='Stable']">
<xsl:choose>
<xsl:when test="@Type = 'Deleted'">
<tr>
<td class="delete_l">
Instruction <xsl:value-of select="@Type"/>
</td>
<td class="delete_r">
<br/>
</td>
</tr>
<tr>
<td class="delete_l">
<br/>
</td>
<td class="delete_r">
<xsl:for-each select=".">
<xsl:value-of select="."/>
</xsl:for-each>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr>
<td>
Instruction <xsl:value-of select="@Type"/>
</td>
<td>
<xsl:value-of select="@Type"/>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>

The output is the nodes Operation+Feedback+Time in a cell, which i really do not understand.
I actually want the results to come out one by one, and each node in one cell..
Or at least being able to put a line break between them :(

Can someone help me along with this please ??
jackoBS at 2007-11-10 3:29:42 >
# 4 Re: [Very simple newbiequestion] Selecting childnodes with foreach and apply templat
Sorry for taking up your time. I just found out that I could use a couple of templates in series :(

I'm kindly asking the admin to remove my post please ...

Thanks a lot and sorry again ...
jackoBS at 2007-11-10 3:30:50 >