Re: Self reference in an element

Including the "dept" structure recursively within itself, looks a bit
inefficient to me.

I think you want your schema to say that, a "dept" can include other
departments. Since you already have the attribute "num" on "dept", you
can used XSD identity-constraints to enforce uniqueness and
referential integrity for department numbers (i.e @num).

I would propose a schema like following for this use case:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="Organization">
    <xs:complexType>
          <xs:sequence>
        <xs:element name="dept" type="Department" maxOccurs="unbounded"/>
   </xs:sequence>
    </xs:complexType>
    <xs:key name="dept_key">
          <xs:selector xpath="dept"/>
   <xs:field xpath="@num"/>
    </xs:key>
    <xs:keyref name="prnt_dept_ref" refer="dept_key">
          <xs:selector xpath="dept"/>
   <xs:field xpath="parent"/>
    </xs:keyref>
     </xs:element>
 
     <xs:complexType name="Department">
    <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="total_emp" type="xs:positiveInteger"/>
    <xs:element name="parent" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="num" type="xs:string"/>
    </xs:complexType>
 
</xs:schema>


A sample instance for this schema is following,

<Organization>
     <dept num="d1">   <!-- this "dept" doesn't have a parent department -->
          <name>dept 1</name>
   <total_emp>5</total_emp>
     </dept>
     <dept num="d2">
          <name>dept 2</name>
   <total_emp>8</total_emp>
   <parent>d1</parent>
     </dept>
     <dept num="d3">
         <name>dept 3</name>
  <total_emp>15</total_emp>
  <parent>d1</parent>
     </dept>
     <dept num="d4">
         <name>dept 4</name>
  <total_emp>7</total_emp>
  <parent>d2</parent>
     </dept>
</Organization>

I hope this helps.

On Tue, Jul 12, 2011 at 11:50 AM, Avinash Nagabhushan
<avinash.sit@gmail.com> wrote:
> Hi Sir,
>
>       I am writing an schema for the following structure:
>
> Organization
>    dept +
>        @num
>        name
>        total_emp
>        dept +
> where dept elements are nested within another dept element, but all
> departments are under organization.
>
>
> My Schema is:
>
>
> <xs:schema xmlns:ns1="http://www.ns1.com"
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.ns1.com">
>        <xs:element name="Organization">
>                <xs:complexType>
>                        <xs:sequence>
>                                <xs:element name="Department" maxOccurs="unbounded">
>                                        <xs:complexType>
>                                                <xs:sequence>
>                                                        <xs:element name="Name"/>
>                                                        <xs:element name="TotalEmp"/>
>                                                        <xs:element ref="ns1:Department" minOccurs="0" maxOccurs="unbounded"/>
>                                                </xs:sequence>
>                                                <xs:attribute name="num" type="xs:string"/>
>                                        </xs:complexType>
>                                </xs:element>
>                        </xs:sequence>
>                </xs:complexType>
>        </xs:element>
> </xs:schema>
>
>
> There is a validation error saying "ns1:Department" is not a QName and
> cannot find declaration for the same. Is there something wrong in my
> definition?
>
>
> Can you please let me know whether it is a correct approach or wrong?
>
>
> Thanks,
>
> Avinash K N





-- 
Regards,
Mukul Gandhi

Received on Tuesday, 12 July 2011 13:38:34 UTC