Some 'more, in-depth' information on Oracle BPEL PM, ESB and other SOA, day2day things

Tuesday, March 28, 2006

xsd:choice, pre-created elements, part 2 - writing a custom xpath function to remove empty elements

Thinking a little bit more on this issue, preferably on a sunday - and voila,
another solution came to my brain. Writing a custom XPATH function that removes
all the empty elements, from a given parent node.

Basically writing an BPEL XPATH function consists of 2 parts:

  • a class that implements

  • and is registered in xpath-functions.xml, located in $BPEL_HOME/domains/<domain name>/config

Ok, one step after the other

Step 1 - implementing the interface - the only method that is available and will be called is

public Object call(IXPathContext context,
List args)
throws XPathFunctionException;

Inside the class it's rather easy to retrieve parts, like the context, or any xpath queried elements as shown below

// retrieve the context ..
Map properties =
(Map) context.getVariableValue(null, null,

ICubeContext cubeContext =
(ICubeContext) properties.get(XPathDefs.CUBE_CONTEXT);

retrieving the xpath element from a query is not much deal too (in this case I expect one argument of type Element)

Element el = (CubeDOMElement) args.get(0)

So the only thing left is implementing an algorithm to search the element (el) for empty nodes .. and remove them ..

Step 2 - registering the function with the BPEL Server
Next, open the xpath-functions.xml, located in $BPEL_HOME/domains/<domainname>/config, and add your new function.
I just copied one of the entries, changed the value of <function> to deleteEmptyElements, the <classname> to point to my new class, and gave the whole thing the right pprefix and namespace (as shown)

<property id="namespace-uri">
<comment>Namespace URI for this function</comment>
<property id="namespace-prefix">
<comment>Namespace prefix for this function</comment>

Using your new function in a process:
As written earlier, one argument of type element is expected, which can be easiliy retrieved throug using bpws:getVariableData function.

<from expression="ora:deleteEmptyElements(
<to variable="outputVariable" part="payload"

still have questions? send your feedback on writing a custom xpath function here


Anonymous Satish Singaram said...


what about using a xslt transformation, which runs through the xml file and removes all empty tags.


2:06 PM


Post a Comment

<< Home