ILOG JRules User Guide > Integrating Application Data > Reference > Mapping Between XML Schema and Dynamic Classes > XML Declarations > Groups

Group mapping is presented here in three subsections:

In each of the following sections, an example is used to demonstrate the different types of mapping. The examples include a schema, an XML document, and the XOM representation. The first two examples also include rules to help clarify the explanation.

Choice Groups

A choice group is an XSD group of elements. Only one of them will appear in the XML document related to the group schema.

Additional dynamic methods are generated to help handle the choice group. For example, the dynamic method getChoice returns the dynamic attribute value selected in a group, and the dynamic method getChosenAttribute returns the dynamic attribute name selected in a group. The String parameter helps to locate the group where there are multiple groups in a complex type. The selected group is identified by the XOM name of a field of one of its children. When using the method without an argument, the first choice is selected by deep search.

The following example uses a complex type "observation" that contains three elements: temperature, pressure, and velocity.

Here is the schema:

<complexType name="observation">
  <choice>
    <element name="temperature" type="double"/>
    <element name="pressure" type="double"/>
    <element name="velocity" type="double"/>
  </choice>
</complexType>

Here is the excerpt from an XML document:

<observation>
  <pressure>10.0</pressure>
</observation>

Here is the XOM representation:

class Observation extends IlrXmlObject
{
  double temperature ;
  double pressure ;
  double velocity ;
...
  // choice dynamic methods
  Object getChoice ( String attrName ) ;
  String getChosenAttribute ( String attrName );
  Object getChoice();
  String getChosenAttribute();
}

And here are the rules:

rule findTemperatureObservation
{
   when {
      Observation ( temperature isknown );
   }
   then {
   ...
   }
}
rule processObservationValue
{
   when {
      obs: Observation ( );
      v: Double () from obs.getChoice ( );
      attr: String () from obs.getChosenAttribute ( );
   }
   then {
   ...
   }
}

Composite Groups

A composite group is a combination of elements and groups.

Note
The W3C specification does not define the concept of a composite complex type. It has been introduced here to describe a new mapping state, separate from the sequence or choice groups.

The following example uses the complex type "observation" from the previous section. This time, observation has been given an additional choice group with two options: error and comment.

Here is the schema:

<complexType name="observation">
  <sequence>
    <choice>
     <element name="temperature" type="double"/>
     <element name="pressure" type="double"/>
     <element name="velocity" type="double"/>
    </choice>
    <choice>
      <element name="error" type="double"/>
      <element name="comment" type="string" />
    </choice>
  <sequence>
</complexType>

Here is the excerpt from an XML document:

<observation>
  <pressure>10</pressure>
  <error>0.2</error>
</observation>

Here is the XOM representation:

class Observation extends IlrXmlObject
{
  double temperature ;
  double pressure ;
  double velocity ;
  double error;
  String comment;
...
  // choice dynamic methods
  Object getChoice ( String attrName ) ;
  String getChosenAttribute ( String attrName );
}

And here are the rules:

rule findTemperatureWithErrorObservation
{
   when {
      Observation ( temperature isknown; error isknown );
   }
   then {
   ...
   }
}
rule processObservationValue
{
   when {
      obs: Observation ( );
      v: Double () from obs.getChoice ( "velocity" );
      attr: String () from obs.getChosenAttribute ( "velocity" );
   }
   then {
   ...
   }
}
rule displayErrorOrComment
{
   when {
      obs: Observation ( );
      c: Object () from obs.getChoice ( "error" );
      attr: String () from obs.getChosenAttribute ( "error" );
   }
   then {
     out.println ( attr + '=' + c.toString() );
   }
}

Collection Groups

The following example demonstrates how to map a collection of choice groups, again using the complex type observation used in the previous two sections. The collection of the choice group is defined in the complex type with maxOccurs="unbounded".

Here is the schema:

<complexType name="observations">
  <choice maxOccurs="unbounded">
    <element name="temperature" type="double"/>
    <element name="pressure" type="double"/>
    <element name="velocity" type="double"/>
  <choice>
</complexType>

Here is the excerpt from an XML document:

<observations>
  <velocity>12.3</velocity>
  <temperature>11.5</temperature>
  <velocity>9.2</velocity>
</observations>

And here is the XOM representation:

class Observations extends IlrXmlObject 
{
  Vector temperatureList;
  Vector pressureList;
  Vector velocityList;
...
}

Related Reference

Elements
Attributes
Appinfo
Schema Member to Dynamic Class Field Name
XML Binding