如何将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."

我做错了什么?

当指定了相关xsd的targetNamespace(tns)属性并且这些属性相同时,只允许xsd:include(targetNamespace属性的值不能为空字符串)。

然而,可以从具有tns的模式(s2)中包括没有tns的方案(s1);净效果是s1组件采用s2模式的名称空间。这种用法通常被称为变色合成。

这里有一个关于SO的参考文献,描述了两者之间的区别。

来自@Petru Gardea,我正在为那些难以理解的人简化它。

在要导入另一个xsd的根xsd中,只需使用include标记,如下所示。此处不需要命名空间。

<xs:include schemaLocation="Child.xsd"  />

targetNamespace不是强制性的。

最新更新