Retrieving XPath information on XML objects

You can use XPath syntax to locate an information in an XML document.

About this task

XPath is a standard means to locate a piece of information in an XML document. With some XML tools and libraries, you can use an XPath expression to locate, read, or set a value inside a complex document. Decision Server does not use XPath to locate information inside the document because it uses XML binding to map the document onto objects. However, after a rule has located a piece of information in the document, you might want to keep track of this location by using the XPath syntax so that you can reuse it in another tool.

For example, an XML document contains a list of financial transactions and you have built a ruleset to delete suspicious transactions. You can include the XPath value for these transactions. The calling application could then reuse these XPath locations to carry out further processing on the transactions by using other technologies that support XPath.

In Decision Server, the XPath expression is linked to IlrXmlObject when it is built from an XML document. With this expression, you can keep track of the original sub-tree of any XML object, although there is no feature to filter the IlrXmlObject tree by using an XPath expression.

XPath information is provided by the following methods:

  • IlrXmlObject.getRelativeXPath(boolean)

  • IlrXmlObject.getAbsoluteXPath(boolean)

These methods link the dynamic object to the original XML information location. This information is located when the IlrXmlDefaultDataDriver.readObject method is invoked.

In the following example, we assume that an instance of IlrXmlObject has been created from the first <c> complex element on an XML document.

Procedure

To get XPath information on an IlrXmlObject:

  1. Call the XPath information methods by using the following structure:
    <a>
      <b>
         <c>
             … //IlrXMLObject info
         </c>
         <c>
             …
         </c>
      </b>
    </a>
    

    The call to the XPath information methods leads to the following results. A is the mapping type of the <a> XML structure:

    var myObject = (A) driver.readObject ( xmlFileName );
    
    
    // navigation on the myObject tree to access the "c" structure
    // c collection elements are mapped on cList “XOM” attributes
    
    
    var c = myObject.b.cList.elementAt(0);
    
    
    c.getRelativeXPath ( false ); // "c"
    c.getRelativeXPath ( true ); // "c[0]"
    c.getAbsoluteXPath ( false ); // "a/b/c"
    c.getAbsoluteXPath ( true ); // "a/b/c[0]"
    
  2. Retrieve XPath information.

    The code written for this example uses a person object with the surname and address attributes, and two rules: displayAddressRelativeXPath and displayAddressAbsoluteXPath.

    Here is the schema:

    <element name=" person ">
      <complexType><sequence>
        <element name="surname"/>
        <element name="address">
          <complexType><sequence>
            <element name="street"/>
            <element name="town"/>
          </sequence></complexType>
        </element>
    </sequence></complexType>
    </element>
    

    Here is an excerpt from the XML document:

    <person>
      <surname>Smith</surname>
      <address>
        <street>Rue de la Paix</street>
        <town>Paris</town>
      </address>
    </person>
    

    And here are the rules:

    rule displayAddressRelativeXPath
    {
      when {
         p: Person (  );
      }
      then {
         // display : "address"
         out.println ( p.address.getRelativeXPath ( true ) );
      }
    }
    
    
    rule displayAddressAbsoluteXPath
    {
      when {
         p: Person (  );
      }
      then {
         // display : "/person/address"
         out.println ( p.address.getAbsoluteXPath ( true ) );
      }
    }