简体   繁体   English

如何使用XSLT1.0在XML中显示不可见的换行符

[英]How to display invisible linebreaks in XML using XSLT1.0

This is what is displayed in my XML output (note the line breaks) 这就是我的XML输出中显示的内容(请注意换行符)

<root>
<Item type="Comment">
<comment>this is a test 
this is a test 
this is a test</comment>
</Item>
</root>

In my stylesheet 在我的样式表中

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:user-scripts">
<xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" cdata-section-elements="script msxsl:script"/>


<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="/Root">
    <xsl:apply-templates select="Item"/>
</xsl:template>
<xsl:template match="Item[@type='Comment']"> 

    <html>
        <head>
            <title>
                Comment: <xsl:value-of select="comment_no"/>
            </title>
        </head>
        <style>
            * {
            font-family: "Arial", sans-serif;
            font-size: 10pt;
            }
            body {
            min-width: 775px;
            max-width: 775px;
            margin-left: auto;
            margin-right: auto;
            }
            td {
            padding: 0px;
            }
            .layoutTable {
            width: 100%;
            }
            .fieldValue {
            width: 99%;
            padding-left: 7px;
            }
            .layoutTable {
            width: 100%;
            }
            div.layoutBin {
            page-break-inside: avoid;
            overflow: hidden;
            border: solid;
            border-width: 1px;
            margin-bottom: 10px;
            padding-left: 3px;
            padding-right: 3px;
            }
        </style>

        <body>
            <div class="layoutBin" id="keyInfo">
                <div class="left" style="width: 25%">
                    <table class="layoutTable">
                        <tr>
                            <td class="fieldValue">
                                <xsl:value-of select="comment"/>
                            </td>
                        </tr>
                    </table>
                </div>    
            </div>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

The line breaks are removed and my browser displays the result in a single line instead. 换行符被删除,我的浏览器将结果显示在一行中。

How do I preserve/display the line breaks? 如何保存/显示换行符?

In HTML, line breaks are not normally significant. 在HTML中,换行符通常不重要。 They're treated the same way as other white space - turned into a single word space. 它们与其他空白的处理方式相同-变成单个单词空间。 As such, you can't tell from looking at the page whether there's a literal line break in the DOM. 这样,您无法通过查看页面来判断DOM中是否存在文字换行符。

You have several options: 您有几种选择:

  1. Use CSS' white-space: pre; 使用CSS的white-space: pre; or white-space: pre-wrap; white-space: pre-wrap; . (this is the easiest, given you're already writing your own CSS). (这是最简单的,因为您已经在编写自己的CSS)。 Beware: This will also make all the other text you're inserting significant, so you might want to use xsl:text tags. 注意:这还将使您要插入的所有其他文本都有意义,因此您可能要使用xsl:text标记。
  2. Process text nodes to insert a (self-closing) HTML br tag in place of line-break characters. 处理文本节点以插入(自动关闭)HTML br标签代替换行符。
  3. Process text nodes to split them into HTML div s (or paragraphs or similar) 处理文本节点以将其拆分为HTML div (或段落或类似文本)

Which you choose depends on what you want to do with the text. 选择哪种取决于您要对文本执行的操作。

The xsl:preserve-space element might initially appear to be what you're looking for. xsl:preserve-space元素最初可能看起来就是您想要的。 Put it at the top of your stylesheet with the space-separated list of elements whose whitespace you want preserved verbatim - however it's only necessary if you used xsl:strip-space , which it looks like you haven't. 将其放在要用逐个空格保留其空格的元素列表之间,并以空格分隔,将其放在样式表的顶部-但是只有在使用xsl:strip-space才有必要,看起来好像没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM