Java Callout in Mediator to Automatically Deal With Empty Elements
One of the most annoying things with XML Schema validation is that it will not accept empty elements of certain types, like xsd:date and xsd:double, even though the element is defined as optional (minOccurs=”0″). OK, I do understand that an empty element _does_ occur, and that this behaviour is therefore correct, but the implication is that developers will have to add <xsl:if> tags in their XSLT transformations around each and every element where this may cause a problem.
This article will show how using a small and generic Java class and the “Java Callout” feature of the 11g Mediator, we can automatically “fix” these empty elements so that they will not cause problems with schema validation any more.
First, a quick recap about what we’ld need to do to prevent this problem from occuring in the first place. There are basically two ways. The first is to add <xsl:if> statements around all these elements in all XSLT transformations:
This will prevent empty elements from occuring in the output message. The other option is to use the xsi:nil attribute from the Schema Instance namespace. For this to work, you’ld have to set the nillable=”true” attribute in the Schema definitions of these elements, and then you’ld have to map them like this to cover all possible cases:
Of course, the JDeveloper will generate this code for you in the graphical XLST Map Editor, when you use the following Automap setting:
But still, that’s a lot of mapping logic for a single element, and you can’t always use automap!
There’s a lot to be said for a mechanism that automatically deletes, or “xsi:nillifies”, all empty elements “on the way out”, without having to worry too much about them before that. In BPEL, I’ve often used a generic XSLT transformation at the end of the flow to do just that. But with the Mediator, that is not so easy to do, since you only have one transformation (in each direction) available to you.
However, Mediator 11g has a “Java Callout” property that you can define against each operation, which will allow you to program a Java class that gets called, among others, after all the transformations are done. An excellent discussion of this feature can be found in this blog entry by Lucas Jellema.
For this purpose, I have written a simple Java Callout class, that adds the xsi:nil=”true” attribute automatically on ALL empty elements in a document. It is invoked at the very end of the Routing Rule, just before the response is sent back to the client. The code is below:
All you need to do to invoke this logic, is to add this class to your SOA project (you can also put it in a jar and add it to the classpath of both JDeveloper and the WLS server), and add it to the “Java Callout” property of your Mediator components:
And that’s all there’s to it. There’s one thing to consider, though. You will NOT see the result of the Java Callout anywhere in the audit trail in the console! This is the screenshot of the instance details of a simple Mediator component I used for testing:
However, when you look at the XML that was returned for this test instance, you DO see the xsi:nil=”true” attributes:
The code above is easily modified to delete the empty elements instead of adding the xsi:nil attribute to them.
- SOA Suite 11g – introducing Mediator Java Callouts for debug, audit and …
- SOA Suite 11g (TP4) – Create Mediator based SCA Composite Application from XSD – write to output file using File Adapter
- Oracle 11g SOA Suite – Straightforward Mediator example (employees from file to database)
- SQL Script to empty out/cleanse a user schema
- Integration between Java and Oracle BPEL PM – What every Java developer should know about BPEL
This entry was posted by Peter Ebell on March 18, 2010 at 2:57 am, and is filed under General, J(2)EE/Java, Java, Java, JEE, OAS and WebLogic Server, Oracle, SOA & Oracle Fusion Middleware. Follow any responses to this post through RSS 2.0.You can skip to the end and leave a response. Pinging is currently not allowed.
- JDeveloper code templates
- Running Oracle Designer Generation from Ant and Hudson
- OTN Yathra 2013 – Spreading the story of Oracle across India – (Half time)
- Creating an hierarchical user structure in embedded LDAP of weblogic
- Dear Java/JEE developer – why should you care about ADF?
- Oracle SOA Suite 11g PS 5 introduces BPEL with conditional correlation for aggregation scenarios
- Configuration of BAM and BPM for process analytics
- OOW 2012: The yearly AMIS Review from Oracle Open World and JavaOne – slides available
- Kom kennismaken met AMIS en doe mee met uitdagende projecten
- JavaOne 2012: The Big Stories