如何保持白色 space 为

How to keep white space as

我有 xml 元素如下。

<requestBody type="String"><![CDATA[Today, the XXXXXXXX XXXXXXXXXX XXXX (#XXXXXXX) Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Thanks,

XXXXXXXXXX
Test Administrator

XXXXXXXX XXXXXXXXXX XXXX
#### Address
CITY, ST  12345
Direct:  123-123-1234
Main Office:   123-123-1234
Fax:   123-123-1234
E-mail:   XXXXXXXXXX



Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book, please visit www.WEBADDRESS.com<http://www.WEBADDRESS.com>.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.]]>
</requestBody>

当我应用 <xsl:value-of select="/requestBody"/> 时,它会删除此元素中的所有空格并将连接的字符串打印为:

Today, the XXXXXXXX XXXXXXXXXX XXXX (#XXXXXXX) Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Thanks, XXXXXXXXXX Test Administrator XXXXXXXX XXXXXXXXXX XXXX #### Address CITY, ST 12345 Direct: 123-123-1234 Main Office: 123-123-1234 Fax: 123-123-1234 E-mail: XXXXXXXXXX Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book, please visit www.WEBADDRESS.com<http://www.WEBADDRESS.com>. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

有人可以告诉我如何在不删除空格的情况下按原样获取元素内容吗?

提前致谢。

@Harishfysx,正如 John Bollinger 在评论中指出的那样,不清楚您是在描述实际的文件输出,还是在浏览器中呈现——因为您包含了 xhtml 标签,我们(读者)必须假定您可能在谈论浏览器 on-screen 渲染,它确实在某些重要方面忽略了 whitespace。

如果您谈论文件输出,那么您的代码或您的 XSL 处理器正在做一些有趣的事情。如果我应用以下模板:

<xsl:template match="requestBody">
    <xsl:value-of select="."/>
</xsl:template>

...我得到以下输出:

<?xml version="1.0" encoding="UTF-8"?>Today, the XXXXXXXX XXXXXXXXXX XXXX (#XXXXXXX) Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Thanks,

XXXXXXXXXX
Test Administrator

XXXXXXXX XXXXXXXXXX XXXX
#### Address
CITY, ST  12345
Direct:  123-123-1234
Main Office:   123-123-1234
Fax:   123-123-1234
E-mail:   XXXXXXXXXX



Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book, please visit www.WEBADDRESS.com&lt;http://www.WEBADDRESS.com&gt;.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

你会注意到所有的白色space都被保留了下来。

您还会注意到有讨厌的 XML header、<?xml version="1.0" encoding="UTF-8"?> 和 tag-ish link 标记 <http://www.WEBADDRESS.com>在您的源中以转义格式输出为 &lt;http://www.WEBADDRESS.com&gt;。这两个都可以通过在模板前添加以下内容来解决:

<xsl:output method="text"/>

由于我可以很容易地生成保留白色space 的输出,我必须假设您的问题是 而不是 XSL,而是您的浏览器正在显示这个默认浏览器模式下的文本,将 each 和 every 白色 space 字符串(space 的多个实例、制表符、换行符)折叠成单个 space 字符.

正如 John 所建议的,使用 <pre> 元素,或者使用定义 whitespace:pre; 样式的 CSS,如果你想要严格的布局,其中行 运行屏幕右端,或 whitespace:pre-wrap;,如果你想保留白色space 但浏览器换行太长而无法显示在页边距内。