我正在尝试为将具有多个名称空间的文档创建模式。像这样:
<?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>
到目前为止,我的schema中是这样的:
<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
类型扩展的元素?
我在这里找到了答案:XML模式:最佳实践-可变内容容器。
显然您可以将<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>
请查看下面的链接。这说明如何继承元素。
http://www.ibm.com/developerworks/library/x-flexschema/