【发布时间】:2019-02-18 19:31:04
【问题描述】:
我有一个 XSLT 文件,其中有带有一些 XSLT 标记的 HTML 代码。我有一个xsl:template。一切都很好,直到我打电话给xsl:apply-templates。之后,结果文件中的 html 标记之间有 no 空格。它们都是串联的。
如何强制 XSLT 保持我的 HTML 代码格式,在 HTML 标记之间包含制表符和换行符?
xsl:preserve-space 用于从 XML 数据文件中保留空间。我需要在 HTML 中保留空间,即 XSLT 样式表。我试过xsl:call-template,但同样的事情发生了。
======================================
因此,我删除了很多不需要说明我遇到的问题的代码。这是 .xslt 代码:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
]>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
</xsl:text>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<xsl:comment><![CDATA[[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
<o:PixelsPerInch>96</o:PixelsPerInch>
</o:OfficeDocumentSettings>
</xml><![endif]]]></xsl:comment>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0 " />
<meta name="format-detection" content="telephone=no"/>
<xsl:comment><![CDATA[[if !mso]><!]]></xsl:comment>
<xsl:comment><![CDATA[<![endif]]]></xsl:comment>
</head>
<body class="em_body" style="margin:0px auto; padding:0px;" bgcolor="#4070d0">
<xsl:comment>**** H E A D E R ***** </xsl:comment>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="em_full_wrap" bgcolor="#4070d0">
<tr>
<td align="center" valign="top">
<table align="center" width="600" border="0" cellspacing="0" cellpadding="0" class="em_main_table" style="width:600px; table-layout:fixed;">
<tr>
<td valign="top" align="center" style="padding:15px 6px 14px 10px;" class="em_ptrbl"><table width="100%" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<xsl:comment><![CDATA[[if gte mso 9]></td><td valign="top"><![endif]]]></xsl:comment>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<xsl:comment>******* C O N T E N T **** </xsl:comment>
<xsl:for-each select="New/Deals/Deal">
<xsl:apply-templates select=".">
<xsl:with-param name="levelCount" select="position()"/>
<xsl:with-param name="lastCount" select="last()" />
</xsl:apply-templates>
</xsl:for-each>
<xsl:comment>***** F O O T E R *****</xsl:comment>
</body>
</html>
</xsl:template>
<xsl:template match="Deal" name="dealTemplate">
<xsl:param name="levelCount"/>
<xsl:param name="lastCount"/>
<xsl:if test="($levelCount - 1) mod 4 = 0">
<xsl:text disable-output-escaping="yes"><![CDATA[<table width="100%" border="0" cellspacing="0" cellpadding="0" class="em_full_wrap" bgcolor="#4070d0">
<tr>
<td align="center" valign="top">
<table align="center" width="600" border="0" cellspacing="0" cellpadding="0" class="em_main_table" style="width:600px; table-layout:fixed;">]]>
</xsl:text>
</xsl:if>
<xsl:if test="$levelCount=1">
<xsl:comment> [IMAGE: HERO BANNER] </xsl:comment>
<xsl:text disable-output-escaping="yes"><![CDATA[
<tr>
<td valign="top" align="center" class="em_aside5">
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" align="left" bgcolor="#254c9a" style="padding:12px 10px; border-radius: 5px 5px 0 0; font-family:Arial, sans-serif; font-size:20px; line-height:22px; font-weight:bold; color:#ffffff;">Featured deals</td>
</tr>
</table>
</td>
</tr>]]></xsl:text>
</xsl:if>
<xsl:comment>
****** D E A L *******</xsl:comment>
<tr>
<td valign="top" align="center" bgcolor="#ffffff" class="em_plrt">
<xsl:attribute name="style">
<xsl:choose>
<xsl:when test="$levelCount=0">padding:18px 14px 0px 14px;</xsl:when>
<xsl:otherwise>padding:12px 14px 0px 14px;</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" align="center">
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">
<xsl:comment> [HEADLINE] </xsl:comment>
<tr>
<td valign="top" align="left" class="em_blue" style="font-family:Arial, sans-serif; color: #3158aa; font-size:18px; line-height:22px; font-weight:bold; padding-bottom:12px;">
<a style="color: #3158aa; text-decoration: none;" target="_blank">
<xsl:attribute name="href">
<xsl:value-of select="Url"/>
</xsl:attribute>
<xsl:value-of select="Price"/>
<xsl:value-of select="Headline"/>
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<xsl:if test="($levelCount - 1) mod 4 = 3 or $levelCount = $lastCount">
<xsl:text disable-output-escaping="yes"><![CDATA[</table>
</td>
</tr>
</table>]]>
</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
还有源文件,我的数据文件是:
<?xml version="1.0" encoding="utf-8"?>
<New>
<Deals>
<Deal>
<Price>$115+</Price>
<Headline>Save 70% + Up to $400</Headline>
<Url>https://www.yahoo.com/</Url>
</Deal>
Deal>
<Price>$115+</Price>
<Headline>Save 70% + Up to $400</Headline>
<Url>https://www.yahoo.com/</Url>
</Deal>
<Deal>
<Price>$115+</Price>
<Headline>Save 70% + Up to $400</Headline>
<Url>https://www.yahoo.com/</Url>
</Deal>
<Deal>
<Price>$115+</Price>
<Headline>Save 70% + Up to $400</Headline>
<Url>https://www.yahoo.com/</Url>
</Deal>
<Deal>
<Price>$115+</Price>
<Headline>Save 70% + Up to $400</Headline>
<Url>https://www.yahoo.com/</Url>
</Deal>
</Deals>
</New>
他们的要求是每 4 笔交易都有自己的桌子,所以 xsl 中的这个奇怪的数学:if 结果 html 是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
<o:PixelsPerInch>96</o:PixelsPerInch>
</o:OfficeDocumentSettings>
</xml><![endif]-->
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0 " />
<meta name="format-detection" content="telephone=no" />
<!--[if !mso]><!-->
<!--<![endif]-->
</head>
<body class="em_body" style="margin:0px auto; padding:0px;" bgcolor="#4070d0">
<!--**** H E A D E R ***** -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="em_full_wrap" bgcolor="#4070d0">
<tr>
<td align="center" valign="top">
<table align="center" width="600" border="0" cellspacing="0" cellpadding="0" class="em_main_table" style="width:600px; table-layout:fixed;">
<tr>
<td valign="top" align="center" style="padding:15px 6px 14px 10px;" class="em_ptrbl">
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<!--[if gte mso 9]></td><td valign="top"><![endif]-->
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!--******* C O N T E N T **** --><table width="100%" border="0" cellspacing="0" cellpadding="0" class="em_full_wrap" bgcolor="#4070d0">
<tr>
<td align="center" valign="top">
<table align="center" width="600" border="0" cellspacing="0" cellpadding="0" class="em_main_table" style="width:600px; table-layout:fixed;">
<!-- [IMAGE: HERO BANNER] -->
<tr>
<td valign="top" align="center" class="em_aside5">
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" align="left" bgcolor="#254c9a" style="padding:12px 10px; border-radius: 5px 5px 0 0; font-family:Arial, sans-serif; font-size:20px; line-height:22px; font-weight:bold; color:#ffffff;">Featured deals</td>
</tr>
</table>
</td>
</tr><!--
****** D E A L *******--><tr><td valign="top" align="center" bgcolor="#ffffff" class="em_plrt" style="padding:12px 14px 0px 14px;"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="center"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><!-- [HEADLINE] --><tr><td valign="top" align="left" class="em_blue" style="font-family:Arial, sans-serif; color: #3158aa; font-size:18px; line-height:22px; font-weight:bold; padding-bottom:12px;"><a style="color: #3158aa; text-decoration: none;" target="_blank" href="https://www.yahoo.com/">$115+Save 70% + Up to $400</a></td></tr></table></td></tr></table></td></tr><!--
****** D E A L *******--><tr><td valign="top" align="center" bgcolor="#ffffff" class="em_plrt" style="padding:12px 14px 0px 14px;"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="center"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><!-- [HEADLINE] --><tr><td valign="top" align="left" class="em_blue" style="font-family:Arial, sans-serif; color: #3158aa; font-size:18px; line-height:22px; font-weight:bold; padding-bottom:12px;"><a style="color: #3158aa; text-decoration: none;" target="_blank" href="https://www.yahoo.com/">$115+Save 70% + Up to $400</a></td></tr></table></td></tr></table></td></tr><!--
****** D E A L *******--><tr><td valign="top" align="center" bgcolor="#ffffff" class="em_plrt" style="padding:12px 14px 0px 14px;"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="center"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><!-- [HEADLINE] --><tr><td valign="top" align="left" class="em_blue" style="font-family:Arial, sans-serif; color: #3158aa; font-size:18px; line-height:22px; font-weight:bold; padding-bottom:12px;"><a style="color: #3158aa; text-decoration: none;" target="_blank" href="https://www.yahoo.com/">$115+Save 70% + Up to $400</a></td></tr></table></td></tr></table></td></tr><!--
****** D E A L *******--><tr><td valign="top" align="center" bgcolor="#ffffff" class="em_plrt" style="padding:12px 14px 0px 14px;"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="center"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><!-- [HEADLINE] --><tr><td valign="top" align="left" class="em_blue" style="font-family:Arial, sans-serif; color: #3158aa; font-size:18px; line-height:22px; font-weight:bold; padding-bottom:12px;"><a style="color: #3158aa; text-decoration: none;" target="_blank" href="https://www.yahoo.com/">$115+Save 70% + Up to $400</a></td></tr></table></td></tr></table></td></tr></table>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="em_full_wrap" bgcolor="#4070d0">
<tr>
<td align="center" valign="top">
<table align="center" width="600" border="0" cellspacing="0" cellpadding="0" class="em_main_table" style="width:600px; table-layout:fixed;">
<!--
****** D E A L *******--><tr><td valign="top" align="center" bgcolor="#ffffff" class="em_plrt" style="padding:12px 14px 0px 14px;"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="center"><table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><!-- [HEADLINE] --><tr><td valign="top" align="left" class="em_blue" style="font-family:Arial, sans-serif; color: #3158aa; font-size:18px; line-height:22px; font-weight:bold; padding-bottom:12px;"><a style="color: #3158aa; text-decoration: none;" target="_blank" href="https://www.yahoo.com/">$115+Save 70% + Up to $400</a></td></tr></table></td></tr></table></td></tr></table>
</td>
</tr>
</table>
<!--***** F O O T E R *****--></body>
</html>
所以,我的问题在于这些连接的 html 元素(它们之间没有空格,因为它是在 .xslt 文件中确定的),从 **** Deal *** 开始。 HTML 在 xsl:template 之外的格式很好,即在我调用 apply-templates 之前。我在许多地方和条件下尝试使用 xml:space="preserve",但没有任何帮助。
【问题讨论】:
-
你试过
<xsl:output method="html" indent="yes">吗? -
是的,我的代码中有这个。这就像调用 xsl 模板中断标识
-
好吧,XML 中有
xml:space="preserve",而 XSLT 是 XML,所以也许这会有所帮助:w3.org/TR/REC-xml/#sec-white-space 和 w3.org/TR/xslt-30/#stylesheet-stripping 提供更多详细信息。但是发布关于 XML、XSLT、你得到的输出和你想要的输出的最小但完整的细节。