为什么 XML 有如此冗长的结束标记?

发布于 2024-10-06 11:20:01 字数 1432 浏览 2 评论 0 原文

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

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

发布评论

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

评论(6

書生途 2024-10-13 11:20:01

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

一个简单的例子?

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

你认为它很有可读性吗?如果不计算结束标签,很难理解 在哪里。

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.

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

鸩远一方 2024-10-13 11:20:01

我可以看到一个很大的优点:丢失的结束标签会立即被(人或计算机)捕获,而不是得到像 Insufficient close Tagsprovided 之类的错误;请仔细阅读您的 1000 行文件并找出它发生的位置。

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.

御守 2024-10-13 11:20:01

你的建议相当于S-Expression。你知道,所有 Lisp 都是用它编写的,例如 (thisisSomewhatLong Hello, world!)。确实有人认为这更好,因为它不那么冗长。他们是对的,它不那么冗长。但不管你喜欢与否,这种冗长也有其优点。最值得注意的是,它允许及早进行错误检测。对于 SExprs 或类似的,缺少一个近括号或有一个太多,意味着“有不匹配的括号,祝你好运找到你”(如果你幸运的话 - 如果你犯了两次这样的错误,它会被拉平并很容易搞砸所有标记 - 虽然它当然可以产生一个不符合模式的结构(假设你有这样的东西),这可以允许稍微更好的错误报告)。

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

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

Also see "XML is not S-Expressions".

想念有你 2024-10-13 11:20:01

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

它在某种程度上是人类可读的;它对于存储将被许多应用程序使用的数据非常有效,但最终,这些标签的存在是为了解析器可以读取该数据,检查标签是否匹配并用它做一些有意义的事情。

许多人不喜欢 XML 的冗长,所以如果您也不喜欢,也不必担心。你并不孤单。

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

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.

断舍离 2024-10-13 11:20:01

顺便说一句,风险是迷失方向

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

,它可以在没有结束标签名称的情况下很好地验证。

The risk is to get lost in

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

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

如果没有 2024-10-13 11:20:01

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

确实,您不需要计算结束标记,但这会被类似这样的错误风险所抵消:

<color>red</colour>

必须始终保持同步的冗余定义 = 压力。这就是为什么我几乎抵制 XML(如果可能的话)并选择 YAML,它不会遇到这个问题,而且在其他方​​面与 XML 一样具有表现力(减去 DTD,在我这些年里,它尚未向我展示任何价值) 。

另一种替代方案是 JSON,它同样避免了这种冗余问题,但 JSON 缺乏内部引用,并且无论如何 YAML 都是 JSON 的完整超集。

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.

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

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.

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