XML Namespaces
- Previous Page XML Attributes
- Next Page XML Display
XML namespaces provide a method to avoid element naming conflicts.
Naming Conflicts
In XML, element names are defined by developers. Naming conflicts occur when two different documents use the same element name.
This XML document carries information from a table:
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
This XML document contains 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, naming conflicts will occur due to the fact that both documents contain <table> elements with different content and definitions.
Users or XML applications will not be able to determine how to handle such conflicts.
Using prefixes to resolve naming conflicts
Using name prefixes can easily avoid naming conflicts in XML.
This XML contains information about HTML tables 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>
Now, there is no naming conflict, which is due to the fact that both documents use different names to name their <table> elements (<h:table>
and <f:table>
)。
By using prefixes, we created two different types of <table> elements.
XML Namespace - xmlns Attribute
在 XML 中使用前缀时,必须定义前缀的命名空间(Namespaces)。
命名空间可以通过元素开始标记中的 xmlns
属性来定义。
命名空间声明使用这种语法: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="https://www.codew3c.com/furniture"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root>
在上面的例子中:
第一个 <table> 元素中的 xmlns
属性为 h:
前缀提供了限定的命名空间。
第二个 <table> 元素中的 xmlns
属性为 f:
前缀提供了限定的命名空间。
如果为元素定义了命名空间,则相同前缀的所有子元素都与相同的命名空间相关联。
命名空间也可以在 XML 根元素中声明:
<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="https://www.codew3c.com/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>
注意:解析器不会使用名称空间 URI 来查找信息。
使用 URI 的目的只是为命名空间提供唯一的名称。
不过,很多公司常常会作为指针来使用命名空间指向实际存在的网页,这个网页包含关于命名空间的信息。
统一资源标识符 (URI)
统一资源标识符 (Uniform Resource Identifier,URI) 是标识 Internet 资源的字符串。
最常见的 URI 是统一资源定位符 (Uniform Resource Locator,URL),它标识因特网域名地址。
另一种不太常见的 URI 类型是统一资源名称 (Uniform Resource Name,URN)。
Default Namespaces
Defining a default namespace for an element can save us from using prefixes in all child elements.
Please use the following syntax:
xmlns="namespaceURI"
This XML document carries HTML table information:
<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="https://www.codew3c.com/furniture"> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>
Practical application of namespaces
XSLT is a language that can be used to convert XML documents to other formats.
The following XML document is used to convert XML to HTML.
If you observe the following XSL document carefully, you will see that most of the tags are HTML tags.
Non-HTML tags have the prefix xsl, and are indicated by the namespace: "http://www.w3.org/1999/XSL/Transform":
The namespace "http://www.w3.org/1999/XSL/Transform" identifies the XSLT elements in the HTML document:
<?xml version="1.0" encoding="UTF-8"?> <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 border="1"> <tr> <th style="text-align:left">Title</th> <th style="text-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 more about XSLT, please read our XSLT Tutorial.
- Previous Page XML Attributes
- Next Page XML Display