<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 naming conflict will occur because both documents contain <table> elements with different content and definitions.
The XML parser cannot determine how to handle such conflicts.
Naming 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.
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 element's opening tag.
The syntax for 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="//www.w3cschool.cn/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 h: and f: prefixes.
When a namespace is defined in the opening tag of an element, all child elements with the same prefix are associated with the same namespace.
Namespaces can be declared in the element in which they are used or in the XML root element:
<root xmlns:h="http://www.w3.org/TR/html4/"xmlns:f="//www.w3cschool.cn/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: Namespace URIs are not used by the parser to find information.
Its purpose is to give the namespace a unique name. However, many companies often use namespaces as pointers to actual existing web pages that contain information about the namespace.
Please visit http://www.w3.org/TR/html4/.
A Uniform Resource Identifier (URI) is a string of characters that identifies an Internet resource.
The most commonly used URI is a Uniform Resource Locator (URL) used to identify an Internet domain name address. Another less commonly used URI is the Uniform Resource Name (URN).
In our example we only use URLs.
Defining a default namespace for an element saves us the work of using a prefix on all child elements. Its syntax is as follows:
xmlns="namespaceURI"
This XML carries the information of the 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="//www.w3cschool.cn/furniture"><name>African Coffee Table</name><width>80</width><length>120</length></table>
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 are prefixed with xsl and identified by this namespace:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform":<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body><h2>My CD Collection</h2><table><tr><th align="left">Title</th><th align="left">Artist</th></tr><xsl:for-each select=" catalog/cd"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>
If you want to learn about XSLT, find XSLT tutorials on our home page.