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

时间:2022-11-14 08:59:12

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

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

<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. 这能行吗(也就是说,在我没有考虑过的情况下,是否会出现模棱两可/失败的情况)?
  3. If it would work, why didn't 'they' design it that way?
  4. 如果可以的话,为什么“他们”不这样设计呢?

2 个解决方案

#1


5  

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.

一个原因是SGML/XML也被设计成人类可读的。您的/a/b示例是可读的,但是结构要复杂得多,这将是一个试图解释的噩梦。

This would especially be true with mixed content (PCDATA and element structures mixed).

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

#2


2  

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.

如果您确保XML具有适当的缩进,那么嵌套问题就不是问题了(下面是具有适当缩进的相同代码)。然而,由于缩进不重要,我们需要另一种机制来保持XML的可读性。在本例中,人类可读意味着人类可以很容易地看到哪些内容属于哪个元素。解决方案是命名每个结束标记引用的元素。

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

当我们给出结束标记名称时,即使是在不正确的缩进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>

#1


5  

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.

一个原因是SGML/XML也被设计成人类可读的。您的/a/b示例是可读的,但是结构要复杂得多,这将是一个试图解释的噩梦。

This would especially be true with mixed content (PCDATA and element structures mixed).

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

#2


2  

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.

如果您确保XML具有适当的缩进,那么嵌套问题就不是问题了(下面是具有适当缩进的相同代码)。然而,由于缩进不重要,我们需要另一种机制来保持XML的可读性。在本例中,人类可读意味着人类可以很容易地看到哪些内容属于哪个元素。解决方案是命名每个结束标记引用的元素。

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

当我们给出结束标记名称时,即使是在不正确的缩进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>