XML namespace provides a way to avoid element name conflicts.
Naming conflict
Because the elements used in the XML document are not fixed, two different XML documents use the same name to describe the case of different types of elements. And this situation often leads to a naming conflict. Please see the following two examples
This XML document carries fruit information in the Table element:
Apples TD>
Bananas TD>
TR>
TABLE>
This XML document carries a table information in the table element (furniture, can't eat):
African coffee table name>
80 width>
120 Length>
TABLE>
If the above two XML document pieces happen together, the status of naming conflicts will occur. Because both pieces contain
elements, the definition of these two Table elements is different from those contained.
Use prefix to solve naming conflicts
The following XML document carries information in the Table element:
apples h: td>
Bananas h: TD>
h: TR>
h: Table>
The following XML document carries the information of furniture Table:
African Coffee Table f: Name>
80 f: width>
120 f: Length>
f: Table>
There is no problem with element name conflicts, because the two documents use different prefixes for their respective table elements, and the table element is ( and ).
By using a prefix, we created two different Table elements.
Use namespace
The following XML document carries information in the Table element:
apples h: td>
Bananas h: TD>
h: TR>
h: Table>
The following XML document carries the information of furniture Table:
African Coffee Table f: Name>
80 f: width>
120 f: Length>
f: Table>
In the above two examples, in addition to the prefix, both Table elements use an XMLNS attribute to associate elements and different namespaces together.
Name spatial properties
The namespace properties are typically placed at the beginning of the element, and the syntax is as follows:
XMLns: Namespace-prefix = "namespace"
In the above example, the namespace defines an Internet address:
XMLns: f = "http://www.w3schools.com/furniture" w3c naming specification declared namespace itself is a uniform resource IDTIFIER (URI).
When we use the namespace at the beginning marker of the element, all sub-elements of this element will be related to the same namespace through a prefix.
Note: The network address used to identify the namespace is not called by the XML parser. The XML parser does not need to find information from this network address. The role of the network address is just a unique name for the namespace, so this network address It can also be virtual, but many companies often sympathize this network address value to a real web page, which contains more detailed information about the current namespace. You can access http://www.w3.org/tr/html4/.
Unified resource identifier
A Uniform Resource Identifier (URI) is a string that identifies network resources. The most common URI should be a Uniform Resource Locator (URL). URL is used to identify the address of the network host. On the other hand, another uncommon URI is a universal resource name universal resource name (URN). In our example, usually use URLs.
Since the previous example used the URL address to identify the namespace, we can confident that this namespace is unique.
Default namespace
Define a default XML namespace allows us to use a prefix in the start tag of child elements. His grammar is as follows:
The following XML document contains fruit information in the table element:
Apples TD>
Bananas TD>
TR>
TABLE>
The following XML document contains information about furniture Table:
African coffee table name>
80 width>
120 Length>
TABLE>
Use namespace
When the file starts using XSL, it will find that the namespace is so frequent. The XSL style single is mainly used to convert an XML document into a format similar to the HTML file.
If you look at the XSL document below, you will find that there are many tags that are HTML tags. Those markers are not HTML tags, which is the prefixed XSL, this XSL prefix is identified by namespace "http://www.w3.org/tr/xsl":