enforce subelements to be of a specific type in XSD?

Discussion in 'XML' started by Brett Gerhardi, May 16, 2005.

  1. 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>
    Brett Gerhardi, May 16, 2005
    #1
    1. Advertising

  2. 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>


    "Brett Gerhardi" <> wrote in message
    news:...
    > 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>
    >
    Brett Gerhardi, May 16, 2005
    #2
    1. Advertising

  3. 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

    "Brett Gerhardi" <> wrote in message
    news:...
    > 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>
    >
    >
    > "Brett Gerhardi" <> wrote in message
    > news:...
    >> 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>
    >>

    >
    >
    Brett Gerhardi, May 17, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin Khurana
    Replies:
    2
    Views:
    821
    Jonathan Bromley
    Apr 27, 2004
  2. Daniel Jorge

    SubElements in my WebControl

    Daniel Jorge, Jul 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    343
    Daniel Jorge
    Jul 10, 2005
  3. Nicole Szymanski
    Replies:
    3
    Views:
    797
  4. thomas
    Replies:
    2
    Views:
    384
    thomas
    Jan 25, 2004
  5. bogdan
    Replies:
    1
    Views:
    242
    Cowboy \(Gregory A. Beamer\)
    Apr 1, 2008
Loading...

Share This Page