管理 XML 文档中的命名空间Managing Namespaces in an XML Document

XML 命名空间将 XML 文档中的元素和属性名称与自定义和预定义的 URI 关联起来。XML namespaces associate element and attribute names in an XML document with custom and predefined URIs. 要创建这些关联,您应为命名空间 URI 定义前缀,并使用这些前缀来限定 XML 数据中的元素和属性名称。To create these associations, you define prefixes for namespace URIs, and use those prefixes to qualify element and attribute names in XML data. 命名空间可防止元素和属性名称冲突,并允许以不同方式处理和验证同名的元素和属性。Namespaces prevent element and attribute name collisions, and enable elements and attributes of the same name to be handled and validated differently.

声明命名空间Declaring namespaces

若要对某个元素声明命名空间,请使用 xmlns: 属性:To declare a namespace on an element, you use the xmlns: attribute:

xmlns:<name>=<"uri">

其中,<name> 是命名空间前缀,<"uri"> 是用于标识命名空间的 URI。where <name> is the namespace prefix and <"uri"> is the URI that identifies the namespace. 在声明前缀后,可使用该前缀来限定 XML 文档中的元素和属性,并将它们与命名空间 URI 相关联。After you declare the prefix, you can use it to qualify elements and attributes in an XML document and associate them with the namespace URI. 因为命名空间前缀在整个文档中使用,所以它的长度应较短。Because the namespace prefix is used throughout a document, it should be short in length.

本示例定义两个 BOOK 元素。This example defines two BOOK elements. 第一个元素由前缀 mybook 限定,第二个元素由前缀 bb 限定。The first element is qualified by the prefix, mybook, and the second element is qualified by the prefix, bb. 每个前缀都与不同的命名空间 URI 相关联:Each prefix is associated with a different namespace URI:

<mybook:BOOK xmlns:mybook="http://www.contoso.com/books.dtd">  
<bb:BOOK xmlns:bb="urn:blueyonderairlines" />
</mybook>

若要指示某个元素是特定命名空间的一部分,请向其添加命名空间前缀。To signify that an element is a part of a particular namespace, add the namespace prefix to it. 例如,如果 Author 元素属于 mybook 命名空间,则将其声明为 <mybook:Author>For example, if a Author element belongs to the mybook namespace, it is declared as <mybook:Author>.

声明范围Declaration scope

命名空间在从声明点开始直至声明它的元素结束这一范围内有效。A namespace is effective from its point of declaration until the end of the element it was declared in. 在此示例中,在 BOOK 元素中定义的命名空间不适用于 BOOK 元素外部的元素(例如,Publisher 元素):In this example, the namespace defined in the BOOK element doesn't apply to elements outside the BOOK element, such as the Publisher element:

<Author>Joe Smith</Author>  
<BOOK xmlns:book="http://www.contoso.com">  
    <title>My Wonderful Day</title>  
      <price>$3.95</price>  
</BOOK>  
<Publisher>  
    <Name>MSPress</Name>  
</Publisher>  

必须先声明命名空间,然后才可以使用它,但命名空间不一定出现在 XML 文档的顶部。A namespace must be declared before it can be used, but it doesn't have to appear at the top of the XML document.

当您在 XML 文档中使用多个命名空间时,可以将一个命名空间定义为默认命名空间以产生外观更整洁的文档。When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. 默认命名空间是在根元素中声明的,它适用于文档中的所有非限定元素。The default namespace is declared in the root element and applies to all unqualified elements in the document. 默认命名空间只适用于元素,不适用于属性。Default namespaces apply to elements only, not to attributes.

若要使用默认命名空间,请在该元素的声明中省略前缀和冒号:To use the default namespace, omit the prefix and the colon from the declaration on the element:

<BOOK xmlns="http://www.contoso.com/books.dtd">  
...
</BOOK>

管理命名空间Managing namespaces

XmlNamespaceManager 类存储命名空间 URI 及其前缀的集合,您可在此集合中查找、添加和删除命名空间。The XmlNamespaceManager class stores a collection of namespace URIs and their prefixes, and lets you look up, add, and remove namespaces from this collection. 在某些上下文中,需要使用此类以获得更佳 XML 处理性能。In certain contexts, this class is required for better XML processing performance. 例如,XsltContext 类使用 XmlNamespaceManager 以获得 XPath 支持。For example, the XsltContext class uses XmlNamespaceManager for XPath support.

命名空间管理器对命名空间不执行任何验证,而是假定前缀和命名空间已经过验证并符合 W3C 命名空间规范。The namespace manager doesn't perform any validation on the namespaces, but assumes that prefixes and namespaces have already been verified and conform to the W3C Namespaces specification.

备注

C#Visual Basic 中的 LINQ TO XML 不使用 XmlNamespaceManager 来管理命名空间。LINQ TO XML in C# and Visual Basic don't use XmlNamespaceManager to manage namespaces. 若要了解如何在使用 LINQ to XML 时管理命名空间,请参阅 LINQ 文档中的使用 XML 命名空间 (C#)使用 XML 命名空间 (Visual Basic)See Working with XML Namespaces (C#) and Working with XML Namespaces (Visual Basic) in the LINQ documentation for information about managing namespaces when using LINQ to XML.

下面是可使用 XmlNamespaceManager 类执行的一些管理和查找任务。Here are some of the management and lookup tasks you can perform with the XmlNamespaceManager class. 有关更多信息和示例,请遵循指向每个方法或属性的引用页的链接。For more information and examples, follow the links to the reference page for each method or property.

功能To 使用Use
添加命名空间Add a namespace AddNamespace 方法AddNamespace method
移除命名空间Remove a namespace RemoveNamespace 方法RemoveNamespace method
查找默认命名空间的 URIFind the URI for the default namespace DefaultNamespace 属性DefaultNamespace property
查找命名空间前缀的 URIFind the URI for a namespace prefix LookupNamespace 方法LookupNamespace method
查找命名空间 URI 的前缀Find the prefix for a namespace URI LookupPrefix 方法LookupPrefix method
获取当前节点中命名空间的列表Get a list of namespaces in the current node GetNamespacesInScope 方法GetNamespacesInScope method
限定命名空间的范围Scope a namespace PushScopePopScope 方法PushScope and PopScope methods
检查是否在当前范围内定义了前缀Check whether a prefix is defined in the current scope HasNamespace 方法HasNamespace method
获取用于查找前缀和 URI 的名称表Get the name table used to look up prefixes and URIs NameTable 属性NameTable property

请参阅See also