Reputation: 10245
I am trying to create a schema for a document that will have multiple namespaces. Something like this:
<?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>
This is what I have in my schema so far:
<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>
The plan is for others to be able to create documents with arbitrary child elements, as long as those child elements extends my Child
type. My question is: how can I restrict the <parent>
element such that it can only contain elements whose types are extensions of the Child
type?
Upvotes: 1
Views: 1269
Reputation: 10245
I found the answer here: XML Schemas: Best Practices - Variable Content Containers.
Apparently you can declare <element>
s as abstract
. A solution is as follows:
<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>
Other schemas can then define their own child types like this:
<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>
We can then have this as a valid document:
<parent>
<c1:child-one/>
<c1:child-two/>
</parent>
Upvotes: 1
Reputation: 10555
Please find the link below. This tells how inherit the elements.
http://www.ibm.com/developerworks/library/x-flexschema/
Upvotes: 0