About
namespace in XML 1) are used to be able to have the same node name but in different namespace.
Example
A simple example would be to consider an XML document that contained references to:
- a customer
- and an ordered product.
Both the customer element and the product element could have a child element named id (for identifier).
References to the id element would therefore be ambiguous; placing them in different namespaces would remove the ambiguity.
<xml xmlns:c="https://domain.com/customer" xmlns:p="https://domain.com/product">
<c:id>The customer id</c:id>
<p:id>The product id</p:id>
</xml>
Usage
XML namespaces are used mainly for providing uniquely named elements and attributes in an XML document.
For a schema vocabulary, the XML namespace provides a naming context for:
- named type definitions,
- named model groups,
- global element declarations
- and global attribute declarations
The namespace mechanism defines custom namespaces in which the non-standard elements and attributes are supported.
Syntax
A namespace name is a uniform resource identifier (URI) and is declared using the reserved XML pseudo-attribute:
- xmlns: to define the default namespace
- or xmlns:name: to define a namespace where name is the unique namespace identifier (called also a prefix).
For example, the following JSF tag library declaration maps the h prefix to the http://java.sun.com/jsf/html namespace uri.
<xml xmlns:h="http://java.sun.com/jsf/html" >
http://java.sun.com/jsf/html doesn't represent a web page, it's just an unique identifier
Default
The default namespace of a document is defined without the name.
Example for an XHTML document
<html xmlns="http://www.w3.org/1999/xhtml">