Reputation: 81
I have an XML with promotion codes that I want to display somewhere. I want to show all three entries of my XML but when I use a XSLT for each function I get the first entry three times. I want to display all three of them in a good looking row.
The XML I use:
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:s="https://www.zdnet.com/search" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>Promotions</title>
<promotions>
<promotion>
<enddate>20-01-2022</enddate>
<link>https://www.example.com/code/</link>
<title>Good deal!</title>
<description>this is a good deal</description>
<code>Discount10</code>
<image>https://example.com/favicon.ico</image>
</promotion>
<promotion>
<enddate>19-01-2022</enddate>
<link>https://www.example.com/code/</link>
<title>bad deal!</title>
<description>This is a bad deal</description>
<code />
<image>https://example.com/favicon.ico</image>
</promotion>
<promotion>
<enddate>18-01-2022</enddate>
<link>https://www.example.com/code/</link>
<title>Super deal!</title>
<description>This deal is superb.</description>
<code>discount75</code>
<image>https://example.com/favicon.ico</image>
</promotion>
</promotions>
</channel>
</rss>
The XSLT I use for displaying the entries:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
<xsl:output method="html" encoding="UTF-8" />
<xsl:template match="/">
<xsl:for-each select="/rss/channel/promotions/promotion">
<html xmlns="http://www.w3.org/1999/xhtml">
<tbody>
<tr>
<td style="width: 60px; padding: 0px;">
<img alt="shop logo" style="margin-right: 16px; width: 60px; height: 60px; border-radius: 50%">
<xsl:attribute name="src">
<xsl:value-of select="/rss/channel/promotions/promotion/image" />
</xsl:attribute>
</img>
</td>
<td style="vertical-align: baseline">
<div style="padding-right: 16px; font-size: 16px; color: #0A344F; font-weight: bold;">
<xsl:value-of select="/rss/channel/promotions/promotion/title" />
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<span style="padding-right: 16px; font-size: 14px;">Your discount:</span>
<span
class="discount-code"
style="background-color: #0A344F; font-weight: bold; padding: 8px 16px; color: white; text-align: center; border-radius: 8px; font-size: 14px; word-break: keep-all; cursor: text;"
>
<xsl:value-of select="/rss/channel/promotions/promotion/code" />
</span>
</td>
</tr>
<tr style="height: 10px;"></tr>
<tr>
<td></td>
<td style="display: flex;">
<a
style="background-color: #FF0054; color: white; font-size: 14px; font-weight: bold; border-radius: 8px; width: 100%; text-align: center; border: 0px; padding: 8px 16px; text-decoration: none; display: block;"
>
<xsl:attribute name="href">
<xsl:value-of select="/rss/channel/promotions/promotion/link" />
</xsl:attribute>
Shop nu!
</a>
</td>
</tr>
</tbody>
</html>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 202
Reputation: 116959
If you want to have a row for each promotion
, then create only one tr
for each promotion
. And use a relative path from the current promotion
to get the values for the row's cells. Also create the html
and table
wrappers before calling xsl:for-each
to create the table rows.
Here is a simplified example:
XST 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" />
<xsl:template match="/rss">
<html>
<body>
<table border="1">
<xsl:for-each select="channel/promotions/promotion">
<tr>
<td>
<xsl:value-of select="title" />
</td>
<td>
<xsl:value-of select="description" />
</td>
<!-- more fields -->
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, this will return:
Result
<html>
<body>
<table border="1">
<tr>
<td>Good deal!</td>
<td>this is a good deal</td>
</tr>
<tr>
<td>bad deal!</td>
<td>This is a bad deal</td>
</tr>
<tr>
<td>Super deal!</td>
<td>This deal is superb.</td>
</tr>
</table>
</body>
</html>
Rendered as:
Upvotes: 1
Reputation: 167436
Inside of the for-each
, instead of the absolute paths you have used, like <xsl:value-of select="/rss/channel/promotions/promotion/image" />
, use relative ones, like <xsl:value-of select="image"/>
.
Upvotes: 1