2011-10-14 84 views
1

我正在尝试为具有多个名称空间的文档创建架构。事情是这样的:XML架构:可扩展容器元素

<?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类型的扩展的元素?

回答

1

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

显然你可以声明<element> s为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> 
0

请在下面找到链接。这告诉我们如何继承这些元素。

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

+0

感谢链接。这篇文章似乎没有涉及我后来的事情:我可以做继承,我只是不知道如何限制'Parent'的内容,只允许其类型继承自'Child'的元素。 – Daniel