Bookmark and Share Share

Defining Custom XPath Functions

You can create custom XPath functions to perform calculations and use these functions in XPath expressions anywhere in mashups.

To define custom functions:

  1. Write a Custom XPath Function Class

  2. Add the Function to the Classpath for the EMML Reference Runtime Engine

  3. Add the Function to a Mashup Script

Write a Custom XPath Function Class

You create the function as a Java class that extends org.oma.emml.client.EMMLUserFunction. See org.oma.emml.client.EMMLUserFunction for reference information on this class.

Any method in your custom class can be used as an XPath function with these limitations:

  • The method must be a static class method.

  • All parameters for the method must be simple types.

  • The return type can be void or any simple type. It cannot return a node or node set.

For example:

You must add web-apps-home/emml/WEB-INF/lib/emml.jar to the classpath to compile your custom XPath Function class.

Add the Function to the Classpath for the EMML Reference Runtime Engine

You must add custom XPath function classes to the EMML Engine for any mashup scripts that uses this function.

To add the function to the classpath for the EMML Engine, you copy either the compiled class file or a JAR containing the compiled class file for the function to one of these folders, respectively:

  • web-apps-home/emml/WEB-INF/classes

  • web-apps-home/emml/WEB-INF/lib

Add the Function to a Mashup Script

Once you have added the function to the classpath, you can use it in mashup scripts. First, you must declare a namespace for the custom function class using the java: protocol and the fully qualified Java class name. For example:

<?xml version="1.0"?> 
<mashup name="SoundexSearch" 
  xmlns="http://www.openemml.org/2009-04-15/EMMLSchema" 
  xsi:schemaLocation="http://www.openemml.org/2009-04-15/EMMLSchema 
     ../schema/EMMLSpec.xsd" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:myFunc="java:com.mycompany.mashups.CustomFunctions" > 
...

Then use the function, with its namespace, in any XPath expression in the mashup script. This example uses the function in a filter expression:

... 
  <input name="param" type="document"> 
    <data> 
      <item><name>test1</name></item> 
      <item><name>data2</name></item> 
      <item><name>test3</name></item> 
      <item><name>data4</name></item> 
      <item><name>data-5</name></item> 
      <item><name>data.6</name></item> 
      <item><name>data-6-6</name></item> 
      <item><name>5-6-data-6-6</name></item> 
    </data> 
  </input> 
  <output name="result" type="document"/> 
  <filter inputvariable="param" 
      filterexpr = "/data/item[myfunc:soundex(name, 'data')]" 
      outputvariable="result"/>

Notice that the first parameter for the function is passed with a relative XPath expression to the name node. You can use XPath expressions as function parameters as long as the expressions resolve to a single node with simple content.