XML namespaces
XML namespaces provide a way to avoid element name conflicts.
Naming Conflicts
In XML, element names are defined by the developer. When two different documents use the same element name, a name conflict occurs.
This XML carries information about an HTML table:
| Apples | Bananas |
This XML document carries information about a table (a piece of furniture):
If these two XML documents are used together, since both documents contain elements with different content and definitions,
an XML parser cannot determine how to handle such a conflict.
Using prefixes to avoid name conflicts
Name conflicts in XML can be easily avoided by using a name prefix.
This XML carries information about an HTML table and a piece of furniture:
In the example above, there is no conflict, because the two
XML Namespaces - the xmlns Attribute
When prefixes are used in XML, a so-called namespace for the prefixNamespacingmust be defined.
A namespace is defined in the xmlns attributeof an element's start tag.
The syntax for a namespace declaration is as follows. xmlns:Prefix="URI"。
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3cschool.cc/furniture"
In the example above,
When a namespace is defined in an element's start tag, all child elements with the same prefix are associated with the same namespace.
A namespace can be declared in the element where it is used or in the XML root element:
xmlns:h="http://www.w3.org/TR/html4/"
**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 as a pointer to an actual web page that contains information about the namespace. Please visit http://www.w3.org/TR/html4/。 Uniform Resource Identifier(URI) is a string of characters that identifies an Internet resource. The most commonly used URI is the one that identifies an Internet domain address,Uniform Resource Locator(URL). Another less commonly used URI isUniform Resource Name (URN)(URN)。 In our examples, we only use URLs. Defining a default namespace for an element saves us from using prefixes in all child elements. Its syntax is as follows: xmlns=" namespaceURI " This XML carries information about an HTML table: This XML carries information about a piece of furniture: XSLT is an XML language used to transform XML documents into other formats, such as HTML. In the XSLT document below, you can see that most of the tags are HTML tags. Non-HTML tags all have the prefix xsl and are identified by this namespace: xmlns:xsl="http://www.w3.org/1999/XSL/Transform": xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> If you want to learn about XSLT, please look for theHomeXSLT tutorial on our site.
xmlns:f="http://www.w3cschool.cc/furniture"
Uniform Resource Identifier (URI)
Default namespaces
The xmlns attribute of the tag defines the qualified namespaces for the h: and f: prefixes.
Apples
Bananas
Namespaces in real use
My CD Collection
Title
Artist