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

Help extending a complex type

P: n/a
Hello all,

I am having a problem with extending complex types. I have a simple
base schema (FooBar.xsd) which defines XML documents of the following
form:

<Foo FooAttribute1="aaa">
<Bar BarAttribute1="xxx"/>
</Foo>

I'm trying to write a schema (ExtendedFooBar.xsd) which includes
FooBar.xsd to add one new attribute (each) to <Foo> and <Bar>.
Documents adhering to this schema should look like:

<Foo FooAttribute1="aaa" FooAttribute2="bbb">
<Bar BarAttribute1="xxx" BarAttribute2="yyy"/>
</Foo>

I can get ExtendedFooBar.xsd to validate, but XML which adheres to it
doesn't recognize the new Bar attribute. In other words, valid XML
documents are of the following form:

<Foo FooAttribute1="aaa" FooAttribute2="bbb">
<!-- Note the missing BarAttribute2 -->
<Bar BarAttribute1="xxx"/>
</Foo>

Naively, I thought that by including the base schema in the extended
one, references to 'Bar' would be satisfied by the definition in the
extended schema. This seems to be incorrect.

Here's the schema XML for the two files:

--- FooBar.xsd ---

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified" >
<xs:complexType name="FooType">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element ref="Bar"/>
</xs:sequence>
<xs:attribute name="FooAttribute1" type="xs:string" use="required"/>
</xs:complexType>

<xs:complexType name="BarType">
<xs:attribute name="BarAttribute1" type="xs:string" use="required"/>
</xs:complexType>

<xs:element name="Bar" type="BarType"/>
<xs:element name="Foo" type="FooType"/>
</xs:schema>

--- ExtendedFooBar.xsd ---

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="BaseFooBar.xsd"/>
<xs:complexType name="ExtendedBarType">
<xs:complexContent>
<xs:extension base="BarType">
<xs:attribute name="BarAttribute2" type="xs:string"
use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="ExtendedFooType">
<xs:complexContent>
<xs:extension base="FooType">
<xs:attribute name="FooAttribute2" type="xs:string"
use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:element name="Bar" type="ExtendedBarType"/>
<xs:element name="Foo" type="ExtendedFooType"/>
</xs:schema>

Any help would be appreciated. I've been scouring the web and my books
for a few hours on this one.

Thanks,

Brian

Mar 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Brian,

You cannot declare the same elements again, that will result in an
invalid schema. What you can do however is to redefine the two complex
types as below

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:redefine schemaLocation="FooBar.xsd">
<xs:complexType name="FooType">
<xs:complexContent>
<xs:extension base="FooType">
<xs:attribute name="FooAttribute2" type="xs:string"
use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="BarType">
<xs:complexContent>
<xs:extension base="BarType">
<xs:attribute name="BarAttribute2" type="xs:string"
use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
</xs:schema>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Mar 21 '06 #2

P: n/a
Thanks for taking a look at this. I stumbled upon the same solution
yesterday, but it's nice to have it validated by someone who really
knows this stuff. I'm new to XSLT.

Brian Hartin
Pearson Educational Measurement
http://www.pearsonedmeasurement.com/
http://www.pearsonsolutions.com/

Mar 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.