Friday, 3 May 2013

How to Enrich Existing XML Using XSL in SOA 11g

Most of the  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(lesser mapping). 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:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

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:

<xsl:template
match="sample:Line">
<
sample:Line xmlns:sample="http://www.oracle.com/Sample">
<xsl:apply-templates
select="@* | *"/>
<!-- ADD you custom mappings here. You can also add new elements here--> </
sample:Line> </xsl:template>

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.

3 comments:

  1. It was so nice article and useful to Information learners. That is a good tip especially to those new to the blog here. Brief but very accurate information…
    Oracle Fusion cloud HCM Technical Online Training

    ReplyDelete
  2. It is very good article.Thanks for sharing such a
    nice article.
    oracle fusion SCM online training

    ReplyDelete
  3. This post is so nice. we Provides you Free Online Payment Gateway Malaysia for your online payment.
    malaysian online payment gateway
    Payment
    Online Payment Service

    ReplyDelete

xslt padding with characters call template for left pad and right pad

  Could a call-template be written that took two parameters ?   a string, and a   number) return the string with empty spaces appended t...