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

Discussion in 'XML' started by Sean Bright, Dec 16, 2003.

  1. Sean Bright

    Sean Bright Guest

    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
     
    Sean Bright, Dec 16, 2003
    #1
    1. Advertising

  2. Sean Bright <> 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
    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
     
    C. M. Sperberg-McQueen, Dec 17, 2003
    #2
    1. Advertising

  3. Sean Bright

    Sean Bright Guest

    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
     
    Sean Bright, Dec 17, 2003
    #3
    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. Albretch
    Replies:
    0
    Views:
    339
    Albretch
    May 26, 2005
  2. Tony Prichard
    Replies:
    0
    Views:
    791
    Tony Prichard
    Dec 12, 2003
  3. Abdul Mukit
    Replies:
    0
    Views:
    338
    Abdul Mukit
    Nov 17, 2004
  4. Sascha Kerschhofer
    Replies:
    1
    Views:
    1,148
    Priscilla Walmsley
    Aug 16, 2005
  5. SiJP

    XML from XSD Schemas

    SiJP, Nov 11, 2005, in forum: XML
    Replies:
    1
    Views:
    534
    Tjerk Wolterink
    Nov 12, 2005
Loading...

Share This Page