[英]How can I include HTML sections in XSL template?
我有一個XSL模板,它是HTML電子郵件的一小部分。 XSL部分基於數據而更改,但周圍的HTML不會。 我想在XSL模板中包含此HTML。 我嘗試了以下方法,但因為EmailHeader.html中的標簽未關閉而獲得異常。 我嘗試使用CDATA標簽,但沒有成功。
我的例外:發生了意外的文件結束。 以下元素未關閉:td,tr,table,td,tr,table,body,html。
如何在XSL模板中包含html部分?
(代碼示例已簡化)
我的XSL文件:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:template match="/Email">
<xsl:copy-of select="document('EmailHeader.html')"></xsl:copy-of>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width:25px"> </td>
<td style="width:550px; text-align:left; font-family:Arial; font-size:10pt; color:#444444">
<br /><br />
To <xsl:value-of select="FirstName"/> <xsl:value-of select="LastName"/>:
<br /><br />
Welcome, and thank you for enrolling...
</td>
<td style="width:25px"> </td>
</tr>
</table>
<xsl:copy-of select="document('EmailFooter.html')"></xsl:copy-of>
</xsl:template>
</xsl:transform>
EmailHeader.html:
<html xmlns="http://www.w3.org/1999/xhtml" style="background-color:#96A084">
<head>
<title></title>
</head>
<body style="background-color:#96A084; margin:0px; padding:0px">
<table border="0" cellpadding="0" cellspacing="0" width="630px" style="background-color:#96A084">
<tr>
<td>
EmailFooter.html:
<td style="width:15px"> </td>
</tr>
</table>
</body>
</html>
您可以做的是在單獨的文件中創建完整的電子郵件模板,然后將電子郵件的正文作為參數傳遞給它。 例如,將以下XSLT保存在名為EmailTemplate.xslt的文件中
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template name="Email">
<xsl:param name="body"/>
<html xmlns="http://www.w3.org/1999/xhtml" style="background-color:#96A084">
<head>
<title/>
</head>
<body style="background-color:#96A084; margin:0px; padding:0px">
<table border="0" cellpadding="0" cellspacing="0" width="630px" style="background-color:#96A084">
<tr>
<td>
<xsl:copy-of select="$body"/>
</td>
<td style="width:15px"> </td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:transform>
接下來,修改原始XSLT以導入此文件,然后使用電子郵件正文的HTML作為參數調用命名的電子郵件模板
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:include href="EmailTemplate.xslt"/>
<xsl:output method="html"/>
<xsl:template match="/Email">
<xsl:variable name="body">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width:25px"> </td>
<td style="width:550px; text-align:left; font-family:Arial; font-size:10pt; color:#444444">
<br/>
<br/> To
<xsl:value-of select="FirstName"/>
<xsl:value-of select="LastName"/>:
<br/>
<br/>Welcome, and thank you for enrolling...
</td>
<td style="width:25px"> </td>
</tr>
</table>
</xsl:variable>
<xsl:call-template name="Email">
<xsl:with-param name="body" select="$body"/>
</xsl:call-template>
</xsl:template>
</xsl:transform>
因此,將此XSLT應用於以下XML時
<Email>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Email>
應輸出以下內容
<html style="background-color:#96A084" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body style="background-color:#96A084; margin:0px; padding:0px">
<table border="0" cellpadding="0" cellspacing="0" width="630px" style="background-color:#96A084">
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" xmlns=""><tr><td style="width:25px"> </td><td style="width:550px; text-align:left; font-family:Arial; font-size:10pt; color:#444444"><br><br> To
John
Smith:
<br><br>Welcome, and thank you for enrolling...
</td><td style="width:25px"> </td></tr></table>
</td>
<td style="width:15px"> </td>
</tr>
</table>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.