Page 1 of 1

How do i declare 2 Global elements under 1 root element ?

Posted: Sun Aug 08, 2004 9:36 pm
by rajasrigupta2yahoo.com
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.XMLCourse.edu/targetNS"
xmlns="http://xml.XMLCourse.edu/targetNS"
elementFormDefault="qualified">

<!--
Requirements:

1. Atleast 2 complexTypes
2.Atleast 3 simpleTypes
3.Atleast 1 derived complexType
4.key and keyref
5.attributes optional or required
-->


<!--Global elements-->


<xsd:element name="Bank" >
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
<xsd:element name="Customer" type="internationalCustomerProfile"/>
<xsd:element name="Product" type="productDetails" />
</xsd:sequence>
</xsd:complexType>

<!--<xsd:complexType >
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
<xsd:element name="Product" type="productDetails" />
</xsd:sequence>
</xsd:complexType>-->

</xsd:element>

<!-- <xsd:element name="Product" type="productDetails"/>-->



<xsd:complexType name="profileDetails">
<xsd:sequence>

<!--<xsd:element name="Account" type="acc" maxOccurs="unbounded"/>-->
<xsd:element name="SSN" type="xsd:string"/>
<xsd:element name="Gender" type="gender"/>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="Country" type="xsd:string"/>
<xsd:element name="AccBal" type="xsd:positiveInteger" />
<xsd:element name="Dep" type="xsd:positiveInteger" maxOccurs="unbounded"/>
<xsd:element name="Wdl" type="xsd:positiveInteger" maxOccurs="unbounded"/>
</xsd:sequence>

<xsd:attribute name="Code" type="xsd:string" use="required" />
<xsd:attribute name="Title" type="xsd:string" use="optional"/>
<xsd:attribute name="Name" type="xsd:string" use="required"/>
<xsd:attribute name="City" type="xsd:string" use="required"/>
<xsd:attribute name="Zip" type="xsd:positiveInteger" use="optional"/>


</xsd:complexType>


<xsd:complexType name="internationalCustomerProfile">
<xsd:complexContent>
<xsd:extension base="profileDetails">
<xsd:sequence>
<xsd:element name="localCurrency" type="currencyType" minOccurs="0"/>
<xsd:element name="exchangeRate" type="xsd:double" minOccurs="0" />
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>




<!--This section defines element Product under Bank, all its children elements,attributes and use of key and keyref-->

<!-- <xsd:element name="Bank"> -->

<!--Try to use Substitution Group-->


<xsd:complexType name="productDetails">

<xsd:sequence>

<!-- <xsd:element name="Checking" type="xsd:string" maxOccurs="unbounded"/>-->


<xsd:element name="MinBal" type="xsd:positiveInteger"/>
<xsd:element name="Int" type="xsd:decimal"/>
<xsd:element name="Fees" type="xsd:positiveInteger"/>

</xsd:sequence>



<!--<xsd:element name="Savings" type="xsd:string" maxOccurs="unbounded"/>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MinBal" type="xsd:positiveInteger"/>
<xsd:element name="Int" type="xsd:positiveInteger"/>
<xsd:element name="Fees" type="xsd:positiveInteger"/>

</xsd:sequence>
</xsd:complexType>


<xsd:element name="CD" type="xsd:string" maxOccurs="unbounded"/>

<xsd:complexType>
<xsd:sequence>
<xsd:element name="MinBal" type="xsd:positiveInteger"/>
<xsd:element name="Int" type="xsd:positiveInteger"/>
<xsd:element name="Fees" type="xsd:positiveInteger"/>

</xsd:sequence>
</xsd:complexType>



<xsd:element name="IRA" type="xsd:string" maxOccurs="unbounded"/>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MinBal" type="xsd:positiveInteger"/>
<xsd:element name="Int" type="xsd:positiveInteger"/>
<xsd:element name="Fees" type="xsd:positiveInteger"/>

</xsd:sequence>
</xsd:complexType>-->

<xsd:attribute name="ID" type="xsd:string" use="required"/>
<xsd:attribute name="Name" type="xsd:string" use="required"/>
<xsd:attribute name="Type" type="xsd:string" use="optional"/>


</xsd:complexType>

<!--<xsd:complexType name="productFeatures">

<xsd:sequence>
<xsd:element name="MinBal" type="xsd:positiveInteger"/>
<xsd:element name="Int" type="xsd:positiveInteger"/>
<xsd:element name="Fees" type="xsd:positiveInteger"/>

</xsd:sequence>
</xsd:complexType> -->





<xsd:simpleType name="gender">
<xsd:restriction base="xsd:string">
<xsd:pattern value="Male|Female"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="currencyType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[A-Z]{3}"/>
</xsd:restriction>
</xsd:simpleType>


<!-- USE OF KEY AND KEYREF-->


<xsd:element name="Product">
<xsd:complexType>
<xsd:attribute name="ID" type="xsd:string"/>
</xsd:complexType>



<xsd:key name="productKey">
<xsd:selector xpath="Product"/>
<xsd:field xpath="@ID"/>
</xsd:key>
</xsd:element>


<xsd:element name="Customer">
<xsd:complexType>
<xsd:attribute name="Code" type="xsd:string"/>
</xsd:complexType>

<xsd:keyref name="myRef" refer="productKey">
<xsd:selector xpath="Customer"/>
<xsd:field xpath="@Code"/>
</xsd:keyref>
</xsd:element>



</xsd:schema>

Posted: Tue Aug 10, 2004 5:02 am
by george
Hi,

W3C XML Schema has no notion of root element, any glogal element can be the root of a document that uses that schema.

Global elements are defined as top level schema components, therefore they cannot be declared under some other element. Those are local elements.

If you want to specify the Bank element as a 1-unbounded sequence of Cutomer and Product and define these two elements as global elements in your schema then you can do it like below:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xml.XMLCourse.edu/targetNS" xmlns="http://xml.XMLCourse.edu/targetNS">
<xsd:element name="Bank">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
<xsd:element ref="Customer"/>
<xsd:element ref="Product"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Customer"/>
<xsd:element name="Product"/>
</xsd:schema>
Best Regards,
George