Xml 简明教程

XML - WhiteSpaces

在本章中,我们将讨论 XML 文档中的 whitespace 处理。空白是一系列空格、制表符和换行符。它们通常用于使文档更容易阅读。

In this chapter, we will discuss whitespace handling in XML documents. Whitespace is a collection of spaces, tabs, and newlines. They are generally used to make a document more readable.

XML 文档包含两种类型的空白-有效空白和无效空白。两种空白的解释和示例如下。

XML document contains two types of whitespaces - Significant Whitespace and Insignificant Whitespace. Both are explained below with examples.

Significant Whitespace

有效空白出现在包含文本和标记共同存在的元素中。例如 −

A significant Whitespace occurs within the element which contains text and markup present together. For example −

<name>TanmayPatil</name>

and

<name>Tanmay Patil</name>

上述两个元素不同,因为 TanmayPatil 之间有空格。在 XML 文件中读取此元素的任何程序都有义务保持区别。

The above two elements are different because of the space between Tanmay and Patil. Any program reading this element in an XML file is obliged to maintain the distinction.

Insignificant Whitespace

无效空白是指只允许元素内容的空格。例如 −

Insignificant whitespace means the space where only element content is allowed. For example −

<address.category = "residence">
<address....category = "..residence">

上述示例相同。此处,空格用点号 (.) 表示。在上一个示例中,address 和 category 之间的空格是无效的。

The above examples are same. Here, the space is represented by dots (.). In the above example, the space between address and category is insignificant.

可以将一个名为 xml:space 的特殊属性附加到元素。这表示应用程序不应为此元素删除空白。您可以将此属性设置为 defaultpreserve ,如下面的示例所示 −

A special attribute named xml:space may be attached to an element. This indicates that whitespace should not be removed for that element by the application. You can set this attribute to default or preserve as shown in the following example −

<!ATTLIST address  xml:space (default|preserve) 'preserve'>

其中,

Where,

  1. The value default signals that the default whitespace processing modes of an application are acceptable for this element.

  2. The value preserve indicates the application to preserve all the whitespaces.