Sunday, June 26, 2011

How to Enrich Existing XML Using XSL in SOA 11g

Many times you have a requirement to enrich existing xml objects in a BPEL. I have seen developers using assign activity for that. Assign activity is appropriate if you are adding few mappings. It will be little bit difficult to use assign activity if
  • You need to map optional elements. Assign will fail at runtime if the element is not present in the payload. To prevent selection failure you need to add a Switch activity to check for element existence.
  • You need to add/append/update a collection of xml elements. It requires a While activity to iterate the collection.

  • You need to add mappings for multiple elements.
If you try to do it using Assign your BPEL will be flooded with Switch, Assign and While activities. Your BPEL process become unmanageable its will increase the development time as well. Generally people think the following about XSL transformations:
  • XSL transformations are very complex to write.

  • They may loose some elements if they use XSL.
So they go with Assign activity and bear all the pains I have mentioned earlier. You can pass multiple source XML objects to an XSL map in SOA 11g. See this post to learn how to pass multiple XML objects to the XSL map. Enriching existing XML using XSL maps is easy in SOA 11g, You just need to follow the steps given below:

1. Add a Transform activity and select the same xml variable as source and target.

2. Add additional variables as source. These variables will be passed as parameters to XSL transformation. See the image given below:

3. Click on Apply and it will open the XSLT map.

4. Map at least one element from each of the additional variables to the target variable. When you add mappings for additional variables, XSL designer defined parameters for additional variables in the XSL. If you want to define parameters manually you can ignore this step.

5. Go to XSL source view and remove the default template and mappings added by the map designer. You can keep XPath expressions in a separate text editor window.

6.Copy the following template in the XSL file:

<xsl:template match="@*|node()">
<xsl:apply-templates select="@*|node()"/>

This map copies all the elements from source to the target XML. As you are enriching the existing XML, this template will make sure that you don’t loose any xml element.

7. Now it’s the time for customization. You need to add one template per XML element you want to customize. See the following sample template:

sample:Line xmlns:sample="">
select="@* | *"/>
<!-- ADD you custom mappings here. You can also add new elements here-->

Please note that if you add a template for an XML element it will be applied to that element and its children. You can add new elements, change element values in the XSL templates. Make changes to the above XSL template as per you requirement. Add proper namespace prefixes and declarations.

8. Once done with the XSL, test it in JDeveloper to make sure that it is working fine.

9. Deploy your composite on SOA 11g server and test it.