如何在XSLT中保留空格

时间:2019-02-18 19:31:04

标签: templates xslt

我有一个XSLT文件,其中有带有一些XSLT标签的HTML代码。我有一个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'>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    </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”,但没有任何帮助。

1 个答案:

答案 0 :(得分:2)

您可以在样式表中的元素上使用xml:space="preserve",但要注意后果。可能会导致空白出现在渲染输出中您不希望出现的位置,例如

<xsl:value-of select="'('"/>
<xsl:value-of select="$word"/>
<xsl:value-of select="')'"/>

当您可能需要( word )时,将渲染为(word)

类似地,写作

<xsl:element name="a">
  <xsl:attribute name="b">xx</xsl:attribute>
</xsl:element>

会导致失败,因为在创建元素的属性节点之前,您正在向该元素写入子节点(包含空格文本)。

使用<xsl:output indent="yes"/>向输出中注入新的空格可能是获取可读HTML输出的更好方法。