Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > complexTypes versus elements

Reply
Thread Tools

complexTypes versus elements

 
 
John LaRusic
Guest
Posts: n/a
 
      03-21-2007
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"
xmlnss="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="myoubleField" />
<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"
xmlnss="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"
xmlnss="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

 
Reply With Quote
 
 
 
 
Joseph Kesselman
Guest
Posts: n/a
 
      03-21-2007
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> .... </RootElement> is an element which
has a complex type that says it contains a specific sequence of other
elements.
 
Reply With Quote
 
 
 
 
Joseph Kesselman
Guest
Posts: n/a
 
      03-21-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
Deriving complexTypes by restriction. bclark76@gmail.com XML 5 11-18-2005 02:55 AM
repost-.Net XmlValidatingReader should fail on complextypes withwhitespace? Larry XML 1 06-10-2004 06:55 PM
.Net XmlValidatingReader should fail on complextypes with whitespace? Larry XML 0 06-04-2004 12:59 PM



Advertisments