Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / November 2004

Tip: Looking for answers? Try searching our database.

substitutionGroup and extension problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeffry van de Vuurst - 26 Nov 2004 10:18 GMT
Hi,

I'm working on an xml schema and I'm running into some problems
relating substitutionGroups and extensions.

This xsd validates fine:
There are three elements and three complex types and every element has
the type of some complexType.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Catalogue">
 <xs:complexType>
  <xs:sequence>
   <xs:element ref="Publication" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:complexType>
</xs:element>
<xs:element name="Publication" type="PublicationType"
abstract="true"/>
<xs:element name="Book" type="BookType"
substitutionGroup="Publication"/>
<xs:element name="Magazine" type="MagazineType"
substitutionGroup="Publication"/>
<xs:complexType name="PublicationType">
 <xs:sequence>
  <xs:element name="Title" type="xs:string"/>
  <xs:element name="Author" type="xs:string" minOccurs="0"
maxOccurs="unbounded"/>
  <xs:element name="Date" type="xs:gYear"/>
 </xs:sequence>
</xs:complexType>
<xs:complexType name="BookType">
 <xs:complexContent>
  <xs:extension base="PublicationType">
   <xs:sequence>
    <xs:element name="ISBN" type="xs:string"/>
    <xs:element name="Publisher" type="xs:string"/>
   </xs:sequence>
  </xs:extension>
 </xs:complexContent>
</xs:complexType>
<xs:complexType name="MagazineType">
 <xs:complexContent>
  <xs:restriction base="PublicationType">
   <xs:sequence>
    <xs:element name="Title" type="xs:string"/>
    <xs:element name="Author" type="xs:string" minOccurs="0"
maxOccurs="0"/>
    <xs:element name="Date" type="xs:gYear"/>
   </xs:sequence>
  </xs:restriction>
 </xs:complexContent>
</xs:complexType>
</xs:schema>

Now, if I change the schema so that the elements extend the complex
type, so instead of <xs:element name="Book" type="BookType"
substitutionGroup="Publication"/>

I say:
<xs:element name="Book" substitutionGroup="Publication">
 <xs:complexType>
  <xs:complexContent>
   <xs:extension base="BookType">
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
</xs:element>

The schema now looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Catalogue">
 <xs:complexType>
  <xs:sequence>
   <xs:element ref="Publication" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:complexType>
</xs:element>
<xs:element name="Publication" abstract="true">
 <xs:complexType>
  <xs:complexContent>
   <xs:extension base="PublicationType">
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
</xs:element>
<xs:element name="Book" substitutionGroup="Publication">
 <xs:complexType>
  <xs:complexContent>
   <xs:extension base="BookType">
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
</xs:element>
<xs:element name="Magazine" substitutionGroup="Publication">
 <xs:complexType>
  <xs:complexContent>
   <xs:extension base="MagazineType">
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
</xs:element>
<xs:complexType name="PublicationType">
 <xs:sequence>
  <xs:element name="Title" type="xs:string"/>
  <xs:element name="Author" type="xs:string" minOccurs="0"
maxOccurs="unbounded"/>
  <xs:element name="Date" type="xs:gYear"/>
 </xs:sequence>
</xs:complexType>
<xs:complexType name="BookType">
 <xs:complexContent>
  <xs:extension base="PublicationType">
   <xs:sequence>
    <xs:element name="ISBN" type="xs:string"/>
    <xs:element name="Publisher" type="xs:string"/>
   </xs:sequence>
  </xs:extension>
 </xs:complexContent>
</xs:complexType>
<xs:complexType name="MagazineType">
 <xs:complexContent>
  <xs:restriction base="PublicationType">
   <xs:sequence>
    <xs:element name="Title" type="xs:string"/>
    <xs:element name="Author" type="xs:string" minOccurs="0"
maxOccurs="0"/>
    <xs:element name="Date" type="xs:gYear"/>
   </xs:sequence>
  </xs:restriction>
 </xs:complexContent>
</xs:complexType>
</xs:schema>

Now, I get an error validating. Xmlspy 2005 says this:
Type Definition "PublicationType" is directly derived from 'anyType'
and, thus, not a valid derivation of 'xs:anyType'.

If I try to validate this schema using .net, I get the following
errors:
Schema parsing error 'Magazine' cannot be a member of substitution
group with head element 'Publication'. An error occurred at , (26, 3).
Schema parsing error 'Book' cannot be a member of substitution group
with head element 'Publication'. An error occurred at , (18, 3).

Anybody know what's wrong with this schema? I thought that <xs:element
name="Book" type="BookType" substitutionGroup="Publication"/>

and:
<xs:element name="Book" substitutionGroup="Publication">
 <xs:complexType>
  <xs:complexContent>
   <xs:extension base="BookType">
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
</xs:element>

are basically the same, because I'm using an extension, but I'm not
actually 'extending' it by adding any elements or attributes to it.

Thanks in advance,

Jeffry
Priscilla Walmsley - 29 Nov 2004 16:50 GMT
Hi Jeffry,

They are not the same thing.  Using the second syntax, even though you are
not adding any elements or attributes, you are still creating a new
(anonymous) type that is an extension of BookType.  So, when you go to add
Book to the Publication substitutionGroup, the type of Book is not derived
from the type of Publication; their types are "siblings" in the derivation
chain.

Just out of curiosity, is there any reason you don't like the first schema
you showed us?  I think that one's a lot more clear and compact, and allows
for further derivation down the line...

Hope that helps,
Priscilla

------------------------------------------------------------------
Priscilla Walmsley
Author, Definitive XML Schema  /  XML in Office 2003
http://www.datypic.com
------------------------------------------------------------------

> Hi,
>
[quoted text clipped - 164 lines]
>
> Jeffry
Jeffry van de Vuurst - 29 Nov 2004 19:02 GMT
Hi Priscilla,

So if I understand correctly.

What you're saying is that if I extend BookType in my Book element I create
an anonymous type. So would this be basically the same as doing this?:

<xs:complexType name="ExtendedBookType">
 <xs:complexContent>
  <xs:extension base="BookType">
  </xs:extension>
 </xs:complexContent>
</xs:complexType>

<xs:element name="Book" substitutionGroup="Publication">
 <xs:complexType>
  <xs:complexContent>
   <xs:extension base="ExtendedBookType">
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
</xs:element>

And, the type of Book should derive DIRECTLY from the type of Publication?
It is not that if Book derives from ExtendedBookType, ExtendedBookType
derives from BookType and BookType derives from PublicationType, that I can
substitute the Book element for the Publication element?

Anyway, I too like the first schema best. That was what I came up with as a
response to the second schema that I received from my customer. We are now
discussing the schema's and with your explanation I can argue that the
second schema is not correct. Well, I knew it was not correct, because it
didn't validate, but now I know why (at least, if my abovementioned
understanding of your explanation is correct)  :)

Thanks a lot,
Jeffry

> Hi Jeffry,
>
[quoted text clipped - 186 lines]
>>
>> Jeffry
Priscilla Walmsley - 29 Nov 2004 19:39 GMT
Hi Jeffry,

> What you're saying is that if I extend BookType in my Book element I
> create an anonymous type. So would this be basically the same as doing
> this?:

> <xs:complexType name="ExtendedBookType">
>  <xs:complexContent>
[quoted text clipped - 11 lines]
>  </xs:complexType>
> </xs:element>

No, now the type of Book is an anonymous type that is an extension of
ExtendedBookType, which itself is an extension of BookType.  That's not the
same thing as:

<xs:element name="Book" substitutionGroup="Publication">
 <xs:complexType>
  <xs:complexContent>
   <xs:extension base="BookType">
   </xs:extension>
  </xs:complexContent>
 </xs:complexType>
</xs:element>

where the type of Book is an anonymous type that is an extension of BookType
(2 types instead of 3).

> And, the type of Book should derive DIRECTLY from the type of Publication?
> It is not that if Book derives from ExtendedBookType, ExtendedBookType
> derives from BookType and BookType derives from PublicationType, that I
> can substitute the Book element for the Publication element?

It doesn't have to be directly derived from it.  But in your 2nd schema the
Publication element doesn't have the type PublicationType, it has an
anonymous type that is an extension of PublicationType.  So what you have
is:

      PublicationType
        |              |
 anon1           BookType
                          |
                         anon2

where anon1 is the type of Publication and anon2 is the type of Book.  anon2
is not considered to be derived from anon1 because it is not a descendent of
anon1.

Does that make sense?

Priscilla
------------------------------------------------------------------
Priscilla Walmsley
Author, Definitive XML Schema  /  XML in Office 2003
http://www.datypic.com
------------------------------------------------------------------

> Hi Priscilla,
>
[quoted text clipped - 34 lines]
> Thanks a lot,
> Jeffry
Jeffry van de Vuurst - 29 Nov 2004 20:32 GMT
Oh stupid me, I meant to say this:

<xs:complexType name="ExtendedBookType">
 <xs:complexContent>
  <xs:extension base="BookType">
  </xs:extension>
 </xs:complexContent>
</xs:complexType>

<xs:element name="Book" type="ExtendedBookType"
substitutionGroup="Publication">
</xs:element>

But that doesn't make a difference, because of your explanation:
  PublicationType
        |              |
 anon1           BookType
                          |
                         anon2

So in the case of what I meant to say, this diagram would look like
PublicationType
   |                |
anon1        BookType
                   |
               ExtendedBookType

Also in this case the type of Book (ExtendedBookType) doesn't derive from
the type of Publication (anon1).

I think to point is, which I didn't know and you made perfectly clear, that
if an element extends a type, it now has an anonymous type that extends from
that type. And that's what causes all the confusion. Right? I hope so :)

Thanks you very much for your explanation.

Jeffry

> Hi Jeffry,
>
[quoted text clipped - 103 lines]
>> Thanks a lot,
>> Jeffry
Priscilla Walmsley - 29 Nov 2004 22:47 GMT
Hi Jeffry,

> I think to point is, which I didn't know and you made perfectly clear,
> that if an element extends a type, it now has an anonymous type that
> extends from that type. And that's what causes all the confusion. Right? I
> hope so :)

Exactly!

Priscilla

------------------------------------------------------------------
Priscilla Walmsley
Author, Definitive XML Schema  /  XML in Office 2003
http://www.datypic.com
------------------------------------------------------------------

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.