我有一個帶有促銷代碼的 XML,我想在某處顯示。我想顯示我的 XML 的所有三個條目,但是當我為每個函式使用 XSLT 時,我得到第一個條目三次。我想將它們三個都顯示在一個好看的行中。
我使用的 XML:
<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>
我用于顯示條目的 XSLT:
<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>
uj5u.com熱心網友回復:
如果您想為每個創建一行,則為每個promotion創建一個 。并使用當前的相對路徑來獲取行單元格的值。在呼叫創建表行之前還要創建和包裝器。trpromotionpromotionhtmltablexsl:for-each
這是一個簡化的示例:
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>
應用于您的輸入示例,這將回傳:
結果
<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>
渲染為:

uj5u.com熱心網友回復:
在 內部for-each,而不是您使用的絕對路徑,如<xsl:value-of select="/rss/channel/promotions/promotion/image" />,使用相對路徑,如<xsl:value-of select="image"/>.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/418189.html
標籤:
