By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

XML schema - key/keyref and inheritance

P: n/a

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

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

Here is the schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="">
<xs:element name="Root" type="Root"/>
<xs:complexType name="Root">
<xs:element name="Base" type="Base" minOccurs="0"
<xs:complexType name="Base" abstract="true">
<xs:attribute name="key" type="xs:ID" use="required"/>
<xs:complexType name="A">
<xs:extension base="Base"/>
<xs:complexType name="B">
<xs:extension base="Base">
<xs:element name="Ref_to_A" type="xs:IDREF"/>

And here is an instance example:

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi=""
<Base xsi:type="A" key="key1"/>
<Base xsi:type="B" key="key2">

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:keyref name="InvalidKeyref" refer="InvalidKey">
<xs:selector xpath="Base[@type="B"]/Ref_to_A"/>
<xs:field xpath="."/>
Jul 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.