Xml 简明教程

XML - Namespaces

Namespace 是唯一名称的集合。命名空间是一种机制,通过它可以将元素和属性名分配给一个组。命名空间由 URI(统一资源标识符)识别。

A Namespace is a set of unique names. Namespace is a mechanisms by which element and attribute name can be assigned to a group. The Namespace is identified by URI(Uniform Resource Identifiers).

Namespace Declaration

命名空间使用保留的属性声明。这样一种属性名必须是 xmlns 或以 xmlns: 开头,如下所示 −

A Namespace is declared using reserved attributes. Such an attribute name must either be xmlns or begin with xmlns: shown as below −

<element xmlns:name = "URL">

Syntax

  1. The Namespace starts with the keyword xmlns.

  2. The word name is the Namespace prefix.

  3. The URL is the Namespace identifier.

Example

命名空间只影响文档中的有限区域。包含声明及其所有后代的元素都在命名空间的作用域中。以下是 XML 命名空间的一个简单示例 −

Namespace affects only a limited area in the document. An element containing the declaration and all of its descendants are in the scope of the Namespace. Following is a simple example of XML Namespace −

<?xml version = "1.0" encoding = "UTF-8"?>
<cont:contact xmlns:cont = "www.tutorialspoint.com/profile">
   <cont:name>Tanmay Patil</cont:name>
   <cont:company>TutorialsPoint</cont:company>
   <cont:phone>(011) 123-4567</cont:phone>
</cont:contact>

在这里,命名空间前缀是 cont ,命名空间标识符(URI)是 www.tutorialspoint.com/profile。这意味着具有 cont 前缀(包括联系元素)的元素名和属性名都属于 www.tutorialspoint.com/profile 命名空间。

Here, the Namespace prefix is cont, and the Namespace identifier (URI) as www.tutorialspoint.com/profile. This means, the element names and attribute names with the cont prefix (including the contact element), all belong to the www.tutorialspoint.com/profile namespace.