在多个 XSD 模式中使用摘要

Using Abstract in Multiple XSD Schemas

我正在尝试创建一组模式,想法是将有一个标准模式的核心,然后将导入具有特定功能的其他模式,并且可以在单个 XML 文件.


所以,我有 SchemaRoot.xsd,它包含文档的顶级信息,其中有一个无限序列引用名为 SchemaRoot:Component 的根级抽象元素。

<xs:element name="Components">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="Component"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="Component" abstract="true">
    <xs:attribute name="ServiceID" type="xs:ID" use="required"/>
    <!-- I CAN PUT OTHER ATTRIBUTES/SEQUENCES HERE TOO, AND IT VALIDATES -->
</xs:element>

现在在 SpecialComponentSetA.xsd 中,我导入 SchemaRoot.xsd,并创建一个名为 SpecialComponentSetA:SpecialComponentA1 的元素,它是替换组 SchemaRoot:Component.

的一部分
<xs:element name="SpecialComponentA1" substitutionGroup="SchemaRoot:Component"/>

在我的文档中,名为 DeploymentSetAlpha.xml 的文件导入了这两个模式,创建了一个根级元素,其中包含一个组件 SpecialComponentSetA:SpecialComponentA1,这验证得很好。

<SchemaRoot:Components>
    <SpecialComponentSetA:SpecialComponentA1/>
</SchemaRoot:Components>

但是,当我尝试添加任何序列、属性或任何不仅仅是该替换组中元素存在的内容时:

<xs:element name="SpecialComponentA1" substitutionGroup="SchemaRoot:Component">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="AReallyImportantElement" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

方案不再有效,我收到 e-props-correct.4 错误:

The {type definition} of element 'SpecialComponentA1' is not validly derived from the {type definition} of the substitutionHead 'SchemaRoot:Component', or the {substitution group exclusions} property of 'SchemaRoot:Component' does not allow this derivation.


那么,我如何让我的架构得到验证,以便我可以制作包含来自各种 XSD 架构的结构化数据的有效 XML 文档?

抱歉,这么快就找到了解决方案,我觉得很傻,但是:


对于SchemaRoot.xsd

<xs:element name="Components">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="Component"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="Component" type="Component" abstract="true"/>
<xs:complexType name="Component" abstract="true">
    <xs:attribute name="ServiceID" type="xs:ID" use="required"/>
</xs:complexType>

对于SpecialComponentSetA.xsd

<xs:element name="SpecialComponentA1" substitutionGroup="SchemaRoot:Component">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="SchemaRoot:Component">
                <xs:sequence>
                    <xs:element name="AReallyImportantElement" type="xs:string"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

然后这允许 DeploymentSetAlpha.xml 验证

<SchemaRoot:Components>
    <SpecialComponentSetA:SpecialComponentA1>
        <AReallyImportantElement>FOOBAR</AReallyImportantElement>
    </SpecialComponentSetA:SpecialComponentA1>
</SchemaRoot:Components>