About namespaces
XML namespaces are used to uniquely identify elements and attributes within an XML document to avoid naming conflicts. Namespaces are declared as a Uniform Resource Identifier (URI) and are typically located in the start tag of the root element of an XML document. However, they can also be defined at the node level, where they are valid for that node and all of its descendants. Associating a URI with a namespace ensures that elements with the same name remain distinct.
The Namespaces in XML recommendation, developed by the World Wide Web Consortium (W3C), provides the xmlns attribute to uniquely define a namespace for an XML document. The following is an example of a namespace that is used to define the vocabulary for Extensible Stylesheet Language Transformation (XSLT):
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
A namespace is divided into three sections:
- xmlns keyword The keyword defined by the W3C for identifying XML namespaces. This section is defined first and is separated from the namespace prefix by a colon (:).
- Prefix The abbreviated name of the namespace that is used in declaring all elements and attributes that belong to it. The prefix can be any name other than xmlns or xml, which are reserved words.
- Definition The URI that uniquely defines the namespace. Can also be defined as a URN or a URL.
After a namespace is declared for an XML document, any element or attribute that belongs to that namespace uses the namespace prefix in its declaration. For example, .xslt files use the xsl namespace prefix, as shown in the following example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:output method="html" indent="no"/>
<xsl:template match="myNode:myNodes">
...
</xsl:template>
</xsl:stylesheet>