首页 > 解决方案 > 从其他 XML 模式继承:重新定义单个类型而不触及基础 xsd

问题描述

我有一个想要扩展的在线 XML 模式。它看起来像这样:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:inheritance:1"
           xmlns="urn:inheritance:1"
>

    <xs:complexType name="aType">
        <xs:sequence>
            <xs:element name="b" type="bType"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="bType">
        <xs:sequence>
            <xs:element name="c"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="a" type="aType"/>

</xs:schema>

其中我想扩展类型bType

我目前的解决方案是:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:inheritance:2"
           xmlns="urn:inheritance:2"
           xmlns:inh="urn:inheritance:1"
>

    <xs:redefine schemaLocation="urn:inheritance:1">

        <xs:complexType name="aType">
            <xs:complexContent>
                <xs:extension base="aType">
                    <xs:sequence>
                        <xs:element name="b" type="bType"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

        <xs:complexType name="bType">
            <xs:complexContent>
                <xs:extension base="bType">
                    <xs:sequence>
                        <xs:element name="newC" type="newCType"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

    </xs:redefine>

    <xs:element name="a" type="aType"/>

    <xs:complexType name="newCType"/>

</xs:schema>

我发现这非常麻烦并且实际上并没有完全解决我的问题,因为如果bType完全在其他地方使用,我必须重复这个结构。

是否可以做这样的事情,我只是重新定义类型,以便我的原始方案用这个更新的类型更新:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:inheritance:2"
           xmlns="urn:inheritance:2"
           xmlns:inh="urn:inheritance:1"
>

    <xs:redefine schemaLocation="urn:inheritance:1">

        <xs:complexType name="bType">
            <xs:complexContent>
                <xs:extension base="bType">
                    <xs:sequence>
                        <xs:element name="newC" type="newCType"/>
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>

    </xs:redefine>

    <xs:element name="a" type="aType"/>

    <xs:complexType name="newCType"/>

</xs:schema>

标签: xmlinheritance

解决方案


推荐阅读