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 com.oracle.bpel.xml.xpath.IXPathFunction
- 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,
PathDefs.XPATH_FUNCTION_VARIABLE_DATA);
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">
<value>http://schemas.oracle.com/xpath/extension</value>
<comment>Namespace URI for this function</comment>
</property>
<property id="namespace-prefix">
<value>ora</value>
<comment>Namespace prefix for this function</comment>
</property>
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.
<copy>
<from expression="ora:deleteEmptyElements(
bpws:getVariableData
('outputVariable',
'payload',
'/client:UsingChoicesProcessResponse/
client:choiceTest'))"
/>
<to variable="outputVariable" part="payload"
query="/client:UsingChoicesProcessResponse/
client:choiceTest"
/>
</copy>
still have questions? send your feedback on writing a custom xpath function here
1 Comments:
Hi,
what about using a xslt transformation, which runs through the xml file and removes all empty tags.
Thanks
Satish
2:06 PM
Post a Comment
<< Home