【发布时间】:2020-11-10 18:57:05
【问题描述】:
我需要一些帮助来构建 xsd。 第三方提供了一个 xsd (v1.xsd),我必须将其包含在我的自定义 xsd (example.xsd) 中。他们将很快发布他们的 xsd (v2.xsd) 的新版本,不幸的是使用相同的命名空间。我需要在我的 xsd 中包含新版本 (v2.xsd) 和旧版本 (v1.xsd),但我正在努力实现这一目标。 我不知道如何使用相同的命名空间导入这两个(或多个)xsd 并引用它们。
有没有人建议我如何做到这一点?
我希望这个小例子能说明我想要完成的事情。
example.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:v1="http://loremipsum.org/xsd/1_1"
xmlns:v2="http://loremipsum.org/xsd/1_1"
>
<xs:import namespace="http://loremipsum.org/xsd/1_1"
schemaLocation="include/v1.xsd"/>
<xs:import namespace="http://loremipsum.org/xsd/1_1"
schemaLocation="include/v2.xsd"/>
<xs:element name="CONTENT">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element ref="v1:note"/>
<xs:element ref="v2:note"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
v1.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://loremipsum.org/xsd/1_1"
targetNamespace="http://loremipsum.org/xsd/1_1"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
v2.xsd
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://loremipsum.org/xsd/1_1"
targetNamespace="http://loremipsum.org/xsd/1_1"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="additional" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
任何帮助表示赞赏:)。
【问题讨论】:
-
做不到;在一个模式中不能有两个相同类型的冲突定义。一般来说,如果您对架构进行不兼容的更改(即,如果版本 2 架构不验证版本 1 实例),那么新版本应该使用不同的命名空间,这被认为是一种很好的做法。