Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Question about attribute inheritence in XML Schemas using <xsd:extension>

Reply
Thread Tools

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

 
 
Sean Bright
Guest
Posts: n/a
 
      12-16-2003
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
 
Reply With Quote
 
 
 
 
C. M. Sperberg-McQueen
Guest
Posts: n/a
 
      12-17-2003
Sean Bright <(E-Mail Removed)> writes:

> 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
xmlnssd="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
 
Reply With Quote
 
 
 
 
Sean Bright
Guest
Posts: n/a
 
      12-17-2003
C. M. Sperberg-McQueen wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can xml schemas from a dataset be used to validate standard xml files? Andy B ASP .Net 0 08-06-2008 11:42 PM
Validate xml using nested schemas dhek Java 1 08-05-2007 08:09 AM
XML Schemas Question abu123 XML 2 02-21-2005 07:06 PM
Setting Conditions based on attribute values in XML schemas Abdul Mukit XML 0 11-17-2004 09:08 AM
Help on including one XML document within another XML document using XML Schemas Tony Prichard XML 0 12-12-2003 03:18 PM



Advertisments