Deriving from abstract class and only reference the derived classes

时间:2017-01-03 10:23:20

标签: xml xsd

I want to have an abstract class (as a example "vehicles") and want to derive from it other classes (as an example "car" and "motorbike").

Now I want to reference the abstract class in my main element, so that in the xml file it is only allowed to use every derived class from "vehicles". I am just unsure how to do that, any help would be appreciated.

Example XML:

<main xmlns="http://www.exampleURI.com/example">
  <car>

  </car>
  <motorbike>

  </motorbike>
</main>

Example XSD:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.exampleURI.com/example" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ex="http://www.exampleURI.com/example">
    <xs:element name="main" type="ex:main"/>
    <xs:complexType name="main">
        <xs:sequence>
            <xs:element name="vehicles" type="ex:vehicles"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="vehicles" type="ex:vehicles"/>
    <xs:complexType name="vehicles" abstract="true">
        <xs:sequence/>
    </xs:complexType>
    <xs:element name="car" type="ex:car"/>
    <xs:complexType name="car">
        <xs:complexContent>
            <xs:extension base="ex:vehicles">
                <xs:sequence/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="motorbike" type="ex:motorbike"/>
    <xs:complexType name="motorbike">
        <xs:complexContent>
            <xs:extension base="ex:vehicles">
                <xs:sequence/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

1 个答案:

答案 0 :(得分:1)

似乎替换组会做到这一点。此外,在main内,您需要使用ref属性来确保引用正确的元素,并允许无限数量的子元素。

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.exampleURI.com/example" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ex="http://www.exampleURI.com/example">
    <xs:element name="main" type="ex:main"/>
    <xs:complexType name="main">
        <xs:sequence>
            <xs:element ref="ex:vehicles" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="vehicles" type="ex:vehicles"/>
    <xs:complexType name="vehicles" abstract="true">
        <xs:sequence/>
    </xs:complexType>
    <xs:element name="car" type="ex:car" substitutionGroup="ex:vehicles"/>
    <xs:complexType name="car">
        <xs:complexContent>
            <xs:extension base="ex:vehicles">
                <xs:sequence/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="motorbike" type="ex:motorbike" substitutionGroup="ex:vehicles"/>
    <xs:complexType name="motorbike">
        <xs:complexContent>
            <xs:extension base="ex:vehicles">
                <xs:sequence/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

本文档对上述架构有效:

<main xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.exampleURI.com/example test.xsd"
 xmlns="http://www.exampleURI.com/example">
    <car></car>
    <motorbike></motorbike>
</main>