XML/HTML 标签内的空白

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3314535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:52:30  来源:igfitidea点击:

White space inside XML/HTML tags

htmlxmltags

提问by Channel72

I know how white space is handled in text nodes for XML and HTML, but I'm uncertain about white spaces inside tag elements themselves. Obviously, white spaces are used inside tags to separate attributes, but is it valid to have white spaces after '<' or before '>'?

我知道如何在 XML 和 HTML 的文本节点中处理空格,但我不确定标签元素本身内的空格。显然,在标签内部使用空格来分隔属性,但是在“<”之后或“>”之前有空格是否有效?

For example:

例如:

<  foo  >
< /foo >

Or even:

甚至:

<foo>
< /  foo >

Are these tags valid XML? What about HTML, assuming they were actual HTML tag names?

这些标签是有效的 XML 吗?假设它们是实际的 HTML 标签名称,那么 HTML 呢?

回答by Guffa

The specification (section 3.1 Start-tags, end-tags, and empty-element tags)says that there is no white space between the '<'and the tag name, between '</'and the tag name, or inside '/>'. You can add white space after the tag name, though:

规范(部分3.1起始标签,结束标签和空元素标签)说,存在之间没有空格'<'和标签名,之间'</'和标记名称,或里面'/>'。不过,您可以在标签名称后添加空格:

<foo            >
</foo        >
<bar
/>

回答by Dan Breslau

</and />are tokens, so whitespace between the two characters would be a syntax error. And as Guffa pointed out, whitespace isn't allowed between the opening token and the name. But you're fine adding whitespace between the element tag and the closing >(or />) token.

<//>是标记,因此两个字符之间的空格将是语法错误。正如Guffa 指出的那样,开头标记和名称之间不允许有空格。但是您可以在元素标记和结束>(或/>)标记之间添加空格。

EDITto reflect Guffa's correct citing of the XML specification.

编辑以反映 Guffa 对 XML 规范的正确引用。

回答by sisu

The HTML[5] standard appears to agree exactly with the XML standard, as described by Guffa...

正如 Guffa 所描述的,HTML[5] 标准似乎与 XML 标准完全一致...

http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#start-tags

http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#start-tags

回答by Sean Tank Garvey

You can't start with a space, but you can (optionally) end with one (before the second angle bracket, that closes the first tag).

您不能以空格开头,但可以(可选)以空格结尾(在第二个尖括号之前,即关闭第一个标签)。