XML schema - key/keyref and inheritance

P

peterpeter

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>
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top