YouTip LogoYouTip

Xml Namespaces

XML Namespaces | Rookie Tutorial

XML Namespaces

XML namespaces provide a way to avoid element name conflicts.

Name Conflicts

In XML, element names are defined by developers. A name conflict occurs when two different documents use the same element name.

This XML carries information about an HTML table:

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

This XML document carries information about a table (a piece of furniture):

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

If these two XML documents are used together, a name conflict will occur because both documents contain <table> elements with different contents and definitions.

XML parsers cannot determine how to handle such conflicts.

Avoid Name Conflicts with Prefixes

Name conflicts in XML can be easily avoided by using name prefixes.

This XML carries information about an HTML table and a piece of furniture:

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

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

In the above example, there is no conflict because the two <table> elements have different names.

XML Namespaces - xmlns Attribute

When using prefixes in XML, a so-called namespace for the prefix must be defined.

The namespace is defined in the xmlns attribute of the start tag of the element.

The syntax for a namespace declaration is as follows: xmlns:prefix="URI".

<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.w3cschool.cc/furniture">
    <f:name>African Coffee Table</f:name>
    <f:width>80</f:width>
    <f:length>120</f:length>
  </f:table>

</root>

In the above example, the xmlns attribute of the <table> tag defines the qualified namespace for the prefixes h: and f:.

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.

Namespaces can be declared in the elements where they are used or in the root element of the XML:

<root xmlns:h="http://www.w3.org/TR/html4/"
       xmlns:f="http://www.w3cschool.cc/furniture">

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

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

</root>

Note: The namespace URI is not used by the parser to look up information.

Its purpose is to give the namespace a unique name. However, many companies often use the namespace to point to an actual existing web page that contains information about the namespace.

Please visit http://www.w3.org/TR/html4/.

Uniform Resource Identifier (URI)

A Uniform Resource Identifier (URI) is a string of characters that identifies an Internet resource.

The most commonly used URI is the Uniform Resource Locator (URL), which identifies an Internet domain address. Another less commonly used URI is the Uniform Resource Name (URN).

In our examples, we only use URLs.

Default Namespace

Defining a default namespace for elements allows us to avoid the need to use prefixes in all child elements. Its syntax is as follows:

xmlns="namespaceURI"

This XML carries information about an HTML table:

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

This XML carries information about a piece of furniture:

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

Namespace in Practice

XSLT is an XML language used to transform XML documents into other formats, such as HTML.

In the following XSLT document, you can see that most of the tags are HTML tags.

Non-HTML tags have the prefix xsl, and thus are identified by the namespace: xmlns:xsl="http://www.w3.org/1999/XSL/Transform":



My CD Collection

Title Artist

If you want to learn more about XSLT, please look for the XSLT tutorial on our homepage.

XML Applications

XML CDATA

Coding Plan

Supports mainstream large models such as Doubao, GLM, DeepSeek, Kimi, MiniMax, and official direct supply for stable and reliable service.

Β₯9.9 / month

Immediately Open

Feixiang Star Coding Plan

Includes free model call quotas, DeepSeek, GLM, Kimi, MiniMax, one-stop experience and deployment platform.

Β₯3.9 / month

Immediately Open

Share Notes

Follow WeChat

My Favorites

← Xml CdataXml Parser β†’