17

I'm having problems finding out what the valid characters are for an HTML tag.

In this (Is the at-sign (@) a valid HTML/XML tag character?) answer it says:

After another look at the XML Specification:

A tag consists of:

'<' Name (S Attribute)* S? '>' A Name consists of:

NameStartChar (NameChar)* A NameStartChar consists of:

":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] A NameChar consists of:

NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040] The @ sign is U+0040

So the @ sign is not valid in a NameChar or a NameStartChar, and thus not valid in a Name.

But on the HTML syntax page (http://www.w3.org/TR/html-markup/syntax.html#tag-name) it only says:

Tag names are used within element start tags and end tags to give the element’s name. HTML elements all have names that only use characters in the range 0–9, a–z, and A–Z.

What are the valids characters in an HTML tag name? And are there differences between HTML4, XHTML and HTML5?

Community
  • 1
  • 1
Matteo Pagliazzi
  • 5,140
  • 12
  • 49
  • 83
  • 3
    Valid HTML characters are those that are mentioned in the HTML specification. Valid XML characters are those mentioned in the XML specification. What's the confusion? – JJJ Mar 03 '13 at 11:44
  • 20
    This is why I really dislike StackOverflow: Top search result on Google but a closed question with non-answers because some mods decided to shut down the community (or potential community). Custom HTML tags are now a thing and the official draft W3C spec says that the *XML* list of characters for tag names is now valid for HTML! Closing legit questions really irks me and shows why I don't participate here very often. For example, the question makes sense when writing a [universal-ish tag parser](https://github.com/cubiclesoft/ultimate-web-scraper/blob/master/support/tag_filter.php). – CubicleSoft Feb 08 '18 at 15:29
  • 6
    Fully agree with @CubicleSoft - I've seen so many valid questions closed for the most stupid reasons lately. As StackOverflow developped, as it happens with every entity, it became stuck in some rigid "rules" that do more harm than good to the community. If everyone in human history was stuck in such rigid parameters, we wouldn't have exited the stone age yet. For a programmer, it is *paramount* to think outside the box in order to progress, but as soon as entities start to become bigger, they tend to always forget what made them great in the first place, and end up in the history's dustbin. – Yin Cognyto Mar 08 '19 at 15:11
  • 1
    "It's difficult to tell what is being asked here" might have been true for the original question, but now it's crystal clear, valid, and fits the site's format. @CubicleSoft 's answer is what I came here for. – John Frazer Jan 30 '20 at 11:37
  • 1
    Here is some info about custom element names: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name – donquixote Jun 18 '22 at 19:54

1 Answers1

-1

HTML documents must fit HTML specification. XML documents must fit XML specification. XHTML documents must fit XML and HTML specification.

So for XHTML docments you must take the intersection of valid characters from both specifications. Since XHTML is based on XML the specification it cannot allow characters that are forbidden in XML. Therefore the allowed characters in XHTML are always a subset of the ones from XML. So the allowed characters are that from the XHTML specification.

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
  • XHTML is not just a XML-formatted HTML document. W3C defines specific specs for XHTML. As a side-note: there is no XHTML spec corresponding to HTML5 – xorcus Mar 17 '15 at 08:30
  • 1
    @xorcus That's right and I never stated something like `XHTML is not just a XML-formatted HTML document.`. I think you misunderstood `XHTML documents must fit XML and HTML specification.`. I am NOT saying that this alone is sufficient. – Fabian Barney Mar 17 '15 at 10:31
  • 1
    Some examples, please? – zrajm Mar 19 '19 at 16:43