views:

51

answers:

3

Hi, I've an XML file like the one below

 <transaction name="TEST_EX" type="singletonComplex">
  <vo class="svc.vo.UserProfile">
   <field deepMapping="true">
    <vo class="svc.vo.UserVO">
     <field name="UserName" column="User_Name" type="String"/>
     <field name="Age" column="User_Age" type="Integer"/>
    </vo>
   </field>
   <field name="Address1" column="Address_1" type="String"/>
   <field name="Address2" column="Address_2" type="String"/>
  </vo>
 </transaction>

Inside there are couple of fields. Some of the fields has deepMapping enabled to true.I want' to make sure that, if deepMapping is enabled, then it should be listed first, ie ahead of "Address1" and "Address2". I've created an XSD file, but I'm not sure how I can implement this in that XSD file. Please help me.

+2  A: 

You cannot make the contents of an element (or attribute) dependent on the contents of another. XML Schema does not support this. You will have to use some other validation mechanism on top of it.

xcut
A: 

As xcut said, you can't do this with XML Schema. What you could do is use two different element names with the same complex type:

 <xs:complexType name="fieldType">
    <xs:sequence>
      <xs:element name="field" maxOccurs="unbounded">
        <!--  .... -->
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="vo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="deepMappedField" type="fieldType" minOccurs="0"/>
        <xs:element name="field" type="fieldType" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
Tim Jansen
Thanks everyone for your kind response. I'll definitely check the alternative approaches suggested.
Appps
+1  A: 

Are you able to change the XML structure? If yes, I would suggest adding a new XML element to group all fields with enabled deepMapping, e.g.:

 <transaction name="TEST_EX" type="singletonComplex">
  <vo class="svc.vo.UserProfile">
   <deepMapping>
    <vo class="svc.vo.UserVO">
     <field name="UserName" column="User_Name" type="String"/>
     <field name="Age" column="User_Age" type="Integer"/>
    </vo>
   </deepMapping>
   <field name="Address1" column="Address_1" type="String"/>
   <field name="Address2" column="Address_2" type="String"/>
  </vo>
 </transaction>

Your request to put all fields with deepMapping="true" at the top is more or less a grouping of fields.

pitpod