XML Schema:可扩展的容器元素

时间:2011-10-14 10:25:31

标签: xml xsd

我正在尝试为具有多个名称空间的文档创建架构。像这样:

<?xml version="1.0"?>
<parent xmlns="http://myNamespace"
        xmlns:c1="http://someone/elses/namespace"
        xmlns:c2="http://yet/another/persons/namespace">

    <c1:child name="Jack"/>
    <c2:child name="Jill"/>
</parent>

到目前为止,这是我在模式中的内容:

<xs:element name="parent" type="Parent"/>

<xs:complexType name="Parent">
    <!-- don't know what to put here -->
</xs:complexType>

<!-- The type that child elements must extend -->           
<xs:complexType name="Child" abstract="true">
    <xs:attribute name="name" type="xs:string"/>
</xs:complexType>

计划是让其他人能够创建具有任意子元素的文档,只要这些子元素扩展到Child类型。我的问题是:如何限制<parent>元素,使其只能包含类型为Child类型扩展名的元素?

2 个答案:

答案 0 :(得分:1)

我在这里找到答案:XML Schemas: Best Practices - Variable Content Containers

显然,您可以将<element>声明为abstract。解决方案如下:

<xs:element name="parent" type="Parent"/>

<xs:element name="child" abstract="true"/>

<xs:complexType name="Parent">
    <xs:sequence>
        <xs:element ref="child" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="Child" abstract="true">
    <xs:attribute name="name" type="xs:string"/>
</xs:complexType>

然后,其他模式可以定义自己的子类型:

<xs:element name="child-one" substitutionGroup="child" type="ChildOne"/>

<xs:element name="child-two" substitutionGroup="child" type="ChildTwo"/>

<xs:complexType name="ChildOne">
    <xs:complexContent>
        <xs:extension base="Child"/>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="ChildTwo">
    <xs:complexContent>
        <xs:extension base="Child"/>
    </xs:complexContent>
</xs:complexType>

我们可以将此作为有效文件:

<parent>
    <c1:child-one/>
    <c1:child-two/>
</parent>

答案 1 :(得分:0)

请找到以下链接。这说明了如何继承元素。

http://www.ibm.com/developerworks/library/x-flexschema/

相关问题