关于xml的命名空间(转自国外一blog)
2006-08-11 19:50
218 查看
A Short Explanation of XML Namespaces
I wrote this up in response to a question on the Axis User discussion list, but thought others might appreciate it.The purpose of a namespace qualification is to disambiguate two
components of the same name. For example, if you have multiple schemas
that each defines an element called "foo", how do you tell them apart?
[code] <s:schema xmlns:s="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:example:foo:1"> <s:element name="foo" type=x:string"/> </s:schema> <s:schema xmlns:s="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:example:foo:2"> <s:element name="foo" type=x:int"/> </s:schema> <s:schema s="http://www.w3.org/2001/XMLSchema" xmlns:ns1="urn:example:foo:1" xmlns:ns2="urn:example:foo:2" targetNamespace="urn:example:foo:0"> <s:import namespace="urn:example:foo:1"/> <s:import namespace="urn:example:foo:2"/> <s:element name="foo"> <s:complexType> <s:sequence> <s:element ref="ns1:foo"/> <s:element ref="ns2:foo"/> </s:sequence> </s:complexType> </s:element> </s:schema>
An instance document of this element could look like this:
[code] <ns0:foo xmlns:ns0="urn:example:foo:0" xmlns:ns1="urn:example:foo:1" xmlns:ns2="urn:example:foo:2"> <ns1:foo>some string</ns1:foo> <ns2:foo>12345</ns2:foo> </ns0:foo>
This instance is equally valid (but less readable):
[code] <tns:foo xmlns:tns="urn:example:foo:0"> <tns:foo xmlns:tns="urn:example:foo:1"> some string</tns:foo> <tns:foo xmlns:tns="urn:example:foo:2"> 12345</tns:foo> </tns:foo>
In other words, the string used for the prefix is irrelevant -- what
matters is the namespace it's been assigned to and the scope of the
namespace declaration. A namespace declaration applies to the element
it's defined in and all that element's children, but you can always
reassign the prefix to a different namespace in a child element.
The default namespace declaration (e.g., xmlns="urn:example:foo:0")
says that all non-explicitly qualified elements belong to the default
namespace. I generally recommend avoiding use of the default namespace
-- especially if you have unqualified elements -- because that forces
you to override the default namespace (e.g., xmlns="") on all
unqualified elements.
So for example, let's say I have this schema:
[code] <s:schema xmlns:s="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:example:foobar"> <s:element name="foobar"> <s:complexType> <s:sequence> <s:element name="foo" type="s:string"/> <s:element name="bar" type="s:string"/> </s:sequence> </s:complexType> </s:element> </s:schema>
Because the schema does not specify elementFormDefault="qualified",
all local elements ("foo" and "bar") are unqualified. A valid instance
of this schema is:
[code] <tns:foobar xmlns:tns="urn:example:foobar"> <foo>some string</foo> <bar>another string</bar> </tns:foobar>
But the following instance is not valid because "foo" and "bar" must be unqualified:
[code] <foobar xmlns="urn:example:foobar"> <foo>some string</foo> <bar>another string</bar> </foobar>
This instance is valid:
[code] <foobar xmlns="urn:example:foobar"> <foo xmlns="">some string</foo> <bar xmlns="">another string</bar> </foobar>
相关文章推荐
- .net关于xml文件命名空间的解析读取
- 关于xml schame出现的命名空间错误
- 关于xml命名空间的——xmlns:f="http://www.name.com.cn/sub",一些疑问
- .net关于xml文件命名空间的解析读取
- 关于XML命名空间
- 关于XML中的名称空间
- [XML]学习笔记(四)——命名空间
- 关于php规范psr4与psr0命名空间的理解
- 关于头文件和命名空间
- xml-了解 XML 命名空间
- 由“Jasperrpeorts 4.1.2升级到5.1.2对flex项目的解析”到AS3 带命名空间的XML的操作
- Xpath dom4j解析带有命名空间的XML的方法
- 关于反射中Assembly.Load("程序集").CreateInstance("命名空间.类")与Activator.CreateInstance()方法
- 关于soap命名空间的问题(转)
- 关于javascript的命名空间[转]
- jaxb解析xml忽略命名空间
- java 在dom4j中,使用XPath处理XML(主要对命名空间的处理)
- web.xml开头<web-app>中的xmlns、xsi、schemaLocation、xsd等,命名空间
- xml处理命名空间
- XML配置文件的命名空间