【问题标题】:What has an namespace to do with XML?命名空间与 XML 有什么关系?
【发布时间】:2010-02-10 15:30:06
【问题描述】:

这里有些让我困惑:

NSXMLParser 方法有一个 namespaceURI 属性:

- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName

从文档中我无法弄清楚“命名空间”是什么意思。有人可以举个例子解释一下 XML 中的命名空间是什么以及我为什么想要它?

编辑:是的,我注意到了维基百科。但这又令人困惑。将一个单一的命名空间声明放在 XML 文件的顶部有什么意义,比如

xmlns:xhtml="http://www.w3.org/1999/xhtml"

??同样,这只是零意义。 Wikipedia 也没有有用的示例来获取它,为什么我真的想要命名空间以及 - 更重要的是 - 它在 XML 文件中的外观。他们说这是为了解决多个同名元素(如 ID)的歧义,但没有例子说明多个命名空间如何解决这个问题。

【问题讨论】:

    标签: iphone xml namespaces nsxmlparser


    【解决方案1】:

    XML 命名空间与其他任何地方的命名空间一样工作。

    它们提供了一种唯一区分同名元素或属性的方法。这是通过声明命名空间 URI 并可选地为节点名称附加前缀来完成的。此前缀(可选)与命名空间声明一起定义。

    <!-- node without any namespace (it's in the default namespace) -->
    <node>
      <child /><!-- descendants are in the parent namespace by default -->
    </node>
    
    <!-- node with explicit default namespace -->
    <node xmlns="http://some/namespace/uri/">
      <child /><!-- descendants are in the parent namespace by default -->
    </node>
    
    <!-- NS declaration with prefix (node is still in the default namespace!) -->
    <node xmlns:prefix="http://some/namespace/uri/">
      <child /><!-- descendants are in the parent's namespace -->
      <prefix:child><!-- explicit namespace assignment by prefix -->
        <grandchild /><!-- prefixes don't propagate, this is in the default namespace! -->
      </prefix:child>
    </node>
    

    命名空间是严格限定范围的。它们仅对节点及其后代可用。要在整个 XML 文档中使用名称空间,必须在顶级元素(文档元素)中声明它。

    在您的情况下,在示例中或&lt;prefix:child /&gt;

    didEndElement = "child"
     namespaceURI = "http://some/namespace/uri/"
    qualifiedName = "prefix:child"
    

    【讨论】:

    • 澄清一下:在 中,child 仍然有默认命名空间,而不是链接到前缀的命名空间。
    • @Bart:非常正确,我会更改描述。
    【解决方案2】:
    【解决方案3】:

    命名空间用于限定元素名称,前缀用于简化 XML。默认情况下,有一个带有“空”uri 的命名空间。

    扩展 Tomalaks 示例:

    <!-- Mixing namespaces default namespace -->
    <node xmlns:gc="http://other/namespace/gc"> <!-- node is in default NS -->
      <child> <!-- child is still in default NS, inherited from parent - node -->
        <gc:grandchild/>  <!-- grandchild is in the "http://other/namespace/gc", because of gc prefix -->
      </child>
      <child xmlns="http://some/namespace/uri/" xmlns:gc2="http://other/namespace/gc"> <!-- node is in "http://home/namespace/uri" because of declration -->
        <gc2:grandchild/> <!-- again, in "http://other/namespace/gc", despite different prefix -->
        <grandchild/> <!-- yet this one is in "http://home/namespace/uri", no prefix, inherited from parent -->
      </child>
    </node>
    

    这里的要点是两个“子”节点都是不同的节点。它们看起来相同,但它们的“完全限定名称”不同”。这在 XSL 中尤为重要。

    孙辈也是如此。 gc:grandchild 和 gc2:grandchild 是相同的,它们共享相同的命名空间 URI,但使用不同的前缀。第三个孙节点不同,因为它使用从父子节点继承的命名空间。

    命名空间很重要,因为它们可以让您“混合和匹配”来自不同词汇表的单个文档中的 xml。例如,在 XHTML 文档中嵌入 SVG 标记。使用命名空间可以防止相同的基本节点名称重叠。

    对于大多数简单的 XML 问题,它们是不必要的。但是当您开始合并标准等时,它们变得非常重要。

    另一个例子是在 Web 服务的 SOAP 信封中嵌入 SAML 断言。 SOAP 是一种标准,SAML 是另一种标准——独立的标准和标准体,但两者都允许在其文档中插入“任意 XML”区域。命名空间将内容分开。

    【讨论】:

      【解决方案4】:

      Namespaces 在 XML 中很重要,因为您可能在同一 xml 层次结构中拥有具有相同 local-name 的节点。

      想象一个描述期刊文章的 xml 文档。您可能有一个定义文章作者的元素:

      <article>
        <head>
          <author>Emily Berthenstien</author>
        </head>
      ....
      

      在文章的下方,您可能会引用列出已被引用的作者的引文。

      <citations>
        <citation>
          <author>Bernard Rightofen</author>
        </citation>  
      </citations>       
      

      你如何区分这 2 个“作者”节点?

      将不同的命名空间添加到头部的“作者”元素和引用节点将为每个作者节点创建一个QName,然后您可以使用它来专门访问它们(使用 XPath 或其他)而不是获取使困惑。例如,您现在可以只选择使用 XPath 及其 QName 的引文作者:

      declare namespace citauths="http://citation/authors/only"
      
      //citauths:author
      

      只有文章作者:

      declare namespace auths="http://article/authors/only"
      
      //auths:author
      

      【讨论】:

        猜你喜欢
        • 2020-12-16
        • 1970-01-01
        • 2010-09-12
        • 2020-07-27
        • 1970-01-01
        • 2014-07-27
        • 1970-01-01
        • 2014-12-28
        • 1970-01-01
        相关资源
        最近更新 更多