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.