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"
elementFormDefa ult="qualified"
xmlns:my="http://my.company.org/SampleSchema1"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="StringFie ld" type="xs:string "/>
<xs:element name="IntegerFi eld" nillable="true" type="xs:intege r"/>
<xs:element name="DoubleFie ld" nillable="true" type="xs:double "/>
<xs:element name="BooleanFi eld" nillable="true" type="xs:boolea n"/>
<xs:element name="RootEleme nt">
<xs:complexType >
<xs:sequence>
<xs:element ref="my:StringF ield" />
<xs:element ref="my:Integer Field" />
<xs:element ref="my:DoubleF ield" />
<xs:element ref="my:Boolean Field" />
</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"
elementFormDefa ult="qualified"
xmlns:my="http://my.company.org/SampleSchema2"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="RootEleme nt">
<xs:complexType >
<xs:sequence>
<xs:element name="StringFie ld" type="xs:string "/>
<xs:element name="IntegerFi eld" type="xs:intege r"/>
<xs:element name="DoubleFie ld" type="xs:double "/>
<xs:element name="BooleanFi eld" type="xs:boolea n"/>
</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"
elementFormDefa ult="qualified"
xmlns:my="http://my.company.org/SampleSchema3"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexTy pe name="RootEleme ntType">
<xs:sequence>
<xs:element name="StringFie ld" type="xs:string "/>
<xs:element name="IntegerFi eld" type="xs:intege r"/>
<xs:element name="DoubleFie ld" type="xs:double "/>
<xs:element name="BooleanFi eld" type="xs:boolea n"/>
</xs:sequence>
</xs:complexType>
<xs:element name="RootEleme nt" type="my:RootEl ementType" />
</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