White space inside XML/HTML tags

HtmlXmlTags

Html Problem Overview


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 '>'?

For example:

<  foo  >
< /foo >

Or even:

<foo>
< /  foo >

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

Html Solutions


Solution 1 - Html

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:

<foo            >
</foo        >
<bar
/>

Solution 2 - Html

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

EDIT to reflect Guffa's correct citing of the XML specification.

Solution 3 - Html

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

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

Solution 4 - Html

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChannel72View Question on Stackoverflow
Solution 1 - HtmlGuffaView Answer on Stackoverflow
Solution 2 - HtmlDan BreslauView Answer on Stackoverflow
Solution 3 - HtmlsisuView Answer on Stackoverflow
Solution 4 - HtmlSean Tank GarveyView Answer on Stackoverflow