如何保持白色空间

时间:2017-07-06 15:16:23

标签: xml xslt printing xhtml xslt-1.0

我有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.

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

提前谢谢。

1 个答案:

答案 0 :(得分:0)

@Harishfysx,正如John Bollinger在评论中指出的那样,目前还不清楚你是在描述实际的文件输出,还是在浏览器中进行渲染 - 因为你包含xhtml标签,我们(读者)必须假设您可能正在谈论浏览器屏幕渲染,这在某些重要方面确实忽略了空白。

如果 正在讨论文件输出,那么您的代码或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.

您会注意到所有空格都会被保留。

您还会注意到,令人讨厌的XML标头<?xml version="1.0" encoding="UTF-8"?>以及源中的标记链接标记<http://www.WEBADDRESS.com>以转义格式输出为{ {1}}。通过在模板之前添加以下内容可以解决这两个问题:

&lt;http://www.WEBADDRESS.com&gt;

由于我可以很容易地保留输出并保留空格,我必须假设您的问题是 XSL,而是您的浏览器在默认浏览器模式下显示此文本,该模式将崩溃< strong>每个空白字符串(空格,制表符,换行符的多个实例)组成一个空格字符。

正如约翰建议的那样,使用<xsl:output method="text"/> 元素,或使用定义<pre>样式的CSS,如果您想要严格布局,其中行在屏幕右端运行,或{{1如果你想保留空格,但浏览器换行太长而不能在边距内显示。