如何在XSLT中保留空格

问题描述 投票:0回答:1

我有一个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”在许多地方和条件,但没有任何帮助。

templates xslt
1个回答
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输出的更好方法。

© www.soinside.com 2019 - 2024. All rights reserved.