【问题标题】:namespaces and attributes with foo, bar带有 foo、bar 的命名空间和属性
【发布时间】:2014-03-23 16:47:54
【问题描述】:

我有这段代码:

<a:foo qux="A" xmlns:a="B" xmlns:b="C">
  <b:bar a:quux="D" xmlns="E"/>
  <xmlns xmlns:a="F" xmlns="G">
    <baz a:corge="H" xmlns:baz="I" xmlns=""/>
  </xmlns>
</a:foo>

我的问题是 foo、bar 和 bas 属于哪些命名空间? qux、quux和forge分别属于哪些命名空间?

我认为foo属于a,bar属于b,但不知道这样说对不对?

【问题讨论】:

    标签: xml-namespaces


    【解决方案1】:

    您的代码示例看起来很混乱。命名空间是 XML 中的一个基本概念。如果您不熟悉命名空间,请花时间学习和理解它们。

    一些一般说明:

    • xmlns="something" 是一个命名空间声明 - 它不是一个属性,
    • 不要将命名空间前缀与命名空间 URI 混淆,它们是分开的,
    • 不要将命名空间前缀与非前缀元素或属性名称混淆,它们是分开的,
    • 元素和属性也可以属于一个命名空间,
    • 不要使用xmlns作为元素名称,
    • 如果使用前缀,则必须将其绑定到范围内的命名空间 URI

    您的问题的答案作为 cmets 嵌入到您的示例代码中。

    <!-- foo belongs to "B", attribute qux has no namespace -->
    <a:foo qux="A" xmlns:a="B" xmlns:b="C">
      <!-- bar belongs to "C", quux belongs to "B" -->
      <b:bar a:quux="D" xmlns="E"/>
      <!-- Using element names that begin with xml is against the 
           XML recommendation. Furthermore, xmlns as element name 
           does not create a namespace definition -->
      <xmlns xmlns:a="F" xmlns="G">
        <!-- baz has no namespace, since this element has an empty 
             default namespace declaration that overrides the one 
             set the parent element.
             corge belongs to "F" because prefix "a" was redefined
             in the parent element -->
        <baz a:corge="H" xmlns:baz="I" xmlns=""/>
      </xmlns>
    </a:foo>
    

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 2012-01-25
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 2019-07-09
      • 2011-03-05
      • 2017-06-23
      • 2011-05-09
      相关资源
      最近更新 更多