【发布时间】:2016-08-16 22:25:01
【问题描述】:
我正在为使用 XML 和 XSLT 的活动构建 HTML 电子邮件。我几乎得到了我想要的东西,但是我得到了一些重复的内容,我不知道如何消除重复的元素。
我最初忘记添加一个额外的要求:我需要为每个内容元素添加自定义模板,以根据元素应用不同的格式。另外,内容中有随机图片需要建模
这里是一些示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<job>
<surface>
<preheader><preheader_p>Click for more information</preheader_p></preheader>
<preheader><preheader_p>Questions? Call 877-555-1212</preheader_p></preheader>
<preheader><preheader_p>Click to unsubscribe</preheader_p></preheader>
<brand href="Images/logo.jpeg" />
<headline>Headline goes here</headline>
<subhead>Subhead goes here</subhead>
<body_copy>First paragraph goes here</body_copy>
<body_copy>Second paragraph goes here</body_copy>
<chart href="Images/graph.jpeg" />
<body_copy>Third paragraph goes here</body_copy>
</surface>
</job>
使用 XSLT,我需要构建一个表,该表在两列嵌套表的左列中插入预标题内容。在右栏中,我需要插入产品徽标。
显示前页眉和徽标内容后,其余内容将按顺序插入,每个内容都在各自的表格行中。
这是我的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<table width="600" border="1">
<tr><td>
<table width="100%" border="1">
<tr>
<td width="60%">
<table width="100%" border="1">
<xsl:apply-templates select="job/surface/preheader" />
</table>
</td>
<td width="40%"><xsl:apply-templates select="job/surface/brand"/></td>
</tr>
</table>
</td></tr>
<xsl:apply-templates select="job/surface" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="preheader"><tr><td style="font-size:11pt;"><xsl:value-of select="."/></td></tr>
</xsl:template>
<xsl:template match="brand"><img style="max-width:100%" src="{@href}" />
</xsl:template>
<xsl:template match="headline"><tr><td style="font-size:20pt;"><xsl:value-of select="."/></td></tr>
</xsl:template>
<xsl:template match="subhead"><tr><td style="font-size:16pt;"><xsl:value-of select="."/></td></tr>
</xsl:template>
<xsl:template match="body_copy"><tr><td style="font-size:12pt;"><xsl:value-of select="."/></td></tr>
</xsl:stylesheet>
问题在于,preheader 和 logo 元素重复了两次。
目标是创建以下 HTML:
<html>
<body>
<table width="600" border="1">
<tr><td>
<table width="100%" border="1">
<tr><td width="60%"><table width="100%" border="1">
<tr><td>Click for more information</td></tr>
<tr><td>Questions? Call 877-555-1212</td></tr>
<tr><td>Click to unsubscribe</td></tr>
</table></td>
<td width="40%"><img style="max-width:100%" src="Images/logo.jpeg"></td</tr>
</table></td></tr>
<tr><td style="font-size:20pt;">Headline goes here</td></tr>
<tr><td style="font-size:16pt;">Subhead goes here</td></tr>
<tr><td style="font-size:12pt;">First paragraph goes here</td></tr>
<tr><td style="font-size:12pt;">Second paragraph goes here</td></tr>
<tr><td><img style="max-width:100%" src="Images/graph.jpeg" /></td></tr>
<tr><td style="font-size:12pt;">Third paragraph goes here</td></tr>
</table>
</body>
</html>
【问题讨论】: