xml - XML 阅读器是否应该忽略 XML 文件中的连续空格?

标签 xml xmldom

在我的工作中,我们有自己的 XML 类来构建 DOM,但我不确定应该如何处理连续的空格?

例如

<some-text>
Hello     World
</some-text>

当它被读入 DOM 时,文本节点应该包含 Hello 和 World 之间的连续空白还是只将其减少到一个空格?

或者XML源应该这样写:

<some-text>
Hello &nbsp;&nbsp;&nbsp;&nbsp;World
</some-text>

或者如果不是  比也许 ?

最佳答案

听说人们甚至不看 XML 规范就实现 XML 处理器,这有点令人不安。

来自XML 1.0 specification, section 2.10 White Space Handling" (强调我的):

In editing XML documents, it is often convenient to use "white space" (spaces, tabs, and blank lines) to set apart the markup for greater readability. Such white space is typically not intended for inclusion in the delivered version of the document. On the other hand, "significant" white space that should be preserved in the delivered version is common, for example in poetry and source code.

An XML processor MUST always pass all characters in a document that are not markup through to the application. A validating XML processor MUST also inform the application which of these characters constitute white space appearing in element content.

A special attribute named xml:space may be attached to an element to signal an intention that in that element, white space should be preserved by applications. In valid documents, this attribute, like any other, MUST be declared if it is used. When declared, it MUST be given as an enumerated type whose values are one or both of "default" and "preserve". For example: ...

关于xml - XML 阅读器是否应该忽略 XML 文件中的连续空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/449532/

相关文章:

android - 如何在没有 XML 的情况下以编程方式将 fragment 添加到 Activity

java - SAX 过早结束解析?

xml - 将多个xml节点提取到数组中而不循环

javascript - 如何解析位于不同 Web 服务器上的 XML 文件?

javascript - 将 JavaScript XML DOM 对象转换为具有一定结构的对象

c# - 从 XML C# 读取特定位置

c# - 如何解决数据集错误 - 数据集不支持 'union' 或 'list' 作为 simpleType

python - 如何使用python读取xml文件?

php - 如何使用 PHP 解压 xml 文件

excel - VBA - 获取标签名称的 XPath 语法是什么