每个的 XSLT 不显示来自 XML 的所有条目
XSLT for each does not display all entries from XML
我有一个 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>
在 for-each
中,不要使用您使用的绝对路径,例如 <xsl:value-of select="/rss/channel/promotions/promotion/image" />
,而是使用相对路径,例如 <xsl:value-of select="image"/>
.
如果您想为每个 promotion
创建一行,则只为每个 promotion
创建 一个 tr
。并使用来自当前 promotion
的 relative 路径来获取行单元格的值。在调用 xsl:for-each
之前创建 html
和 table
包装器 以创建 table 行。
这是一个简化的例子:
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>
应用于您的输入示例,这将 return:
结果
<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>
呈现为:
我有一个 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>
在 for-each
中,不要使用您使用的绝对路径,例如 <xsl:value-of select="/rss/channel/promotions/promotion/image" />
,而是使用相对路径,例如 <xsl:value-of select="image"/>
.
如果您想为每个 promotion
创建一行,则只为每个 promotion
创建 一个 tr
。并使用来自当前 promotion
的 relative 路径来获取行单元格的值。在调用 xsl:for-each
之前创建 html
和 table
包装器 以创建 table 行。
这是一个简化的例子:
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>
应用于您的输入示例,这将 return:
结果
<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>
呈现为: