Schema restrictions on objects and instances

Discussion in 'XML' started by Victor, May 12, 2004.

  1. Victor

    Victor Guest

    I have a Schema with some elements which are "types" or objects, and
    some elements which correspond to instances of the types. How can I
    specify that a <locationInstance> must have an attribute "type" which
    equals any one of the "id" attributes of the <locationType>s?

    --
    Victor
    CERN / NTNU
     
    Victor, May 12, 2004
    #1
    1. Advertising

  2. Victor wrote:

    > I have a Schema with some elements which are "types" or objects, and
    > some elements which correspond to instances of the types. How can I
    > specify that a <locationInstance> must have an attribute "type" which
    > equals any one of the "id" attributes of the <locationType>s?


    Have a look at ID/IDREF and key/keyref.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 12, 2004
    #2
    1. Advertising

  3. Victor

    Victor Guest

    Tip: Types and instances in Schemas (was: Schema restrictions onobjects and instances)

    Martin Honnen wrote:
    >
    >
    > Victor wrote:
    >
    >> I have a Schema with some elements which are "types" or objects, and
    >> some elements which correspond to instances of the types. How can I
    >> specify that a <locationInstance> must have an attribute "type" which
    >> equals any one of the "id" attributes of the <locationType>s?

    >
    >
    > Have a look at ID/IDREF and key/keyref.


    Thanks!

    After a lot of looking around for working and well-explained examples, I
    managed to get the following (in case someone is looking for something
    similar):

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="ImportData">
    <xsd:complexType>
    <xsd:sequence>
    .. definitions of the "locationType" and "location" elements ..
    </xsd:sequence>
    </xsd:complexType>

    <xsd:key name="locationTypeId">
    <xsd:annotation>
    <xsd:documentation>Within the whole data set, every location
    type must have a unique ID attribute</xsd:documentation>
    </xsd:annotation>
    <xsd:selector xpath=".//locationType"/>
    <xsd:field xpath="@id"/>
    </xsd:key>

    <xsd:key name="locationId">
    <xsd:annotation>
    <xsd:documentation>Within the whole data set, every location
    must have a unique ID attribute</xsd:documentation>
    </xsd:annotation>
    <xsd:selector xpath=".//location"/>
    <xsd:field xpath="@id"/>
    </xsd:key>

    <xsd:keyref name="locationIdRef" refer="locationTypeId">
    <xsd:annotation>
    <xsd:documentation>Locations must be of an existing
    locationType</xsd:documentation>
    </xsd:annotation>
    <xsd:selector xpath=".//location"/>
    <xsd:field xpath="@type"/>
    </xsd:keyref>

    .... The rest of the Schema ...

    A few notes:

    - <key> is basically the same as <unique>, except that the elements
    and/or attributes referenced in the <field> XPath _has_ to exist, i.e.
    they cannot be empty.

    --
    Victor
     
    Victor, May 13, 2004
    #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. Jean-Marie Condom
    Replies:
    2
    Views:
    406
    Jean-Marie Condom
    Nov 26, 2004
  2. Moth
    Replies:
    2
    Views:
    432
  3. ruediger
    Replies:
    0
    Views:
    583
    ruediger
    May 6, 2006
  4. Paul Johnston

    Restrictions in XML Schema

    Paul Johnston, Nov 8, 2006, in forum: XML
    Replies:
    1
    Views:
    467
  5. Marc Hebert
    Replies:
    3
    Views:
    619
Loading...

Share This Page