Reputation: 15
I'm stucked with a xslt for-each-loop.
The xml source file is:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="party.xsl"?>
<party date="31.12.01">
<guest name="Albert">
<drink>wine</drink>
<drink>beer</drink>
<status single="true" sober="false" />
</guest>
<guest name="Martina">
<drink>apple juice</drink>
<status single="true" sober="true" />
</guest>
<guest name="Zacharias">
<drink>wine</drink>
<status single="false" sober="false" />
</guest>
</party>
I'd like to get the following output:
Therefore i wrote following XSLT file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head></head>
<body bgcolor="white">
<xsl:for-each select="party/guest">
<ul><li><b><xsl:value-of select="@name"/></b>
<xsl:text> drinks: </xsl:text>
<xsl:value-of select="drink"/>
<xsl:text>, </xsl:text>
</li></ul>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Using the XSLT file above i get following output:
How do i have to change my XSLT file, that i get all drinks form the guests displayed? Thanks a lot for your support!
Upvotes: 0
Views: 75
Reputation: 338108
You could just nest another level of <xsl:for-each select="drink">
.
I recommend to avoid <xsl:for-each>
, though. Also, don't cram everything into a single do-it-all template.
<xsl:template match="/">
<html>
<head></head>
<body bgcolor="white">
<xsl:apply-templates select="party" />
</body>
</html>
</xsl:template>
<xsl:template match="party">
<ul>
<xsl:apply-templates select="guest" />
</ul>
</xsl:template>
<xsl:template match="guest">
<li>
<xsl:value-of select="concat(@name, ' drinks: ')" />
<xsl:apply-templates select="drink" />
</li>
</xsl:template>
<xsl:template match="drink">
<xsl:value-of select="." />
<xsl:if test="position() < last()">, </xsl:if>
</xsl:template>
Upvotes: 1