为什么要限定实例文档中的全局元素?
当使用 xml-Schema 和命名空间验证 xml 文档时,除了声明命名空间本身之外,实例文档的全局元素还必须以命名空间为前缀来限定它。
人们可能会认为在这种情况下默认名称空间足以限定元素,但 xml-Schema 显然有另一种观点。
这是为什么? xml-Schema 无法处理的默认名称空间有什么问题?
When validating an xml document using xml-Schema and namespaces, the instance document has to have its global element prefixed with a namespace to qualify it, besides declaring the namespace itself.
One would think that a default namespace would suffice in this case to qualify the element, but xml-Schema apparently has another opinion.
Why is that? What is it about default namespaces that the xml-Schema just can't cope with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这是设计用于支持 Chameleon 命名空间并避免 Chameleon 组件的名称冲突。我的意思是,通过不提供命名空间,我们默认采用 Chameleon 设计。但是,当您不提供 targetNamspace 作为默认 namspace 时,您最终会遇到问题和错误。
注意:部分内容摘自 XFront.com
集体开发的一套架构设计指南
以下是您的一般规则:
除了无命名空间模式之外,每个 XML 模式都至少使用两个命名空间 - targetNamespace 和 XMLSchema 命名空间。对于处理这两个命名空间,可以使用三种方法来设计架构:
反之亦然 - 使 targetNamespace 成为默认命名空间,并显式限定 XMLSchema 命名空间中的所有组件。
反之亦然
不要使用默认命名空间 - 显式限定对 targetNamespace 中组件的引用,并显式限定 XMLSchema 命名空间中的所有组件。
不要使用
关于哪种方法是正确的,确实没有最佳实践。首先,这完全取决于设计师。
不过,在我的研究中我发现:
- 限定用于构造模式的组件(模式、元素、复杂类型、序列等)通常不会造成混乱,因为限定符始终相同。也就是说,人们习惯于看到 xsd:schema、xsd:element 等。
我希望这能回答你的问题。
I believe this is by design to support Chameleon Namespace and to Avoid Name Collisions of Chameleon Components. In that I mean that by not providing a namsepace we default to a Chameleon design. However, when you do not provide the targetNamspace as the default namspace you will end up with a problem and error.
Note: Some Content Excerpted from XFront.com
A collectively Developed Set of Schema Design Guidelines
Here are your rules in general:
Except for no-namespace schemas, every XML Schema uses at least two namespaces - the targetNamespace and the XMLSchema namespace. There are three ways to design your schemas, with regards to dealing with these two namespaces:
Make XMLSchema the default namespace, and explicitly qualify all references to components in the targetNamespace.
Vice versa - make the targetNamespace the default namespace, and explicitly qualify all components from the XMLSchema namespace.
Do not use a default namespace - explicitly qualify references to components in the targetNamespace and explicitly qualify all components from the XMLSchema namespace.
There really isn't a best practice with regards to which approach is correct. Primarily, it is all up to the designer.
Though, in my research I have found:
- Qualifying the components that are used to construct a schema (schema, element, complexType, sequence, etc) are typically not a source of confusion since the qualifier is always the same. That is, people get used to seeing xsd:schema, xsd:element, etc.
I hope this answers your question.