结束标签内的 XML/HTML 标签名称真的有必要吗?
这本身并不是一个编程问题,但我想知道为什么 XML 的结束标记中需要标记的名称。例如,不能
<a>
<b>stuff</b>
</a>
写成
<a>
<b>stuff</>
</>
使得每个结束标记 仅终止最后一个打开的标记吗?
所以我的问题是
- 这是否可行(即是否有任何我没有想到的极端情况,其中这会是不明确/失败的)?
- 如果它可行,为什么“他们”不那样设计呢?
This is really not a programming question per se, but I was wondering why the name of the tag is required in a closing tag in XML. For instance, couldn't
<a>
<b>stuff</b>
</a>
Be written
<a>
<b>stuff</>
</>
So that each closing tag </>
merely terminated the last opened tag?
So my questions are
- Would this work (i.e. are there any corner cases I'm not thinking of in which this would be ambiguous/fail)?
- If it would work, why didn't 'they' design it that way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因之一是 SGML/XML 也被设计为人类可读。您的
/a/b
示例是可读的,但是更复杂的结构对于尝试解释来说将是一场噩梦。对于混合内容(PCDATA 和元素结构混合)尤其如此。
One reason is that SGML/XML are also designed to be human readable. Your
/a/b
example is readable, but a structure much more complex would be a nightmare to try to interpret.This would especially be true with mixed content (PCDATA and element structures mixed).
它可以工作,但是调试嵌套问题会很糟糕。例如:
如果您确保 XML 有正确的缩进,则嵌套问题就不是问题(下面是具有正确缩进的相同代码。但是,由于缩进微不足道,我们需要另一种机制来保持 XML 的可读性。在这个例子中,人类可读意味着人们可以轻松地看到哪些内容属于哪个元素,解决方案是命名每个结束标签引用的元素
,即使在缩进不正确的 XML 中也可以清楚地看到 。那该文本属于元素
。It would work, but it would be awful to debug a nesting issue. For example:
If you make sure the XML has proper indentation, the nesting issue isn't a problem (below is the same code with proper indentation. Yet because indentation is insignificant, we need another mechanism to keep the XML human-readable. In this example, human-readable means a human can easily see which content belongs to which element. The solution is to name which element each closing tag refers to.
When we give the closing tags names, even it's clear to see even in improperly indented XML that the text belongs to element
<eight>
.