Question about attribute inheritence in XML Schemas using <xsd:extension>

S

Sean Bright

Hi there.

I'm having a problem which I hope is a simple one... Any help would be
appreciated. I think it has something to do with the way attributes are
(or are not) inherited in schemas...

This is the content of my XML Schema (minus the schema tag):

<xsd:complexType name="AType">
<xsd:attribute name="attr1" type="xsd:string" />
<xsd:attribute name="attr2" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="BType">
<xsd:complexContent>
<xsd:extension base="AType">
<xsd:attribute name="attr3" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:element name="mydoc">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="a" type="AType"/>
<xsd:element name="b" type="BType"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>

In my instance document, I simply want to be able to do the following:

<mydoc xmlns="http://foo.org/foo.xsd">
<a attr1="1" attr2="2"/>
<b attr1="1" attr2="2" attr3="3"/>
</mydoc>

Whenever I try to validate the document, however, I get an error that
attr1 and attr2 are not valid for <b>. But <b> is of BType which
extends AType and attr1 and attr2 are defined for AType. What am I missing?

Thanks in advance,
Sean
 
C

C. M. Sperberg-McQueen

Sean Bright said:
Hi there.

I'm having a problem which I hope is a simple one... Any help would be
appreciated. I think it has something to do with the way attributes
are (or are not) inherited in schemas...

It may. I suspect it has something to do with the
way namespaces work in XML and in XML Schema.
This is the content of my XML Schema (minus the schema tag):

Can you show us the start-tag of the schema element, too? Only
that will allow us to see whether what is happening below is
what you think is happening. I need to see your namespace
declarations and your targetNamespace attribute.
<xsd:complexType name="AType">
<xsd:attribute name="attr1" type="xsd:string" />
<xsd:attribute name="attr2" type="xsd:string" />
</xsd:complexType>

OK. This declares a complex type named "AType" in the
target namespace.
<xsd:complexType name="BType">
<xsd:complexContent>
<xsd:extension base="AType">
<xsd:attribute name="attr3" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

OK. This declares a complex type named "BType" in
the target namespace, which in turn extends a complex
type named "AType" in whatever namespace (if any) is
declared as the default namespace in the schema
document.

The local elements will either be qualified names in the target
namespace (if you have one, and if you have specified
elementFormDefault as "qualified"), or unqualified names (names not
associated with any namespace -- some people like to say 'in the
anonymous namespace' and others to say 'not in any namespace')
if you have left elementFormDefault as "unqualified" (the
default).

When I reconstructed your schema just now, I wrote

<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://foo.org/foo.xsd"
xmlns="http://foo.org/foo.xsd"
elementFormDefault="qualified">

and I get no errors from Xerces J when I validate your
sample document.
In my instance document, I simply want to be able to do the following:

<mydoc xmlns="http://foo.org/foo.xsd">
<a attr1="1" attr2="2"/>
<b attr1="1" attr2="2" attr3="3"/>
</mydoc>

Whenever I try to validate the document, however, I get an error that
attr1 and attr2 are not valid for <b>. But <b> is of BType which
extends AType and attr1 and attr2 are defined for AType. What am I
missing?

It's more helpful if you show the error message. My guess is
that one way or another the elements in your document, or the
element declarations in your schema, or the type definitions in
your schema, did not end up in the same namespace.

I hope this helps.

-C. M. Sperberg-McQueen
World Wide Web Consortium
 
S

Sean Bright

C. M. Sperberg-McQueen said:
Can you show us the start-tag of the schema element, too? Only
that will allow us to see whether what is happening below is
what you think is happening. I need to see your namespace
declarations and your targetNamespace attribute.

Aye, there's the rub...

You go on to mention elementFormDefault and attributeFormDefault. The
problem was that in my <xsd:schema> tag I was specifying that the
attributeFormDefault was 'qualified'

Stupid mistake on my part. Combination of my own mistakes and Visual
Studio .NET's intellisense functionality. Apparently Intellisense
(which uses XML Schema to drive it) can not handle the <xsd:extension>
types and I was using the appearance (or lack there of) of attribute
options as the error check, as opposed to running the validator each
time. That'll teach me too be lazy.

Thanks for your feedback, I thought I was going crazy.

Oh, and my targetNamespace was "http://foo.org/foo.xsd" but ultimately
that wasn't the fish I should have been frying.

Thanks,
Sean
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top