ILOG JRules User Guide > Integrating Application Data > Concepts > XML Binding > Stage 1: XML Schema Processing

At compile time, the XML schema is processed using a schema driver (IlrXmlSchemaDriver). The schema driver converts the schema to dynamic data structures to create a XOM. This process is required in order to present the XML data in a format that the rule engine can read. The XOM is represented by the Java class IlrReflect.

images/xmlschemaprocessing.png

In some cases there will be a direct mapping from schema components to existing classes, especially for common types such as String, Date, Vector, and so forth. XSD complex types, however, are converted to dynamic user classes specific to the schema being used.

The following table outlines the XSD to XOM mapping that takes place during schema processing. For more information on mapping, see the reference section Mapping Between XML Schema and Dynamic Classes.

Table 1 XSD to XOM Mapping
XSD 
XOM 
Complex type 
XOM dynamic class 
Elements, Attributes, Content 
Typed fields 
Complex inheritance (restriction, extension) 
Inheritance 
Built-in simple type 
Predefined Java type (primitive, object) 
Restricted simple type 
Predefined Java type 
Facets 
XOM static information 
Application information (Appinfo) 
XOM property 

The following sections show examples of how JRules converts a simple and then a more complex schema to a dynamic class.

Simple Content Example

The following example shows how an address element is converted to a dynamic class Address.

Here is the schema:

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ilog.fr/Schema/Examples/simpleSchema1">
   <element name="address">
      <complexType>
         <sequence>
         <element name="city" type="string"/>
         <element name="zipcode" type="integer"/>
         </sequence>
         <attribute name="id" type="ID"/>
      </complexType>
   </element>
</schema>

Here is the XML document:

<?xml version="1.0"?>
<address xmlns = "http://www.ilog.fr/Schema/Examples/simpleSchema1">
   <city>New York</city>
   <zipcode>11275</zipcode>
</address>

Every dynamic class that is created from a schema extends the IlrXmlObject class, as the Address class demonstrates:

class Address extends IlrXmlObject
{
   String id;
   String city;
   int zipcode;
}

After the dynamic objects have been instantiated, a rule can be applied on address objects and any fields tested. For example, trying to match an address in the city Chicago.

...
rule Address
...
      ?a : address(city equals "Chicago")
...

Extended Schema Example

The following example extends the Simple Content example by adding a street element. Here is the schema element:

<element name="street" >
      <complexType>
         <sequence>
            <element name="number" type="int"/>
            <element name="name" type="string"/>
         </sequence>
      </complexType>
 </element>

With a reference to this element in the previous address element as <element ref="street"/>:

<element name="address" >
   <complexType>
      <sequence>
         <element ref="street"/>
         <element name="city" type="string"/>
         <element name="zipcode" type="integer"/>
      </sequence>
      <attribute name="id" type="ID"/>
   </complexType>
</element>

The XML document now contains a street number and name:

<street>
   <number>32</number>
   <name>Elma Street</name>
</street>

The translation contains a new class Street, and the class Address contains a field called street, of type Street:

class Street extends IlrXmlObject
{
   int number;
   String name;
}
class Address extends IlrXmlObject
{
   ...
   Street street;
}

There are more ways of defining elements in schemas than there are in Java, so numerous schema definitions may map to the same dynamic class structure. For example, the following two lines of code give the same result for the XML document (though not necessarily for the XOM):

<element name="street" type="street">
<complexType name="street">

In this example, to indicate that the definition of the XSD type street must be continued, the address element is converted into a dynamic class Address. The city and zipcode subelements become fields of this class as the id schema attribute.

A name must be unique in a schema definition in order to be converted correctly. If you define a type/group/attribute/element with the same name in a schema, an error will be raiseds (unless you use a redefine instruction).

Related Concepts

XML Binding
Stage 2: XML Document Processing
Web Service Binding
Third Party Data Access

Related Tasks

Using the XML Binding API
Using the Dynamic XOM API
Defining the Execution Object Model

Related Reference

Mapping Between XML Schema and Dynamic Classes

Related Samples and Tutorials

How to Run Rules Against XML Objects