在多个XSD架构中使用Abstract

时间:2015-11-10 11:03:39

标签: xml xsd abstract

我正在尝试创建一组模式,并认为将有一个标准模式的核心,然后将使用特定函数导入其他模式,并且可以在单个XML文档中描述整个模式。 / p>

所以,我有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错误:

  

元素&#39; SpecialComponentA1&#39;的{type definition}不能有效地从substituteHead&#39; SchemaRoot:Component&#39;的{type definition}或者&#39; SchemaRoot:Component&#39;的{substitute group exclusions}属性中获得。不允许这种推导。

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

1 个答案:

答案 0 :(得分:0)

抱歉,我很快就找到解决方案,但是:

代表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>
相关问题