473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

XML Polymorphism through inheritance?

Hi all,
I've got a DTD which I'm trying to tidy up and improve into an XSD - I'm
battling with an area of object orientation which I'm used to in Java, but
unsure about whether I can do it in XML:

I've got a container which currently contains a Choice element listing all
of children it can contain. Whenever I add a new child I have to remember
to extend this choice definition.

Since all of the children are extensions of a single base-type I was hoping
that there would be some way of defining that my container just contains
multiple instances of the base-type (or it's children). This type of thing
seems quite normal in OOP, I just don't know if I'm asking too much of XSDs.

Cheers,
Ian
Jul 20 '05 #1
4 2719
"Ian Mayo" <Ia*****@hotmail.com> writes:
Hi all,
I've got a DTD which I'm trying to tidy up and improve into an XSD - I'm
battling with an area of object orientation which I'm used to in Java, but
unsure about whether I can do it in XML:

I've got a container which currently contains a Choice element listing all
of children it can contain. Whenever I add a new child I have to remember
to extend this choice definition.

Since all of the children are extensions of a single base-type I was hoping
that there would be some way of defining that my container just contains
multiple instances of the base-type (or it's children). This type of thing
seems quite normal in OOP, I just don't know if I'm asking too much of XSDs.


Yes. Define your container type as containing 1 occurrence
(or however many you want) of top-level element foo, of type Foo.

Now for each of the possible kinds of children, specify that
they are in the substitution group of element foo.

E.g. <xs:element name="bar" type="my:Foo" substitutionGroup="my:foo"/>
<xs:element name="baz" type="my:Foo2" substitutionGroup="my:foo"/>

Constraints:
- the 'foo' element needs to be top-level, not local (so
that other element declarations can point at it and say
"That's my substitution-group head over there")
- the type of each child needs to be derived from type Foo
(you mention that this is the case, so it shouldn't be
a problem)

In some cases, you may wish to make the foo element itself
abstract, so that it cannot be instantiated (so only my:bar
and my:baz can occur, not my:foo).

I hope this helps.

-C. M. Sperberg-McQueen
World Wide Web Consortium
Jul 20 '05 #2
A thousand thank-you's Michael.

I can't admit to completely understading the answer, but now I know to
investigate Substitution Groups in my O'Reilly XML Schema book - especially
now I know it certainly is "do-able".

Thanks again,
Ian Mayo

"C. M. Sperberg-McQueen" <cm****@acm.org> wrote in message
news:87************@acm.org...
"Ian Mayo" <Ia*****@hotmail.com> writes:
I hope this helps.

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

Jul 20 '05 #3
Hello Ian,

If you want to use the globally defined elements <A> and <B>, change the
following content model

<xs:complexType name="ListTypeB">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="A" type="TypeA"/>
<xs:element name="B" type="TypeB"/>
</xs:choice>
</xs:complexType>

to:

<xs:complexType name="ListTypeB">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="A" />
<xs:element ref="B" />
</xs:choice>
</xs:complexType>

There is a difference bettwen the two content models. The top defines
and use local element <A> and <B>. They are not the same as the two
global element defintion. Think of it as defining a local variable in
a method with the same name as global variable in a class.

Kind regards,
Eric
Ian Mayo wrote:
Thanks again for that advice Michael.

It's worked fine for defining my structure to model "the real world".

Unfortunately my XML editor (XMLSpy) doesn't appear to be property
recognising the model, but there's a strong chance that this is caused by my
incorrect schema definition.

In the sample xsd (below) a scenario object can contain a ListA - which
contains multiple instances of a Model (or child thereof). It can a ListB
which includes instances of the same objects (defined using my "old" way of
doing it).

My editor can see that into ListA I can but objects of type A or type B, but
it isn't aware of their properties = whereas when I insert them into ListB -
it provides direct access to their properties.

Is this because my schema definition is too vague to provide it with that
information? Or is this in fact a problem with my XML editor?

Many thanks in advance,

Ian Mayo

================================================== ===========

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by Ian Mayo
(PlanetMayo Ltd) -->
<!--W3C Schema generated by XMLSPY v5 rel. 4 U (http://www.xmlspy.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xs:element name="Scenario">
<xs:annotation>
<xs:documentation>Container for a chain</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="ListA" type="ListTypeA"/>
<xs:element name="ListB" type="ListTypeB"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="ListTypeA">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="Model" />
</xs:choice>
</xs:complexType>

<xs:complexType name="ListTypeB">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="A" type="TypeA"/>
<xs:element name="B" type="TypeB"/>
</xs:choice>
</xs:complexType>

<!-- -->
<!-- DECISION MODELS -->
<!-- -->
<xs:element name="Model" abstract="true"/>
<xs:complexType name="ModelType">
<xs:annotation>
<xs:documentation>Base type for models</xs:documentation>
</xs:annotation>
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:complexType>

<xs:element name="A" type="TypeA" substitutionGroup="Model"/>
<xs:complexType name="TypeA">
<xs:complexContent>
<xs:extension base="ModelType">
<xs:attribute name="attA_A" type="xs:float" use="required"/>
<xs:attribute name="attA_B" type="xs:float" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:element name="B" type="TypeB" substitutionGroup="Model"/>

<xs:complexType name="TypeB">
<xs:complexContent>
<xs:extension base="ModelType">
<xs:attribute name="attB_A" type="xs:string" use="required"/>
<xs:attribute name="attB_B" type="xs:string" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>


Jul 20 '05 #4
Thanks for coming back to me on this Eric,

but my challenge is how to avoid having to specify in the schema that I am
allowing objects of type A or type B, I just want to specify that elements
of type Model (or members of that substitution group) can be inserted into
the list.

This will prevent me having to remember to change the ListType definition
when I add a new model type. In my sample, the ListB element works ok (and
that has been the way I've always done it), I just want to move forward to
specifying sub-elements using the ListB type definition.

Cheers,
Ian
"Eric Sirois" <es*********************@ca.ibm.com> wrote in message
news:bg**********@hanover.torolab.ibm.com...
Hello Ian,

If you want to use the globally defined elements <A> and <B>, change the
following content model

<xs:complexType name="ListTypeB">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="A" type="TypeA"/>
<xs:element name="B" type="TypeB"/>
</xs:choice>
</xs:complexType>

to:

<xs:complexType name="ListTypeB">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="A" />
<xs:element ref="B" />
</xs:choice>
</xs:complexType>

There is a difference bettwen the two content models. The top defines
and use local element <A> and <B>. They are not the same as the two
global element defintion. Think of it as defining a local variable in
a method with the same name as global variable in a class.

Kind regards,
Eric
Ian Mayo wrote:
Thanks again for that advice Michael.

It's worked fine for defining my structure to model "the real world".

Unfortunately my XML editor (XMLSpy) doesn't appear to be property
recognising the model, but there's a strong chance that this is caused by my incorrect schema definition.

In the sample xsd (below) a scenario object can contain a ListA - which
contains multiple instances of a Model (or child thereof). It can a ListB which includes instances of the same objects (defined using my "old" way of doing it).

My editor can see that into ListA I can but objects of type A or type B, but it isn't aware of their properties = whereas when I insert them into ListB - it provides direct access to their properties.

Is this because my schema definition is too vague to provide it with that information? Or is this in fact a problem with my XML editor?

Many thanks in advance,

Ian Mayo

================================================== ===========

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by Ian Mayo
(PlanetMayo Ltd) -->
<!--W3C Schema generated by XMLSPY v5 rel. 4 U (http://www.xmlspy.com)--> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xs:element name="Scenario">
<xs:annotation>
<xs:documentation>Container for a chain</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="ListA" type="ListTypeA"/>
<xs:element name="ListB" type="ListTypeB"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="ListTypeA">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="Model" />
</xs:choice>
</xs:complexType>

<xs:complexType name="ListTypeB">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="A" type="TypeA"/>
<xs:element name="B" type="TypeB"/>
</xs:choice>
</xs:complexType>

<!-- -->
<!-- DECISION MODELS -->
<!-- -->
<xs:element name="Model" abstract="true"/>
<xs:complexType name="ModelType">
<xs:annotation>
<xs:documentation>Base type for models</xs:documentation>
</xs:annotation>
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:complexType>

<xs:element name="A" type="TypeA" substitutionGroup="Model"/>
<xs:complexType name="TypeA">
<xs:complexContent>
<xs:extension base="ModelType">
<xs:attribute name="attA_A" type="xs:float" use="required"/>
<xs:attribute name="attA_B" type="xs:float" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:element name="B" type="TypeB" substitutionGroup="Model"/>

<xs:complexType name="TypeB">
<xs:complexContent>
<xs:extension base="ModelType">
<xs:attribute name="attB_A" type="xs:string" use="required"/>
<xs:attribute name="attB_B" type="xs:string" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
3
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
10
by: Lino Barreca | last post by:
Take a look at this code: Class clsAnagrafica Public Overridable ReadOnly Property Codice() As Integer Get Return 1 End Get End Property End Class
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
8
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the...
1
by: mattmao | last post by:
I am brand new to C#.NET so here is my trial on this lab exercise: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace lab02exec { ...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.