为什么XML有这样详细的结束标记?

时间:2021-03-09 19:39:29

Yes, this probably shouldn't bug me.

是的,这可能不应该让我烦恼。

But it does!!

但它确实!!

Why does XML have such verbose closing tags? Not only does it make documents uglier for humans, it needlessly introduces the risk of mismatched (or misspelled!) opening and closing tags.

为什么XML有这样详细的结束标记?它不仅使文档对人类更加丑陋,而且不必要地引入了不匹配(或拼写错误!)打开和关闭标签的风险。

Even if we wanted to require closing tags, why do we need to include the name of the opening tag inside the closing tag? There is never any ambiguity in XML, because innermost tags must be closed before closing outer tags!

即使我们想要关闭标签,为什么我们需要在结束标签中包含开始标签的名称? XML中从不存在任何歧义,因为在关闭外部标记之前必须关闭最内层的标记!

For example:

例如:

<thisIsSomewhatLong>
    Hello, world!
</thisIsSomewhatLong>

...is so much more verbose than:

...比以下更加冗长:

<thisIsSomewhatLong>
    Hello, world!
</>

And it doesn't resolve any ambiguity, either for humans or for computers.

并且它无法解决人类或计算机的任何歧义。

Does anyone know what the rationale is for this rule? What risks are avoided by disallowing empty closing tags?

有谁知道这个规则的基本原理是什么?不允许空的关闭标签可以避免哪些风险?

6 个解决方案

#1


18  

Because it improves readability, XML was born not to be efficient or concise, just to be easy to work with.. and if you think having </> wouldn't create ambiguities it is just because you are indenting the code. If you leave out indentation (which is a really weaker constraint compared to having the name in a closing tag) then it becomes a mess.

因为它提高了可读性,所以XML本身并不高效或简洁,只是为了易于使用......如果你认为不会产生歧义,那只是因为你缩进了代码。如果省略缩进(与在结束标记中使用名称相比,这是一个非常弱的约束)那么它就变得一团糟。

A simple example?

一个简单的例子?

<A><B><C><D>foo</><D>bar</></><H>baz</></></>

You think it's so readable? It's hard to understand where <H> is without counting closing tags..

你认为它是如此可读?很难理解 在哪里没有计算结束标签..

#2


14  

I can see one big advantage: missing closing tags are caught (by the human or computer) right away, rather than getting an error like Insufficient closing tags provided; please read through your 1000 line file and figure out where it happened.

我可以看到一个很大的优势:缺少关闭标签(由人或计算机)立即捕获,而不是像提供的关闭标签不足那样得到错误;请仔细阅读您的1000行文件并找出它发生的位置。

#3


5  

What you suggest amounts to S-Expression. You know, the thingy all Lisp is written in, e.g. (thisisSomewhatLong Hello, world!). There are indeed some who argue that this is better, because it is way less verbose. They are right, it is less verbose. But like it or not, this verbosity also has advantages. Most notably, it allows early error detection. With SExprs or similar, missing a close paren or having one too much that means "there are mismatched parens, good luck finding you" (if you're lucky - if you make such a mistake twice, it evens out and could easily screw all the markup - although it could of course yield a structure that doesn't conform the schema (assuming you have something like this) which can allow slightly better error reporting).

你的建议相当于S-Expression。你知道,所有Lisp的东西都是写的,例如(thisisSomewhatLong你好,世界!)。确实有人认为这样做更好,因为它不那么冗长。他们是对的,它不那么冗长。但无论喜欢与否,这种冗长也具有优势。最值得注意的是,它允许早期错误检测。有了SExprs或类似的东西,错过了一个关闭的paren或者有一个太多,意味着“有不匹配的parens,祝你好运”(如果你很幸运 - 如果你犯了两次这样的错误,它会变得均匀,并且很容易搞砸所有标记 - 虽然它当然可以产生一个不符合模式的结构(假设你有这样的东西),这可以允许稍微更好的错误报告)。

Also see "XML is not S-Expressions".

另请参阅“XML不是S表达式”。

#4


3  

Although you might read on the net otherwise, XML is primarily computer readable, and therefore, uses opening and closing tags for validity checking.

虽然您可能会在网上阅读,但XML主要是计算机可读的,因此,使用开始和结束标记进行有效性检查。

It is somewhat human readable; it is efficient for storing data that will be used by many applications, but ultimately, these tags exist so a parser can read that data, check if tags match and do something meaningful with it.

它有点人性化;它可以高效地存储许多应用程序将使用的数据,但最终存在这些标记,因此解析器可以读取该数据,检查标记是否匹配并对其执行有意义的操作。

Many people don't like XML's verboseness, so if you don't also, don't worry. You're not alone.

很多人不喜欢XML的冗长,所以如果你不喜欢,也不要担心。你不是一个人。

#5


1  

The risk is to get lost in

风险就是迷失方向

    ...
    ...
    ...
    </>
   </>
  </>
 </>
</>

BTW, it can validated fine without end-tag names.

顺便说一句,它可以在没有结束标记名称的情况下进行验证。

#6


1  

I suppose it is for readability, as mentioned above. However, it violates the DRY principle and thus introduces a source of errors, and of course it bloats your document size, which doubly sucks if you're passing it around over a network, which is a common thing to do these days.

我想这是为了可读性,如上所述。然而,它违反了DRY原则,因此引入了错误来源,当然它会使你的文档大小膨胀,如果你通过网络传递它会倍加糟糕,这是现在常见的事情。

True, you don't need to count closing tags, but that's offset by the risk of errors like this:

没错,您不需要计算结束标记,但这可以抵消这样的错误风险:

<color>red</colour>

Redundant definitions that must always be kept in sync = stress. That's why I pretty much boycott XML (when possible) and choose YAML, which does not suffer from this problem and is otherwise every bit as expressive as XML (minus the DTDs, which in all my years have yet to demonstrate any value to me).

冗余定义必须始终保持同步=压力。这就是为什么我几乎抵制XML(如果可能的话)并选择YAML,它没有遇到这个问题,而且与XML一样富有表现力(减去DTD,这在我多年来都没有向我证明任何价值) 。

Another alternative is JSON, which similarly avoids this redundancy problem, but JSON lacks internal references, and in any case YAML is a full superset of JSON.

另一个替代方案是JSON,它同样避免了这种冗余问题,但JSON缺少内部引用,并且在任何情况下YAML都是JSON的完整超集。

#1


18  

Because it improves readability, XML was born not to be efficient or concise, just to be easy to work with.. and if you think having </> wouldn't create ambiguities it is just because you are indenting the code. If you leave out indentation (which is a really weaker constraint compared to having the name in a closing tag) then it becomes a mess.

因为它提高了可读性,所以XML本身并不高效或简洁,只是为了易于使用......如果你认为不会产生歧义,那只是因为你缩进了代码。如果省略缩进(与在结束标记中使用名称相比,这是一个非常弱的约束)那么它就变得一团糟。

A simple example?

一个简单的例子?

<A><B><C><D>foo</><D>bar</></><H>baz</></></>

You think it's so readable? It's hard to understand where <H> is without counting closing tags..

你认为它是如此可读?很难理解 在哪里没有计算结束标签..

#2


14  

I can see one big advantage: missing closing tags are caught (by the human or computer) right away, rather than getting an error like Insufficient closing tags provided; please read through your 1000 line file and figure out where it happened.

我可以看到一个很大的优势:缺少关闭标签(由人或计算机)立即捕获,而不是像提供的关闭标签不足那样得到错误;请仔细阅读您的1000行文件并找出它发生的位置。

#3


5  

What you suggest amounts to S-Expression. You know, the thingy all Lisp is written in, e.g. (thisisSomewhatLong Hello, world!). There are indeed some who argue that this is better, because it is way less verbose. They are right, it is less verbose. But like it or not, this verbosity also has advantages. Most notably, it allows early error detection. With SExprs or similar, missing a close paren or having one too much that means "there are mismatched parens, good luck finding you" (if you're lucky - if you make such a mistake twice, it evens out and could easily screw all the markup - although it could of course yield a structure that doesn't conform the schema (assuming you have something like this) which can allow slightly better error reporting).

你的建议相当于S-Expression。你知道,所有Lisp的东西都是写的,例如(thisisSomewhatLong你好,世界!)。确实有人认为这样做更好,因为它不那么冗长。他们是对的,它不那么冗长。但无论喜欢与否,这种冗长也具有优势。最值得注意的是,它允许早期错误检测。有了SExprs或类似的东西,错过了一个关闭的paren或者有一个太多,意味着“有不匹配的parens,祝你好运”(如果你很幸运 - 如果你犯了两次这样的错误,它会变得均匀,并且很容易搞砸所有标记 - 虽然它当然可以产生一个不符合模式的结构(假设你有这样的东西),这可以允许稍微更好的错误报告)。

Also see "XML is not S-Expressions".

另请参阅“XML不是S表达式”。

#4


3  

Although you might read on the net otherwise, XML is primarily computer readable, and therefore, uses opening and closing tags for validity checking.

虽然您可能会在网上阅读,但XML主要是计算机可读的,因此,使用开始和结束标记进行有效性检查。

It is somewhat human readable; it is efficient for storing data that will be used by many applications, but ultimately, these tags exist so a parser can read that data, check if tags match and do something meaningful with it.

它有点人性化;它可以高效地存储许多应用程序将使用的数据,但最终存在这些标记,因此解析器可以读取该数据,检查标记是否匹配并对其执行有意义的操作。

Many people don't like XML's verboseness, so if you don't also, don't worry. You're not alone.

很多人不喜欢XML的冗长,所以如果你不喜欢,也不要担心。你不是一个人。

#5


1  

The risk is to get lost in

风险就是迷失方向

    ...
    ...
    ...
    </>
   </>
  </>
 </>
</>

BTW, it can validated fine without end-tag names.

顺便说一句,它可以在没有结束标记名称的情况下进行验证。

#6


1  

I suppose it is for readability, as mentioned above. However, it violates the DRY principle and thus introduces a source of errors, and of course it bloats your document size, which doubly sucks if you're passing it around over a network, which is a common thing to do these days.

我想这是为了可读性,如上所述。然而,它违反了DRY原则,因此引入了错误来源,当然它会使你的文档大小膨胀,如果你通过网络传递它会倍加糟糕,这是现在常见的事情。

True, you don't need to count closing tags, but that's offset by the risk of errors like this:

没错,您不需要计算结束标记,但这可以抵消这样的错误风险:

<color>red</colour>

Redundant definitions that must always be kept in sync = stress. That's why I pretty much boycott XML (when possible) and choose YAML, which does not suffer from this problem and is otherwise every bit as expressive as XML (minus the DTDs, which in all my years have yet to demonstrate any value to me).

冗余定义必须始终保持同步=压力。这就是为什么我几乎抵制XML(如果可能的话)并选择YAML,它没有遇到这个问题,而且与XML一样富有表现力(减去DTD,这在我多年来都没有向我证明任何价值) 。

Another alternative is JSON, which similarly avoids this redundancy problem, but JSON lacks internal references, and in any case YAML is a full superset of JSON.

另一个替代方案是JSON,它同样避免了这种冗余问题,但JSON缺少内部引用,并且在任何情况下YAML都是JSON的完整超集。