【问题标题】:Using Abstract in Multiple XSD Schemas在多个 XSD 架构中使用抽象
【发布时间】:2023-03-29 08:47:01
【问题描述】:

我正在尝试创建一组模式,其想法是有一个标准模式的核心,然后将使用特定功能导入其他模式,并且可以在单个 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 错误:

元素“SpecialComponentA1”的{type definition}不是从substitutionHead“SchemaRoot:Component”的{type definition}有效派生的,或者“SchemaRoot:Component”的{substitution group excludes}属性不允许这样做推导。


那么,我怎样才能让我的模式得到验证,以便我可以制作包含来自各种 XSD 模式的结构化数据的有效 XML 文档?

【问题讨论】:

    标签: xml xsd abstract


    【解决方案1】:

    对不起,我觉得这么快就找到解决方案有点傻,但是:


    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>
    

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 2015-05-29
      • 1970-01-01
      • 2012-01-29
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多