Class XPathService

  • All Implemented Interfaces:
    AdaptrisComponent, ComponentLifecycle, ComponentLifecycleExtension, MessageEventGenerator, Service, StateManagedComponent

    @ComponentProfile(summary="Extract data via XPath and store it",
                      tag="service,xml")
    public class XPathService
    extends ServiceImp

    This service allows you to configure an xpath expression which will be executed on source xml, the result of which can be saved to multiple locations.

    To specify where the source xml, source xpath expression and the result of the xpath execution should be saved, you shoud use DataInputParameter or DataOutputParameter.
    For example you can specify the source xml can be found in the AdaptrisMessage payload, by using StringPayloadDataInputParameter like this :

     
     <xpath-service>
       <xml-source class="string-payload-data-input-parameter"/>
       ...
     
     
    And perhaps the source xpath expression will be configured directly in Interlok config, using ConstantDataInputParameter;
     
     <xpath-service>
       <xpath-execution>
         <source class="constant-data-input-parameter">
           <value>//my/xpath/expression</value>
         </source>
       ...
     
     
    And then maybe the result of the xpath execution is to be saved in AdaptrisMessage metadata, using MetadataDataOutputParameter;
     
     <xpath-service>
       <xpath-execution>
         <target class="metadata-data-output-parameter">
           <metadata-key>targetMetadataKey</metadata-key>
         </target>
       ...
     
     

    While you may only specify a single source xml destination, you may if you wish apply multiple XPath expressions, each of which saves the result to a different location. To do this, simply configure multiple executions. Take the following example, where we specify the payload containing the source xml and 3 XPath expressions will be executed each of which will store the result in 3 different metadata items;

     
     <xpath-service>
       <xml-source class="string-payload-data-input-parameter"/>
     
       <xpath-execution>
         <source class="constant-data-input-parameter">
           <value>//my/first/xpath/expression</value>
         </source>
         
         <target class="metadata-data-output-parameter">
           <metadata-key>targetMetadataKey1</metadata-key>
         </target>
       </xpath-execution>
       
       <xpath-execution>
         <source class="constant-data-input-parameter">
           <value>//my/second/xpath/expression</value>
         </source>
         
         <target class="metadata-data-output-parameter">
           <metadata-key>targetMetadataKey2</metadata-key>
         </target>
       </xpath-execution>
       
       <xpath-execution>
         <source class="constant-data-input-parameter">
           <value>//my/third/xpath/expression</value>
         </source>
         
         <target class="metadata-data-output-parameter">
           <metadata-key>targetMetadataKey3</metadata-key>
         </target>
       </xpath-execution>
     
     </xpath-service>
     
     

    Should your source xml contain namespaces, you will need to configure the mappings in this service like this;

     
     <xpath-service>
     ...
       <namespace-context>
         <key-value-pair>
           <key>n1</key>
           <value>http://adaptris.com/xml/namespace1</value>
         </key-value-pair>
         <key-value-pair>
           <key>n2</key>
           <value>http://adaptris.com/xml/namespace2</value>
         </key-value-pair>
         <key-value-pair>
           <key>n3</key>
           <value>http://adaptris.com/xml/namespace3</value>
         </key-value-pair>
         <key-value-pair>
           <key>n4</key>
           <value>http://adaptris.com/xml/namespace4</value>
         </key-value-pair>
        </namespace-context>
      ...
     </xpath-service>
     
     

    If the DocumentBuilderFactoryBuilder has been explicitly set to be not namespace aware and the document does in fact contain namespaces, then Saxon can cause merry havoc in the sense that //NonNamespaceXpath doesn't work if the document has namespaces in it. We have included a shim so that behaviour can be toggled based on what you have configured.

    Since:
    3.0.6
    See Also:
    XPath.newXPathInstance(DocumentBuilderFactoryBuilder, NamespaceContext)

    In the adapter configuration file this class is aliased as xpath-service which is the preferred alternative to the fully qualified classname when building your configuration.