enforce subelements to be of a specific type in XSD?

B

Brett Gerhardi

Hi all, I'm trying to something that I'm sure must be possible within the
schema language but I cannot seem to work it out.

Briefly, I am defining a multi-tiered base structure - each tier has its own
complextype defined with a base CT having the structure that others should
follow.

At the leaf ends of some areas I want to restrict the type of elements that
are defined in restriction extended types but cannot find the correct way to
do it.

Take the following example. I would like to be able to define many different
specific Animal CTs based from Animal CT (Tiger, wilderbeast, giraffe etc)
and be able to define "per specific animal CT" what other specific animal
CTs can be their friend (it is unlikely a wilderbeast will be friends with a
Tiger for example :) ). Each specific animal CT should be able to specify an
unlimited number of elements based on Animal type, but any other type should
not be allowed.

Firstly is this actually possible to protect content models in such a way
and if so how. I can not get my head around how I can express such a type
restriction.

The following example is one technique I've tried and it is invalid ("The
content model of complex type 'MonkeyFriends' is not a valid restriction of
the contact model of complex type 'AnimalFriends')

Many thanks for any suggestions.

-=- Brett

<xs:complexType name="Animal">
<xs:choice>
<xs:element name="Friends" type="AnimalFriends"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="AnimalFriends">
<xs:choice>
<xs:element name="AnyAnimalType" type="Animal"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="Monkey">
<xs:complexContent>
<xs:restriction base="Animal">
<xs:choice>
<xs:element name="Friends" type="MonkeyFriends"/>
</xs:choice>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="MonkeyFriends">
<xs:complexContent>
<xs:restriction base="AnimalFriends">
<xs:choice>
<xs:element name="OnlyMonkeyFriends" type="Animal"/>
</xs:choice>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>
 
B

Brett Gerhardi

My apologies - the reason I got the error in the example I gave was due to
conflicting derived By settings in the CT vs local elements (why does XMLspy
allow such behaviour?.. )

Anyway - here is a slightly modified xsd that if you remove the
SpecialMonkeyFriends element in MonkeyFriends, will validate correctly, but
I want to be able to specify as many elements as I want for the
MonkeyFriends but only of the Animal type.

Again, I do understand why it doesn't validate, but how can I express in the
XSD what I really want?

Thanks
-=- Brett

<xs:complexType name="Animal">
<xs:choice>
<xs:element name="Friends" type="AnimalFriends"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="AnimalFriends">
<xs:choice>
<xs:element name="AnyAnimalType" type="Animal" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="Monkey">
<xs:complexContent>
<xs:restriction base="Animal">
<xs:choice>
<xs:element name="Friends" type="MonkeyFriends"/>
</xs:choice>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="MonkeyFriends">
<xs:complexContent>
<xs:restriction base="AnimalFriends">
<xs:choice>
<xs:element name="NormalMonkeyFriends">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="Animal">
<xs:choice>
<xs:element name="Friends" type="AnimalFriends"/>
</xs:choice>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element name="SpecialMonkeyFriends">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="Animal">
<xs:choice>
<xs:element name="Friends" type="AnimalFriends"/>
</xs:choice>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
 
B

Brett Gerhardi

I have been trying to research this further.

Perhaps the only way to do this would be to drop the base class into its own
namespaced file then include it and use Any with its target namespace?

Is the only way to do this by using namespaces and the any type?

Regards
-=- Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top