结束标签内的 XML/HTML 标签名称真的有必要吗?

发布于 2024-11-25 13:15:26 字数 364 浏览 0 评论 0原文

这本身并不是一个编程问题,但我想知道为什么 XML 的结束标记中需要标记的名称。例如,不能

<a>
    <b>stuff</b>
</a>

写成

<a>
    <b>stuff</>
</>

使得每个结束标记 仅终止最后一个打开的标记吗?

所以我的问题是

  1. 这是否可行(即是否有任何我没有想到的极端情况,其中这会是不明确/失败的)?
  2. 如果它可行,为什么“他们”不那样设计呢?

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

  1. Would this work (i.e. are there any corner cases I'm not thinking of in which this would be ambiguous/fail)?
  2. If it would work, why didn't 'they' design it that way?

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

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

发布评论

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

评论(2

菩提树下叶撕阳。 2024-12-02 13:15:26

如果它可行,为什么“他们”不那样设计?

原因之一是 SGML/XML 也被设计为人类可读。您的 /a/b 示例是可读的,但是更复杂的结构对于尝试解释来说将是一场噩梦。

对于混合内容(PCDATA 和元素结构混合)尤其如此。

If it would work, why didn't 'they' design it that way?

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

落在眉间の轻吻 2024-12-02 13:15:26

它可以工作,但是调试嵌套问题会很糟糕。例如:

<one><two><nine></><ten></><eight><three></><four>
<five></><six></></>
<seven></>To what element does this text belong?</></></>

如果您确保 XML 有正确的缩进,则嵌套问题就不是问题(下面是具有正确缩进的相同代码。但是,由于缩进微不足道,我们需要另一种机制来保持 XML 的可读性。在这个例子中,人类可读意味着人们可以轻松地看到哪些内容属于哪个元素,解决方案是命名每个结束标签引用的元素

<one>
<two>
    <nine>
    </nine>
    <ten>
    </ten>
    <eight>
        <three>
        </>
        <four>
            <five>
            </>
            <six>
            </>
        </>
        <seven>
        </>
        To what element does this text belong?
    </>
</>
</>

,即使在缩进不正确的 XML 中也可以清楚地看到 。那该文本属于元素

<one><two><nine></nine><ten></ten><eight><three></three><four>
<five></five><six></six></four><seven>
</seven>To what element does this text belong?</eight></two></one>

It would work, but it would be awful to debug a nesting issue. For example:

<one><two><nine></><ten></><eight><three></><four>
<five></><six></></>
<seven></>To what element does this text belong?</></></>

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.

<one>
<two>
    <nine>
    </nine>
    <ten>
    </ten>
    <eight>
        <three>
        </>
        <four>
            <five>
            </>
            <six>
            </>
        </>
        <seven>
        </>
        To what element does this text belong?
    </>
</>
</>

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

<one><two><nine></nine><ten></ten><eight><three></three><four>
<five></five><six></six></four><seven>
</seven>To what element does this text belong?</eight></two></one>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文