无效元素和空元素是否相同? [英] Are void elements and empty elements the same?

查看:79
本文介绍了无效元素和空元素是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自90年代以来我一直在编写HTML,但是我刚刚发现了 colgroup col 元素。 根据MDN col 是一个空元素,结束标记是禁止


省略:开始标记是强制​​性的,但是,因为它是一个空元素,
禁止使用结束标记。



我从来没有听说过一个空洞的元素。 MDN没有关于void元素的页面,但是空元素页面表示:注意:在极少数情况下,空元素被称为void
元素。这是一个不适当的名字,应该避免。


然而, W3规范仅引用void元素,从不提及空元素:


void元素是一个元素,其内容模型从不允许
在任何情况下都有内容。


所以我想知道:


  1. 如果这些是相同的东西,它们是否应该被称为空元素而不是空元素?在这种情况下,W3规范是否过时而不被信任?或者W3规范应该优先考虑,即使它已经过时了?

  2. 如果它们不是同一个东西,那就是< col span =2/> gt ; 有效的语法,或者它应该是< col span =2> (没有斜线),因为结束标记是被禁止的?我可能对结束标记有错误的想法,但我一直认为 /> (如< br /> 和< img /> )作为排序的结束标记


解决方案

术语空元素来自SGML,HTML5之前的HTML标准基于此标准,并且EMPTY关键字用于表示元素一个空的内容模型。以下是 HTML 4规范所说的内容:


元素的允许内容称为其内容模型。设计为不含内容的元素类型称为空元素。这些元素类型的内容模型是使用关键字EMPTY声明的。


使用 img 元素:


这个例子演示了一个空元素类型的声明:

 <!ELEMENT IMG  -  O EMPTY> 




  • 声明的元素类型为 IMG
  • 连字符和下面的O表示可以省略结束标记,但与内容模型EMPTY一起,这强化了结束标记< 必须必须省略

  • EMPTY关键字表示此类型的实例不能有内容。


XML 定义一个空元素的方式完全不同:


<定义:一个没有内容的元素被称为

这里的区别在于XML没有说空元素是内容模型为空的元素。相反,它只是说一个空元素是没有内容的元素。这与文档类型或XML模式是否将该特定元素定义为不需要内容无关; XML本身本质上不存在这样的限制。

另外一个术语空元素标签用于描述快捷键语法 /> gt ; 通常用于表示空元素(不管它们是否为空)。这通常也称为自闭语法。



术语void element对于 HTML5 。它具有与空元素的HTML5之前定义相同的定义:即,仅 具有开始标记,没有结束标记,并且不能有任何内容的元素。虽然W3C HTML5规范没有引用术语空元素,但XML中描述的术语空元素标记用于相关文档


在HTML语法中,void元素是始终为空的元素并且从来没有结束标签。在HTML规范中或在扩展规范中列出为void 的所有元素都必须在polyglot中标记具有XML的语法形式 空元素标记 < foo /> )。其他元素不能使用XML空元素标记语法。


现在的HTML标准似乎更喜欢XML定义,并且避开前定义。这看起来合适,因为现代HTML不再是SGML应用程序,而是标记语言本身就是。 (这也不是XML,但这就是polyglot标记起作用的地方。)

所以,总结一下:


  • 无论内容是否被允许,空元素都是 没有内容的元素。

  • 无效元素不能有任何内容的元素。



另外,可以说根据定义,所有void元素都是空元素,但是一个空元素不一定代表一个void元素。



在回答你的问题:
$ b



  1. 如果这些是相同的东西,它们是否应该被称为空元素,而不是无效的元素?在这种情况下,W3规范是否过时而不被信任?或者W3规范应该优先考虑,即使它过时了?


    元素如区域 br col img 是更准确地称为空元素,就像在HTML5中一样。他们被认为是空的,但只是因为他们不能是非空。

    我不知道为什么MDN有一篇文章说['虚空元素']是一个不恰当的名字,应该避免。当它在大多数HTML引用中使用该名称时,无论如何。我认为没有理由不相信官方的规范。



  2. 如果它们不是一回事,那么< col span =2/> 有效的语法,或者它应该是< col span =2> 斜杠),因为结束标签被禁止?我可能对结束标记有错误的想法,但我一直认为 /> (如< br /> 和< img /> )作为排序的结束标记。

    < col span =2/> 只是有效的语法,因为HTML5认为它是标记void元素的流行方式感谢XHTML,并且不允许它不必要地破坏验证与许多XHTML文档的兼容性,否则这些文档将会验证为HTML5。 HTML5本身定义 /> 是没有意义的(一个特定的异常,这里并不真正相关),所以实际上HTML5中的< col span =2/> 只是表示一个 col ,只有一个开始标记和没有结束标记,因此与< col span =2> ,尽管XML友好。



I've been writing HTML since the 90s, but I just found out about the colgroup and col elements. According to MDN, col is a void element and the end tag is forbidden.

Tag omission: The start tag is mandatory, but, as it is a void element, the use of an end tag is forbidden.

I had never heard of a void element before. MDN doesn't have a page on void elements, but the empty element page says:

Note: In very rare cases, empty elements are referred to as void elements. This is an improper name and should be avoided.

However, the W3 spec refers to void elements only and never mentions empty elements:

A void element is an element whose content model never allows it to have contents under any circumstances. Void elements can have attributes.

So I'm wondering:

  1. If these are the same thing, should they be referred to as empty elements and never as void elements? In that case, is the W3 spec outdated and not to be trusted? Or should the W3 spec take precedence, even if it is outdated?
  2. If they are not the same thing, is <col span="2" /> valid syntax or should it be <col span="2"> (without the slash) because the end tag is forbidden? I may have the wrong idea of "end tag" but I've always thought of the /> (as in <br /> and <img />) to be an end tag of sorts.

解决方案

The term "empty element" comes from SGML, on which HTML standards prior to HTML5 were based, and where the EMPTY keyword is used to represent elements with an empty content model. Here's what the HTML 4 spec says:

The allowed content for an element is called its content model. Element types that are designed to have no content are called empty elements. The content model for such element types is declared using the keyword "EMPTY".

With an example declaration for the img element:

This example illustrates the declaration of an empty element type:

<!ELEMENT IMG - O EMPTY>

  • The element type being declared is IMG.
  • The hyphen and the following "O" indicate that the end tag can be omitted, but together with the content model "EMPTY", this is strengthened to the rule that the end tag must be omitted.
  • The "EMPTY" keyword means that instances of this type must not have content.

XML defines an "empty element" quite differently:

[Definition: An element with no content is said to be empty.]

The difference here is that XML does not say that an "empty element" is "an element whose content model is empty". Instead, it simply says that an "empty element" is one that has no content. This is regardless of whether or not the document type or XML schema defines that specific element to have no content by necessity; XML itself by nature places no such restrictions.

An additional term, "empty-element tag", is used to describe the shortcut syntax /> commonly used to indicate empty elements (again, regardless of whether or not they are empty by definition). This is also commonly referred to as "self-closing" syntax.

The term "void element" is new to HTML5. It has the same definition as the pre-HTML5 definition of "empty element": namely, an element that only has a start tag, no end tag, and cannot have any content whatsoever. Although the W3C HTML5 spec does not reference the term "empty element", the term "empty-element tag" as described in XML is used in a related document:

In the HTML syntax, void elements are elements that always are empty and never have an end tag. All elements listed as void in the HTML specification or in an extension spec, MUST in polyglot markup have the syntactic form of an XML empty-element tag (<foo/>). Other elements MUST NOT use the XML empty-element tag syntax.

It seems that modern HTML standards now prefer the XML definition and eschew the former definition. This seems fitting, because modern HTML is no longer an SGML application, but a markup language in its own right. (It's not XML either, but that's where polyglot markup comes into play.)

So, to summarize:

  • An empty element is one that has no content, regardless of whether it is allowed to have content in the first place.
  • A void element is one that cannot have any content.

Additionally, it can be said that all void elements are empty elements by definition, but an empty element does not necessarily represent a void element.

In answer to your questions:

  1. If these are the same thing, should they be referred to as empty elements and never as void elements? In that case, is the W3 spec outdated and not to be trusted? Or should the W3 spec take precedence, even if it is outdated?

    Elements such as area, br, col and img are more accurately referred to as void elements, as in HTML5. They are considered empty as well, but only because they can't be "non-empty".

    I have no idea why MDN has an article that says "['Void elements'] is an improper name and should be avoided." when it uses the name in most of its HTML references anyway. I see no reason not to trust the official specifications.

  2. If they are not the same thing, is <col span="2" /> valid syntax or should it be <col span="2"> (without the slash) because the end tag is forbidden? I may have the wrong idea of "end tag" but I've always thought of the /> (as in <br /> and <img />) to be an end tag of sorts.

    <col span="2" /> is only valid syntax because HTML5 recognizes it as a popular way of marking up void elements thanks to XHTML, and to disallow it would needlessly break validation compatibility with many XHTML documents that would otherwise validate as HTML5. HTML5 itself defines /> to be meaningless (with one specific exception that's not really relevant here), so in actuality <col span="2" /> in HTML5 simply represents a col with just a start tag and no end tag, and is therefore identical to <col span="2">, albeit XML-friendly.

这篇关于无效元素和空元素是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆