Jump to content

Nesting Complex Types in XSD


Wylbur

Recommended Posts

Hi all;I have a question about XSD Complex Elements.I have read the related page on the w3schools site:

	XSD Complex ElementsA complex element contains other elements and/or attributes.What is a Complex Element?A complex element is an XML element that contains other elements and/or attributes.There are four kinds of complex elements:    * empty elements    * elements that contain only other elements    * elements that contain only text    * elements that contain both other elements and textNote: Each of these elements may contain attributes as well!Examples of Complex ElementsA complex XML element, "product", which is empty:<product pid="1345"/>A complex XML element, "employee", which contains only other elements:<employee>  <firstname>John</firstname>  <lastname>Smith</lastname></employee>A complex XML element, "food", which contains only text:<food type="dessert">Ice cream</food>A complex XML element, "description", which contains both elements and text:<description>It happened on <date lang="norwegian">03.03.99</date> ....</description>How to Define a Complex ElementLook at this complex XML element, "employee", which contains only other elements:<employee>  <firstname>John</firstname>  <lastname>Smith</lastname></employee>We can define a complex element in an XML Schema two different ways:1. The "employee" element can be declared directly by naming the element, like this:<xs:element name="employee">  <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:sequence>  </xs:complexType></xs:element>If you use the method described above, only the "employee" element can use the specified complex type. Note that the child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared. You will learn more about indicators in the XSD Indicators chapter.2. The "employee" element can have a type attribute that refers to the name of the complex type to use:<xs:element name="employee" type="personinfo"/><xs:complexType name="personinfo">  <xs:sequence>    <xs:element name="firstname" type="xs:string"/>    <xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType>If you use the method described above, several elements can refer to the same complex type, like this:<xs:element name="employee" type="personinfo"/><xs:element name="student" type="personinfo"/><xs:element name="member" type="personinfo"/><xs:complexType name="personinfo">  <xs:sequence>    <xs:element name="firstname" type="xs:string"/>    <xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType>You can also base a complex element on an existing complex element and add some elements, like this:<xs:element name="employee" type="fullpersoninfo"/><xs:complexType name="personinfo">  <xs:sequence>    <xs:element name="firstname" type="xs:string"/>    <xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="fullpersoninfo">  <xs:complexContent>    <xs:extension base="personinfo">      <xs:sequence>        <xs:element name="address" type="xs:string"/>        <xs:element name="city" type="xs:string"/>        <xs:element name="country" type="xs:string"/>      </xs:sequence>    </xs:extension>  </xs:complexContent></xs:complexType>

------------------------------------------------------http://www.w3schools.com/schema/schema_complex.asp======================================================The above is all well and good, but what if I wantto nest multiple complex elements within anothercomplex element?Would this be the proper way to do it?:

<xs:complexType name="personinfo">  <xs:sequence>	<xs:element name="firstname" type="xs:string"/>	<xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="AddressRef">  <xs:sequence>		<xs:element name="address" type="xs:string"/>		<xs:element name="city" type="xs:string"/>		<xs:element name="country" type="xs:string"/>		<xs:element name="Postal_Code" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="PhoneNumbers">  <xs:sequence>		<xs:element name="home" type="xs:string"/>		<xs:element name="work" type="xs:string"/>		<xs:element name="cell" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="fullpersoninfo">  <xs:complexContent>	<xs:extension base="personinfo"/>	<xs:extension base="AddressRef"/>	<xs:extension base="PhoneNumbers"/>  </xs:complexContent></xs:complexType>

What about this?:

<xs:complexType name="personinfo">  <xs:sequence>	<xs:element name="firstname" type="xs:string"/>	<xs:element name="middlename" type="xs:string"/>	<xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="AddressRef">  <xs:sequence>		<xs:element name="address" type="xs:string"/>		<xs:element name="city" type="xs:string"/>		<xs:element name="country" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="PhoneNumbers">  <xs:sequence>		<xs:element name="home" type="xs:string"/>		<xs:element name="work" type="xs:string"/>		<xs:element name="cell" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="fullpersoninfo">  <xs:complexContent>	<xs:group ref="personinfo"/>	<xs:group ref="AddressRef"/>	<xs:group ref="PhoneNumbers"/>  </xs:complexContent></xs:complexType>

If the above will work, then what if I want to addsomething to one of the nested complex types.What about this?:

<xs:complexType name="personinfo">  <xs:sequence>	<xs:element name="firstname" type="xs:string"/>	<xs:element name="middlename" type="xs:string"/>	<xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="AddressRef">  <xs:sequence>		<xs:element name="address" type="xs:string"/>		<xs:element name="city" type="xs:string"/>		<xs:element name="country" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="PhoneNumbers">  <xs:sequence>		<xs:element name="home" type="xs:string"/>		<xs:element name="work" type="xs:string"/>		<xs:element name="cell" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="fullpersoninfo">  <xs:complexContent>	<xs:group ref="personinfo"/>	<xs:extension base="AddressRef"/>	  <xs:sequence>		<xs:element name="Coounty" type="xs:string"/>		<xs:element name="Post_Code" type="xs:string"/>	  </xs:sequence>	</xs:extension>	<xs:group ref="PhoneNumbers"/>  </xs:complexContent></xs:complexType>

Thanks!!!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...