17

Validation requires that I set it but why? I like to think the code in my xhtml document is doing something.

  • Very similar to [http://stackoverflow.com/questions/5838343/what-does-html-xmlns-http-www-w3-org-1999-xhtml-do] – colinross May 24 '11 at 00:13

3 Answers3

15

From the W3Schools:

the xmlns attribute specifies the xml namespace for a document.

This basically helps to avoid namespace conflicts between different xml documents, if for instance a developer mixes xml documents from different xml applications.

An example of this (also from the W3 website):

XML data to define an html table:

<table>
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

XML data to define information about a coffee table

<table>
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>

There are two table elements here, which would produce a conflict. To fix this, you can add a namespace to signify which piece of information defines an html table and which comprises information about a coffee table:

<root>

<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="http://www.w3schools.com/furniture">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Kyle
  • 4,202
  • 1
  • 33
  • 41
  • 25
    Please don't link to a w3schools page with text "W3 standard". See http://w3fools.com . As is the norm, w3schools is not wholly correct here - it does not specify the namespace for the document - it specifies the namespace of unprefixed elements for the element on which it appears and its descendent elements, except where those descendent elements redefine it. And it only has any effect when it's parsed with an XML parser. – Alohci May 24 '11 at 08:15
  • W3Schools is not the W3C so don't write standards. I've edited the question to remove that claim. – Quentin Jun 03 '11 at 06:17
1

When an XHTML document is served as text/html, as it usually is, the xmlns attribute does nothing.

When an XHTML document is served with an XML content type, the attribute specifies the default namespace of elements. In this case, the practical impact is that if the attribute is omitted, no element has its HTML meaning – all elements are taken as pure XML, which means that they have no special behavior and no default formatting, and the document is rather useless, it isn’t taken as HTML at all.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
1

Imagine that we both defined xlm schemas that include tag foobar. How would you distinguish if both are used in the same document? The answer is - we put them in diferent namespaces. And that's what xmlns is for It's similar to java packages.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49