If you find this helpful, please click the Google Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.) |
What are namespaces in HTML (and XML)?
A namespace is an attribute associated with the name of some object, such as a node in an HTML document, that distinguishes the type of object represented by the name from other types of objects with the same name in other namespaces. For example, in
<head><title>Heading Title</title></head>
title
is the name of an element (the title element) in HTML while in
<a href="..." title="link title" ...>
title
is the name of a title attribute on an HTML <a> tag.
The HTML namespaces (plural) in general are the collection of various namespaces in HTML code. The HTML namespace (singular) itself is the one associated with the namespace URI http://www.w3.org/1999/xhtml
.
On this site, the notation <a href>
refers to the href
attribute in the html:a
element namespace, where html:
is the namespace prefix for the HTML namespace.
See the HTML Namespace Tutorial for more information.
Why are there namespaces in HTML 5 (and xHTML)?
Names that are spelled the same and have the same namespace cannot be distinguished from one another without knowing the context in which they appear. For example, in the RSS version 2.0 and OPML version 2.0 code below, the three <title>
elements with the same name in the same namespace partition (the element partition without a namespace URI in this case) all identify the same type of object:
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> <channel> <title>Feed Title</title> ... <item> <title>Item Title</title> ... </item> </channel> </rss> <?xml version="1.0" encoding="UTF-8"?> <opml version="1.0"> <head> <title>My OPML</title> </head> <body> ... </body> </opml>
In order to differentiate the title
elements above you would have to know that the first one was a descendant of an rss
element and channel
element; the second was a descendant of an rss
element, channel
element and item
element; and the third was a descendant of an opml
element and a head
element. This type of confusion in XML content without namespaces makes it very difficult to aggregate that content into other types of content, even if the other content formats do support namespaces. For example, if portions of the content above was included in an HTML document (without overriding the namespace with xmlns=""
), the title
elements would be treated as a valid HTML titles, the head
element and body
element would be treated as duplicates of their HTML counterparts and most of the other elements would be treated as invalid HTML code.
Note that the HTML title
element would not be confused with the title
elements shown above, since its name is in a different namespace partition associated with the HTML namespace URI.