一种方法是使用替换组。您为样板定义了一个基本 XSD。
基础.xsd
<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/XML Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="head">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="common1" type="xsd:anyType"/>
<xsd:element name="common2" type="xsd:anyType"/>
<xsd:element ref="headerPlaceholder"/>
<xsd:element name="common3" type="xsd:anyType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="headerPlaceholder" type="headerPlaceholder" abstract="true"/>
<xsd:complexType name="headerPlaceholder" abstract="true"/>
<xsd:complexType name="request" abstract="true">
<xsd:sequence>
<xsd:element ref="head"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
然后你可以独立构建你的 req1 和 req 。
请求1.xsd
<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/XML Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="SubstitutionGroupHeader.xsd"/>
<xsd:element name="req1" type="req1"/>
<xsd:complexType name="req1">
<xsd:complexContent>
<xsd:extension base="request">
<xsd:sequence>
<xsd:element name="data1"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="choice1" substitutionGroup="headerPlaceholder">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="headerPlaceholder"/>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="choice2" substitutionGroup="headerPlaceholder">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="headerPlaceholder"/>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
结构如下所示:
请求2.xsd
<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/XML Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="SubstitutionGroupHeader.xsd"/>
<xsd:element name="req2" type="req2"/>
<xsd:complexType name="req2">
<xsd:complexContent>
<xsd:extension base="request">
<xsd:sequence>
<xsd:element name="data2"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="choice3" substitutionGroup="headerPlaceholder">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="headerPlaceholder"/>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="choice4" substitutionGroup="headerPlaceholder">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="headerPlaceholder"/>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
req2 的结构:
我将choice1/2 和choice3/4 放在req1 和req2 中,只是为了内容之间没有重叠。所以文件结构看起来像这样:
XSD组件之间的整体关系:
如果您想从 req1 和 req2 中引用相同的choice2,则必须在其自己的文件中重构choice2,等等。
关于选择与替代组可以讨论很多,但这是另一回事......