Templates 如何在XSLT中保留空白
我有一个XSLT文件,其中包含带有一些XSLT标记的HTML代码。我有一个Templates 如何在XSLT中保留空白,templates,xslt,Templates,Xslt,我有一个XSLT文件,其中包含带有一些XSLT标记的HTML代码。我有一个xsl:template。在调用xsl:apply-templates之前,一切都很好。之后,结果文件中的html标记之间没有空格。它们都是连接在一起的 如何强制XSLT保持HTML代码的格式,在HTML标记之间包含制表符和换行符 xsl:preserve space用于保存XML数据文件中的空间。我需要在HTML(即XSLT样式表)中保留空间。我尝试了xsl:call-template,但同样的事情发生了 =======
xsl:template
。在调用xsl:apply-templates
之前,一切都很好。之后,结果文件中的html标记之间没有空格。它们都是连接在一起的
如何强制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>
]>
!DOCTYPE html PUBLIC“-//W3C//DTD XHTML1.0//EN”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
96
试验
****H E A D E R****
*******C O N T E N T****
*****F O O T E R*****
]]>
[图片:英雄旗帜]
特色交易
]]>
******D E A L*******
填充:18px 14px 0px 14px;
填充:12px 14px 0px 14px;
[标题]
]]>
和源文件,我的数据文件是:
<?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>
$115+
节省70%+高达400美元
https://www.yahoo.com/
交易>
$115+
节省70%+高达400美元
https://www.yahoo.com/
$115+
节省70%+高达400美元
https://www.yahoo.com/
$115+
节省70%+高达400美元
https://www.yahoo.com/
$115+
节省70%+高达400美元
https://www.yahoo.com/
他们的要求是每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”,但都没有效果。您可以在样式表中的元素上使用
xml:space=“preserve”
,但要小心后果。例如,它可能会导致空白出现在渲染输出中不需要的位置
<xsl:value-of select="'('"/>
<xsl:value-of select="$word"/>
<xsl:value-of select="')'"/>
将导致失败,因为在创建元素的属性节点之前,正在将子节点(包含空格文本)写入元素
使用
向输出中注入新空格可能是获得可读HTML输出的更好方法。您可以在样式表中的元素上使用xml:space=“preserve”
,但要注意后果。例如,它可能会导致空白出现在渲染输出中不需要的位置
<xsl:value-of select="'('"/>
<xsl:value-of select="$word"/>
<xsl:value-of select="')'"/>
将导致失败,因为在创建元素的属性节点之前,正在将子节点(包含空格文本)写入元素
使用
向输出中注入新的空白可能是获得可读HTML输出的更好方法。请提供一个您是否尝试过
?是的,我的代码中有。这就像调用xsl模板break-identitation一样,这里有xml:space=