質問

I have this piece of code:

<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>

My question is that which namespaces does foo, bar and bas belongs? and which namespaces does the qux, quux and forge belongs?

I think that foo belongs to a and bar belongs to b, but I don't know if that is correct?

役に立ちましたか?

解決

Your code sample seems very confusing. Namespaces are a fundamental concept in XML. If you are not familiar with namespaces, please take time to learn and understand them.

Some general notes:

  • xmlns="something" is a namespace declaration - it is not an attribute,
  • do not confuse namespace prefixes with namespace URIs, they are separate,
  • do not confuse namespace prefixes with non-prefixed element or attribute names, they are separate,
  • elements and also attributes can belong to a namespace,
  • do not use xmlns as element name,
  • if you use a prefix, it must be bound to a namespace URI that is within the scope

Answers to your questions are below embedded as comments to your sample code.

<!-- 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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top