如何将 XSD 类型导入根架构?
这是我在 foo.xsd
中现有的 XSD 模式,它仅声明类型:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
targetNamespace="foo">
<xs:complexType name="alpha">
<!-- skipped -->
</xs:complexType>
</xs:schema>
这是另一个模式,它声明元素:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
targetNamespace="foo">
<xs:import schemaLocation="foo.xsd" namespace="foo" />
<xs:element name="RootElement" type="alpha"/>
</xs:schema>
这是我从 Java 中的 SAX 解析器得到的内容:
"The namespace attribute 'foo' of an <import> element information
item must not be the same as the targetNamespace of the schema it exists in."
我是什么做错了吗?
This is my existing XSD schema in foo.xsd
, that declares just the type:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
targetNamespace="foo">
<xs:complexType name="alpha">
<!-- skipped -->
</xs:complexType>
</xs:schema>
This is another schema, that declares the element:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
targetNamespace="foo">
<xs:import schemaLocation="foo.xsd" namespace="foo" />
<xs:element name="RootElement" type="alpha"/>
</xs:schema>
This is what I'm getting from SAX parser in Java:
"The namespace attribute 'foo' of an <import> element information
item must not be the same as the targetNamespace of the schema it exists in."
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当指定了涉及的 XSD 的 targetNamespace (tns) 属性且相同时,仅允许 xsd:include(targetNamespace 属性不能将空字符串作为其值)。
然而,可以包括来自具有 tns 的模式 (s2) 的不带 tns 的模式 (s1);最终效果是 s1 组件采用 s2 模式的命名空间。这种用法通常被称为变色龙组合物。
此处提供了描述两者之间差异的 SO 参考。
When the targetNamespace (tns) attributes of the involved XSDs are specified and the same, only xsd:include is allowed (a targetNamespace attribute cannot have empty string as its value).
However, one may include a schema (s1) without a tns from a schema (s2) that has a tns; the net effect is s1 components assume the namespace of the s2 schema. This usage is often referred to as chameleonic composition.
A reference on SO describing the difference between the two, is here.
来自@Petru Gardea,我正在让那些觉得难以理解的人变得更简单。
在根 xsd 中,您想要导入另一个 XSD,只需使用包含标签,如下所示。这里不需要命名空间。
targetNamespace 不是强制性的。
From @Petru Gardea I am making it simpler for people who find it difficult to understand.
In root xsd where you want to import another XSD, Just use include tag as below. namespace not required here.
targetNamespace is not mandatory.