XML schema - key/keyref and inheritance

Discussion in 'XML' started by peterpeter, Feb 11, 2004.

  1. peterpeter

    peterpeter Guest

    Hi.

    There is a XML schema problem that I have with key/keyref:

    I have two complex (A and B) types which both inherit from a common
    base typ (Base). A refers B using a xs:IDREF element named Ref_to_A.
    Both are allowed to be instantiated under the root element by
    declaring an element of the base class.

    I want to have type safety for the reference by using key/keyref.
    There should be a key definition for the @key attribute of type A and
    a keyref definition between this key and the Ref_to_A element of type
    B.

    But there's a problem: The XPath expressions in key/keyref do not
    allow to contain tests like [@type='A']. How can I reach the instance
    element of type A or B with my XPAth? Or is there another solution?

    Thanks for helpful answers...

    Peter


    Here is the schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Root" type="Root"/>
    <xs:complexType name="Root">
    <xs:sequence>
    <xs:element name="Base" type="Base" minOccurs="0"
    maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Base" abstract="true">
    <xs:attribute name="key" type="xs:ID" use="required"/>
    </xs:complexType>
    <xs:complexType name="A">
    <xs:complexContent>
    <xs:extension base="Base"/>
    </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="B">
    <xs:complexContent>
    <xs:extension base="Base">
    <xs:sequence>
    <xs:element name="Ref_to_A" type="xs:IDREF"/>
    </xs:sequence>
    </xs:extension>
    </xs:complexContent>
    </xs:complexType>
    </xs:schema>

    And here is an instance example:

    <?xml version="1.0" encoding="UTF-8"?>
    <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test.xsd">
    <Base xsi:type="A" key="key1"/>
    <Base xsi:type="B" key="key2">
    <Ref_to_A>key1</Ref_to_A>
    </Base>
    </Root>

    I planned these key/keyref to be within my instance element
    declaration of the Root:

    <xs:element name="Root" type="Root">
    <xs:key name="InvalidKey">
    <xs:selector xpath="Base[@type="A"]"/>
    <xs:field xpath="@key"/>
    </xs:key>
    <xs:keyref name="InvalidKeyref" refer="InvalidKey">
    <xs:selector xpath="Base[@type="B"]/Ref_to_A"/>
    <xs:field xpath="."/>
    </xs:keyref>
    </xs:element>
    peterpeter, Feb 11, 2004
    #1
    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. Markus
    Replies:
    1
    Views:
    1,495
    Markus
    Nov 23, 2005
  2. Marek Malowidzki

    Key/keyref validation in schema?

    Marek Malowidzki, Aug 18, 2003, in forum: XML
    Replies:
    1
    Views:
    492
    Mark Preston
    Aug 19, 2003
  3. John Carron
    Replies:
    2
    Views:
    842
    John Carron
    Sep 2, 2005
  4. John Carron
    Replies:
    0
    Views:
    462
    John Carron
    Sep 1, 2005
  5. peterpeter
    Replies:
    0
    Views:
    715
    peterpeter
    Feb 12, 2007
Loading...

Share This Page