W3C home > Mailing lists > Public > xmlschema-dev@w3.org > July 2011

Re: Self reference in an element

From: Mukul Gandhi <gandhi.mukul@gmail.com>
Date: Tue, 12 Jul 2011 19:07:46 +0530
Message-ID: <CABuuzNOZmGGQ5Z4410Cn6yZwSw4Kmpxy+P_CQBdWCyZuEZ-B-g@mail.gmail.com>
To: Avinash Nagabhushan <avinash.sit@gmail.com>
Cc: xmlschema-dev@w3.org
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

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:15:59 UTC