我有这段代码:
<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,但我不知道这是否正确?
答案 0 :(得分:0)
您的代码示例似乎非常令人困惑。命名空间是XML中的基本概念。如果您不熟悉名称空间,请花些时间学习和理解它们。
一些一般性说明:
xmlns="something"
是名称空间声明 - 它不是属性,xmlns
作为元素名称,您的问题的答案在下面作为对示例代码的评论嵌入。
<!-- 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>