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