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

发布于 2024-07-12 02:54:35 字数 371 浏览 8 评论 0原文

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

例如,

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

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

或者 XML 源应该这样写:

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

或者如果不是 &nbsp; 也许比 ?

At my work we have our own XML classes which build a DOM, but I'm not sure how consecutive whitespace should be handled?

e.g.

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

When this is read into the DOM, should the text node include the consecutive whitespace inbetween Hello and World or just reduce it to one space?

Or should the XML source be written like this:

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

or if not   than perhaps ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦幻的心爱 2024-07-19 02:54:36

  是一个 HTML 实体,与 XML 本身无关。

不过,为了回答你的问题,我认为这很重要。 即使 HTML DOM 也将连续的空格视为重要的,只是它仅在视觉上呈现一个空格。 它在 DOM 中的显示方式和它在屏幕上的显示方式是完全不同的两件事。

  is a HTML entity and nothing to do with XML itself.

To answer your question though, i would treat that as significant. Even the HTML DOM treats consecutive spaces as significant, it's just that it only visually renders one space. How it appears in the DOM and how it appears on your screen are two entirely different things.

盗心人 2024-07-19 02:54:36

编辑:标签内的空白被认为是重要的(我最初的想法就像 HTML 是错误的;首先谷歌,稍后回答问题!)参见 此说明

EDIT: whitespace within tags is considered significant (my initial thoughts on this being like HTML were wrong; google first, answer questions later!) see this explanation

靑春怀旧 2024-07-19 02:54:36

在我看来,在这种情况下,将空格视为重要的空格似乎很自然。 我希望 DOM 节点的值等于我在标记中使用的值。

IMO it seems quite natural to treat whitespaces as significant in this case. I would expect DOM node's value to be equal to what I used in markup.

南风几经秋 2024-07-19 02:54:36

如果您“有我们自己的 XML 类”,而编写它们的人还不知道这个问题的答案,那么您可能还有很多很多尚未发现的问题。

If you "have our own XML classes," and whoever wrote them doesn't already know the answer to this question, you probably have many, many, many more problems that you just haven't discovered yet.

毁虫ゝ 2024-07-19 02:54:35

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

来自 XML 1.0 规范,第 2.10 节空白处理”(强调我的):

在编辑XML文档时,经常会遇到这样的情况:
方便使用“空白”
(空格、制表符和空行)来设置
分开标记以获得更大的
可读性。 这样的空白是
通常不打算包含在内
在交付的版本中
文档。 另一方面,
“重要的”空白应该
保留在交付版本中
很常见,例如在诗歌中
源代码。

XML 处理器必须始终通过所有
文档中不存在的字符
标记到应用程序。
A
验证 XML 处理器还必须
通知应用程序其中哪一个
字符构成空白
出现在元素内容中。

名为 xml:space 的特殊属性
可以附加到一个元素
发出这样的意图:
元素,空白应该是
由应用程序保存。 无效的
文档,这个属性,就像任何
其他,如果使用必须声明。
声明时,它必须作为
值为 1 的枚举类型
或“默认”和“保留”两者。
例如:...

It's a bit disconcerting to hear that people are out there implementing XML processors without even looking at the XML specifications.

From the XML 1.0 specification, section 2.10 White Space Handling" (emphasis mine):

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: ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文