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

complexTypes versus elements

Hi all,

I'm fairly new to the world of schemas, but I have a question that I
hope someone can help answer for me. I'm curious as to what the
difference is between an element and a complexType?

I know an element can be a complexType, so I guess what my issue is
when I should define a complexType that's not contained in an element
block, and when I should define a top-level element (what I mean by
that is when I should define an element block right under the
xs:schema node).

I know I can define schemas in any of the following ways and that
they're more or less the same schema:

Schema1:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://my.company.org/SampleSchema1"
elementFormDefault="qualified"
xmlns:my="http://my.company.org/SampleSchema1"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="StringField" type="xs:string"/>
<xs:element name="IntegerField" nillable="true" type="xs:integer"/>
<xs:element name="DoubleField" nillable="true" type="xs:double"/>
<xs:element name="BooleanField" nillable="true" type="xs:boolean"/>

<xs:element name="RootElement">
<xs:complexType>
<xs:sequence>
<xs:element ref="my:StringField" />
<xs:element ref="my:IntegerField" />
<xs:element ref="my:DoubleField" />
<xs:element ref="my:BooleanField" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

Schema 2:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://my.company.org/SampleSchema2"
elementFormDefault="qualified"
xmlns:my="http://my.company.org/SampleSchema2"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="RootElement">
<xs:complexType>
<xs:sequence>
<xs:element name="StringField" type="xs:string"/>
<xs:element name="IntegerField" type="xs:integer"/>
<xs:element name="DoubleField" type="xs:double"/>
<xs:element name="BooleanField" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

Schema 3:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://my.company.org/SampleSchema3"
elementFormDefault="qualified"
xmlns:my="http://my.company.org/SampleSchema3"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType name="RootElementType">
<xs:sequence>
<xs:element name="StringField" type="xs:string"/>
<xs:element name="IntegerField" type="xs:integer"/>
<xs:element name="DoubleField" type="xs:double"/>
<xs:element name="BooleanField" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>

<xs:element name="RootElement" type="my:RootElementType" />

</xs:schema>

But which way is the most "correct"? My gut tells me to specify
everything as complexType independent of any particular element, and
only define top-level elements where I absolutely need them, but it
seems most tools like to define everything as top-level elements.

I hope this makes sense. It seems like there are lots of tutorials on
how to create schemas, but I can't seem to find much on best practices
in terms of how those schemas should be defined. Any help would be
appreciated!

Thank you,

John LaRusic
Developer, Atlantic Canada Opportunities Agency

Mar 21 '07 #1
2 1688
An element is the thing made up of a start-tag and an end-tag, which
contains attributes and children. An element's value may have a type;
that type may be a complex type which specifies those attributes and
children.

In your example, <RootElement.... </RootElementis an element which
has a complex type that says it contains a specific sequence of other
elements.
Mar 21 '07 #2
John LaRusic wrote:
I know an element can be a complexType
No. An element is an element. It can have a type assigned by the schema;
that type may either be a simple type or a complex type.
when I should define a complexType that's not contained in an element
block
Do this when there will be several references to the same complex type,
or when it's just clearer to describe it that way.

If you've worked in C, this is the same question as when to use typedef
versus when to just define the struct in-line. Or, in Java, when to use
a stand-alone class definition versus when to use an anonymous class.
There are times when you must give the type a name, there are times when
you don't have to but want to, and there are times when it really is
simplest to just do it where it's being used and not worry about it.
Picking between these is a matter of style and judgement, and that comes
with practice.

If you really can't decide, give the type a name. That may not be the
cleanest answer but it preserves all your options.
But which way is the most "correct"?
Wrong question. Which is clearest, and which allows you to do what you
intend to do later with this document type... and sometimes it honestly
makes no difference.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 21 '07 #3

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

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
12
by: Alan J. Flavell | last post by:
OK, today's email brought a comment from a reader, pointing out something that I'd long since noticed myself but hadn't done anything about it. On my pages, I've got a first-letter style on...
9
by: Michel | last post by:
I'm renewing my interest in CSS and downloaded some stylesheets. However, the .css uses both an ID and class. It looks like something like this. ..class1 { /* CSS code */ } #styleID1 { /*...
0
by: Paula DiTallo | last post by:
Hello Techies-- I am reading an XML document with many nested elements. Many of these elements go to such a variety of tables, that I am trying to simply the read of the document by dumping the...
4
by: Larry | last post by:
I believe the .Net XmlValidatingReader should fail when validating XML that contains a ComplexType element with white space when the ComplexType element has the mixed attribute set to false in the...
5
by: Paul | last post by:
Off the cuff, does anyone know if arraylist is more efficeint at adding items to an array than redim preserve? Paul <begin loop> Dim c As Integer = SomeArray.GetUpperBound(0) + 1 ReDim...
5
by: bclark76 | last post by:
I am getting a strange error, maybe someone knows why it is occurring.. I get the following error when I try to validate Untitled8.xml in Altova XMLSPY: Validation error in another file:...
11
by: David Stone | last post by:
I'm trying to figure out how to avoid tables when presenting a sidebar on an html 4 strict page, by using div instead. I've run into a situation where I get different rendering of the elements...
0
by: Andrew 2006 | last post by:
I have a wsdl definition that includes the following definition for the element <Preferencesinside the request createFooBar: <xs:element minOccurs="1" name="Preferences" nillable="true"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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...

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.