473,387 Members | 1,455 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,387 software developers and data experts.

XSD: Extenstion, substituion, and recursion, Oh My!

(Background and details are first, with my questions at the end.)

Consider the following hierarchy:

Foo (abstract base)
FooDerived1
FooDerived2
<...>
Bar

At runtime, any Foo instance may have one-or-more Foo children or Bar
instances.

For example:

FooDerived1
Bar
FooDerived1
Bar
Bar
FooDerived2
Bar
FooDerived1
Bar
Bar
I'm trying to represent the above via XML Schema (I'm just learning, and
have only been at this for a couple of days), and have come up with two
possibilities (root element and other minor details omitted):

OPTION 1 (XSD)

<xs:simpleType name="Bar">
<xs:restriction base="xs:integer">
<xs:maxInclusive value="100"></xs:maxInclusive>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="Foo" abstract="true">
<xs:sequence maxOccurs="unbounded">
<xs:choice>
<xs:element name="FooDerived1" type="FooDerived1"/>
<xs:element name="FooDerived2" type="FooDerived2"/>
<xs:element name="Bar" type="Bar"/>
</xs:choice>
</xs:sequence>
</xs:complexType>

<xs:complexType name="FooDerived1">
<xs:complexContent>
<xs:extension base="Foo">
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="FooDerived2">
<xs:complexContent>
<xs:extension base="Foo">
</xs:extension>
</xs:complexContent>
</xs:complexType>
OPTION 2 (XSD)

<xs:simpleType name="Bar">
<xs:restriction base="xs:integer">
<xs:maxInclusive value="100"></xs:maxInclusive>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="Foo" abstract="true">
<xs:sequence maxOccurs="unbounded">
<xs:choice>
<xs:element name="Foo" type="Foo"/>
<xs:element name="Bar" type="Bar"/>
</xs:choice>
</xs:sequence>
</xs:complexType>

<xs:complexType name="FooDerived1">
<xs:complexContent>
<xs:extension base="Foo">
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="FooDerived2">
<xs:complexContent>
<xs:extension base="Foo">
</xs:extension>
</xs:complexContent>
</xs:complexType>

Both options are equally valid and represent the same structure, but the
XML syntax is different (OPTION 2 requires xsi:type):

OPTION 1 (XML)

<FooDerived1>
<Bar>1</Bar>
<FooDerived1>
<Bar>2</Bar>
<Bar>3</Bar>
</FooDerived1>
<FooDerived2>
<Bar>4</Bar>
<FooDerived1>
<Bar>5</Bar>
</FooDerived1>
</FooDerived2>
<Bar>6</Bar>
</FooDerived1>
OPTION 2 (XML)

<Foo xsi:type="FooDerived1">
<Bar>1</Bar>
<Foo xsi:type="FooDerived1">
<Bar>2</Bar>
<Bar>3</Bar>
</Foo>
<Foo xsi:type="FooDerived2">
<Bar>4</Bar>
<Foo xsi:type="FooDerived1">
<Bar>5</Bar>
</Foo>
</Foo>
<Bar>6</Bar>
</Foo>

I prefer the readability of OPTION 1 (XML), but I prefer the schema of
OPTION 2 (XSD), as it doesn't require any <xs:choice> blocks, which is
easier to maintain and extend in my mind -- simply add a new type
extended from Foo, and you're set. With OPTION 1 (XSD), you add the new
type the same way, but then you have to go add a line to the <xs:choice>
block as well.

Additionally, the XSD will only be worked on internally, most likely by
myself, so I'm not worried about flexible extension of the schema by
others; however, the XML document has a higher chance of being
implemented/modified by another individual.
QUESTIONS

1) Aside from the oddity of the hierarchy (which cannot easily be
rearchitected), does it look like I'm doing anything "wrong" so far with
the schema definition?

2) Is there a better way to achieve the same result? These types (both
Foo<x> and Bar) will be nested/used in other types, so I cannot take the
element/substitutionGroup route...

3) Which of the two solutions above is preferable? Opinions welcome.
Thank you for any input you can provide!

Jul 20 '05 #1
0 2151

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

Similar topics

5
by: Fu Bo Xia | last post by:
anyone know a Java (or Java usable) package that converts XML Schema (XSD) documents into a tree form? thanks, fu bo
5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
0
by: wooks | last post by:
<?xml version="1.0" ?> - <xsd:schema targetNamespace="urn:faster:userlogin" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:faster:userlogin"> <xsd:include...
1
by: Roland | last post by:
hi all having designed my schema to use repeating groups of elements, I found that some applications (eg. Microsoft InfoPath) refused to recognised that element as a repeating element. I...
3
by: man-in-nature | last post by:
Hello, I have already read several existing posts about xsd:extension, but do not find something useful to my test case. I have one xml file and one xsd file. I can use a simple command line...
4
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.