简体   繁体   English

关闭空标签:XHTML5或HTML5? 为什么一个接一个?

[英]Closing empty tags: XHTML5 or HTML5? Why one over the other?

After reading extensively about HTML5 and XHTML5 and after reading this post: Are (non-void) self-closing tags valid in HTML5? 在广泛阅读了有关HTML5和XHTML5的内容之后,并阅读了这篇文章:( 非无效)自闭合标记在HTML5中是否有效? , I have got a very simple question: ,我有一个非常简单的问题:

"Is there a reason why it would be good to serialise HTML5 as XML?" “有理由将HTML5序列化为XML很好吗?”

I understand: 我明白:

  • the differences between HTML, XHTML, XML and HTML5 HTML,XHTML,XML和HTML5之间的区别
  • that it's good practice to nest elements correctly, use lower case letters, quote attributes, close tags etc... 正确嵌套元素,使用小写字母,引号属性,关闭标签等是一种很好的做法...
  • that HTML5 has no DTD while XHTML has got a DTD and an XML parse HTML5没有DTD,而XHTML有DTD和XML解析
  • that if I specify a page as XHTML5 (HTML5 doctype + XHTML schema) some browsers might not process the page for minor errors etc... 如果我将页面指定为XHTML5(HTML5 doctype + XHTML架构),则某些浏览器可能不会处理该页面而出现小错误等。

So the question is: 所以问题是:

"In which case would it be good to follow very strict XML rules when doing an HTML5 page?" “在哪种情况下,制作HTML5页面时遵循非常严格的XML规则会很好吗?”

Above all when it comes to things like 最重要的是

1) The void elements 1)无效元素

<img src="asdsad.jpg" /> compared with <img src="asdsad.jpg">
<area> compared with <area />
<meta> comparted with <meta/>

2) Checked, download etc.. like 2)检查,下载等。喜欢

<input type="checkbox" name="vehicle" value="Car" checked> VS
<input type="checkbox" name="vehicle" value="Car" checked="checked" />

Should I just write following the HTML5 standard and implement as much common sense as possible (lower case, good nesting) or is there A GOOD REASON for a standard company website to be coded in XHTML5? 我应该遵循HTML5标准编写并实现尽可能多的常识(小写,良好的嵌套),还是将XHTML5编码为标准公司网站的合理原因?

I'd say it's mostly about errors. 我想说的主要是关于错误。 If you always write perfect HTML, it really doesn't matter which mime type+syntax pair you choose. 如果您始终编写完美的HTML,那么选择哪个哑剧类型+语法对都没关系。

When a page might contain errors though, each syntax has alternative benefits 但是,当页面可能包含错误时,每种语法都有其他好处

The regular HTML syntax (served as text/html) means using a parser that will try to make the best of your errors. 常规的HTML语法(用作text / html)意味着使用一种解析器,该解析器将尝试最大程度地利用您的错误。 Your content will get rendered somehow, and in many cases, in the way that you intended. 在某些情况下,您的内容将以您想要的方式呈现。 However, when it doesn't, debugging it can be made more difficult by the surprising fix-ups that the parser can make. 但是,如果不这样做,则解析器可以进行令人惊讶的修复,从而使调试变得更加困难。

With the XHTML syntax (served as application/xhtml+xml) the opposite is true. 使用XHTML语法(用作application / xhtml + xml),则相反。 If you make a syntax error, the parser will just stop. 如果出现语法错误,解析器将停止。 In a browser, you'll see either an error message or just the content up to the point where the syntax error was detected. 在浏览器中,您会看到一条错误消息或仅显示到检测到语法错误为止的内容。 Other kinds of errors though, can be easier to debug because the XML parser won't monkey about with your elements trying to fix the mistake. 但是,其他类型的错误则更易于调试,因为XML解析器不会费劲地尝试尝试解决该错误的元素。

To give an example, suppose you have 举个例子,假设你有

<style> td { font-weight:bold } </style>
<table>
  <tr>
  <td>
  <span>First</span>
  </td>
  <span>Second</span>
  </tr>
</table>

That's an HTML content model error because the second span is not in a td element. 这是HTML内容模型错误,因为第二个跨度不在td元素中。 But it's not an XHTML syntax error (ie it's XML well-formed), so the XML parser won't stop on it. 但这不是XHTML语法错误(即XML格式正确),因此XML解析器不会就此停止。

If you use HTML syntax & mime type, what you'll see in a browser is 如果您使用HTML语法和mime类型,则在浏览器中将看到

Second 第二
First 第一

because the HTML parser will move the second span completely out of the table. 因为HTML解析器会将第二个跨度完全移出表格。 In more complicated tables, it can be hard to work out how that order has come about. 在更复杂的表中,可能很难确定该顺序是如何产生的。

If you use the XHTML syntax and mime type, what you'll see in a browser is 如果您使用XHTML语法和mime类型,则在浏览器中将看到

First Second 第一第二

If you then wonder why "Second" isn't in bold, you can find the appropriate markup more easily because everything's displayed in the order it's placed in the markup. 如果然后您想知道为什么“ Second”没有以粗体显示,您会更容易找到合适的标记,因为所有内容均按其在标记中的放置顺序显示。

In addition to Alohci's excellent answer, let me run by your bullet points briefly... 除了Alohci的出色回答外,让我简要介绍您的要点...

I understand: 我明白:
(..) (..)

  • that it's good practice to nest elements correctly, use lower case letters, quote attributes, close tags etc... 正确嵌套元素,使用小写字母,引号属性,关闭标签等是一种很好的做法...

Quoting attributes is always a good idea, as well as having no errors, but the rest really doesn't matter. 引用属性始终是一个好主意,并且没有错误,但是其余的实际上并不重要。 Sure, it's fashionable these days to write tags and attributes in lowercase, but that by itself doesn't make it good practice. 当然,这些天来以小写形式编写标签和属性已经很流行了,但这本身并不是一种好的做法。 Not that uppercase is better, but I'm just saying. 不是说大写更好,而是我只是说。 By the way, tag names are stored by the browser in the DOM as uppercase in HTML. 顺便说一句,标记名称由浏览器在HTML中以大写形式存储在DOM中。

  • that HTML5 has no DTD while XHTML has got a DTD and an XML parse HTML5没有DTD,而XHTML有DTD和XML解析

Well, XHTML5 doesn't have a DTD. 好吧,XHTML5没有DTD。 It does use the XML parser, and that means you can't use entity references such as &nbsp; 它确实使用了XML解析器,这意味着您不能使用&nbsp;类的实体引用&nbsp; . If you want to use references with XHTML, use numerical references like &#160; 如果要在XHTML中使用引用,请使用&#160;等数字引用&#160; or revert to an older version that has a DTD. 或还原为具有DTD的旧版本。

  • that if I specify a page as XHTML5 (HTML5 doctype + XHTML schema) some browsers might not process the page for minor errors etc... 如果我将页面指定为XHTML5(HTML5 doctype + XHTML架构),则某些浏览器可能不会处理该页面而出现小错误等。

No browsers tolerate errors in XHTML these days. 如今,没有浏览器可以容忍XHTML中的错误。 There used to be browsers that could treat XHTML as HTML, but those no longer exist. 曾经有一些浏览器可以将XHTML视为HTML,但是已经不再存在。

Typically when using web technologies you want to use json, not xml. 通常,当使用Web技术时,您要使用json,而不是xml。 Json has the same serializing capabilities and uses less characters so it's lighter. Json具有相同的序列化功能,并且使用的字符更少,因此更轻便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM