难道《无废话XML》写错了??!!请大家帮忙看看,给分

hackerning 2002-04-22 06:09:47
书里说XML对于空格是不忽略的,有多少就显示多少,而且不需要加什么额外的属性,因为他是XML的一个标准,但事实上我试了一下,结果和普通的HTML没区别啊,
对于空格还是忽略了啊,为什么,有谁能解释一下吗,立刻给分的!!
...全文
43 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2002-04-22
  • 打赏
  • 举报
回复
Example of <xsl:preserve-space> and <xsl:strip-space>
You can use the <xsl:preserve-space> and <xsl:strip-space> elements to preserve and strip white space from XML documents. The following strippreserve.xml document contains <code> and <block> elements that have white space in the form of tabs, spaces, and newline characters. The strippreserve.xsl style sheet is applied to the strippreserve.xml document to retain white space within all <code> elements and to strip it from all the block elements.

In the strippreserve.xml document, the first <code> element and first <block> element contain exactly the same amount and kind of white space (a single space), as do the second <code> and <block> elements. However, the second <code> and second <block> elements also contain the phrase "Some text" among the white space. For the strippreserve.xml document, the <?xml-stylesheet type="text/xsl" href="strippreserve.xsl" ?> instruction is included to apply the strippreserve.xsl style sheet to the strippreserve.xml document. When applied, the strippreserve.xsl style sheet causes the <code> elements to retain white space-only content and the <block> elements to lose white space-only content.

Example
The strippreserve.xsl style sheet below does the following.

Preserves the extraneous white space in the <code> elements.
Strips the extraneous white space from the <block> elements.
Encloses the content of each <code> and <block> element in square brackets, [ ].
Provides a numbered label for each <code> or <block> using the XSL Transformations (XSLT) position() function.
Using the XML Path Language (XPath) translate() function, converts "invisible" white space to a visible character, one character at a time. This enables you to "see" the specific white characters in the <code> and <block> elements.
Each space character appears in the result tree as a hyphen (-).

Each newline character appears in the result tree as the letter "N."

Each carriage return appears in the result tree as the letter "R."

Each tab appears in the result tree as the letter "T."

The first <code> element contains a single space, denoted by the hyphen in the [ ] characters. The first <block> element also contains a single space in the source document. However, the <xsl:strip-space> setting has caused this space to be removed from the result tree.

In the second <code> and <block> elements, no white space has been removed. If an element has any non-white space content, white space will not be stripped even if <xsl:strip-space> is set for that element.

Note This represents a "pure XSLT" approach to the stripping and retention of white space. You can also strip or retain white space by manipulating the Document Object Model (DOM). For more information about this approach, see White Space and the DOM.
XML File (strippreserve.xml)
Copy this text to a file, then find and replace each "tabhere" by a tab (press the Tab key).

<?xml-stylesheet type="text/xsl" href="strippreserve.xsl" ?>
<document>
<block> </block>
<block>
tabhere
Some texttabhere
tabhere
tabhere</block>
<code> </code>
<code>
tabhere
Some texttabhere
tabhere
tabhere</code>
</document>
XSLT File (strippreserve.xsl)
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Retain white space within all <code> elements -->
<xsl:preserve-space elements="code"/>
<!-- ... but strip it from all <block> elements -->
<xsl:strip-space elements="block"/>

<xsl:template match="/">
<html>
<head><title>Test: Stripping/Preserving White Space</title></head>
<body>
<h4>Code blocks:</h4>
<!-- <pre> element forces all output characters to be same width -->
<pre>
<!-- Use translate() XPath function to convert white-space
characters to "visible" form. -->
<xsl:for-each select="//code">
"Code" #<xsl:value-of select="position()"/>: [<xsl:value-of select="translate(.,' ','-NRT')"/>]<br/>
</xsl:for-each>
</pre>
<h4>Normalized blocks:</h4>
<pre>
<xsl:for-each select="//block">
"Block" #<xsl:value-of select="position()"/>: [<xsl:value-of select="translate(.,' ','-NRT')"/>]<br/>
</xsl:for-each>
</pre>
</body>
</html>
</xsl:template>

</xsl:stylesheet>
Formatted Output
Code blocks:

"Code" #1: [-]

"Code" #2: [NTNSome-textTNTNT]

Normalized blocks:

"Block" #1: []

"Block" #2: [NTNSome-texttabhereNTNT]

Processor Output
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-16">
<title>Test: Stripping/Preserving White Space</title></head>
<body>
<h4>Code blocks:</h4>
<pre>
"Code" #1: [-]<br>
"Code" #2: [NTNSome-textTNTNT]<br></pre>
<h4>Normalized blocks:</h4>
<pre>
"Block" #1: []<br>
"Block" #2: [NTNSome-textTNTNT]<br></pre>
</body>
</html>
孟子E章 2002-04-22
  • 打赏
  • 举报
回复
Preserving or Stripping White Space
XSL Transformations (XSLT) can distinguish nodes that contain white space intermingled with other characters. The white space is considered inseparable from the other text in the node.

For nodes that contain nothing but white space, the <xsl:preserve-space> and <xsl:strip-space> elements handle how the nodes are output.

Preserving White Space with <xsl:preserve-space>
The <xsl:preserve-space> element provides a list of those elements in the source document where white space must be preserved on output. <xsl:preserve-space> is always an empty, top-level element, for example, a child of the <xsl:stylesheet> element in an XSLT style sheet.

The following is the general syntax.

<xsl:preserve-space elements="elem1 elem2..." />
elem1 and elem2 are the names of all elements (without the enclosing < and > delimiters) whose white space must be preserved.

You can specify all of the elements in the source document with the asterisk operator ("*").

<xsl:preserve-space elements="*" />
Because all of the content of an XML document is, by default, preserved, <xsl:preserve-space> is useful only in cases in which you have used <xsl:strip-space> to override the default behavior; in such cases, use <xsl:preserve-space> to identify exceptions to the explicit stripping of white space.

Note The Microsoft® XSLT processor will process all of a document's white space only if the preserveWhiteSpace property has been set to True prior to loading the document into the DOM. For more information, see How the MSXML Processor Parses White Space.
Removing White Space with <xsl:strip-space>
The <xsl:strip-space> element provides a list of those elements in the source document in which content must be removed from the output tree. <xsl:strip-space> is an empty, top-level element.

The following is the general syntax.

<xsl:strip-space elements="elem1 elem2..." />
elem1 and elem2 are the names of all elements (without the enclosing < and > delimiters) whose white space must be removed.

You can specify all of the elements in the source document with the asterisk operator ("*").

<xsl:strip-space elements="*" />
If an element appears in both an <xsl:strip-space> and <xsl:preserve-space> list, the last specification applies. Therefore, a typical sequence of these two elements in an XSLT style sheet is as follows.

<xsl:strip-space elements="*" />
<xsl:preserve-space elements="elem1 elem2..." />
If this order were reversed, the explicit "preserve" settings for elem1, elem2, and so on would be overridden by the global "strip" setting.
wangwenyou 2002-04-22
  • 打赏
  • 举报
回复
将你的styleSheet的xsl:space属性的值设为preserved

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧