Powered By Blogger

Tuesday, August 24, 2010

Passing BPEL Variable contents into XSLT as Parameters

XSLT is executed using the XPath Extension Function ora:processXSLT. The well known two arguments for this extension function are as follows.

1) The XSL File Name
2) The source variable to be transformed [bpws:getVariableData(...)]

But there is one more argument that this XPath function can accept - 'properties'.
Note the signature of this function specified in xpath-functions.xml
Signature: ora:processXSLT('xsl template','input variable','properties'?).
These properties translate to XSL Parameters that can be accessed within the XSL map using the construct

<xsl:param name="paramName">

You can retrieve the value of this parameter within your XSLT in a way similar to the way used to extract data from XSL variables.

For e.g. <xsl:value-of select="$paramName"/>

The "properties" argument of the XPath function is expected to be an XML Element that has the following structure.
Illustrated below is an example of such a properties XML.

<parameters xmlns:ns2="http://schemas.oracle.com/service/bpel/common" xmlns="http://schemas.oracle.com/service/bpel/common">
<ns2:item>
<ns2:name>userName</ns2:name>
<ns2:value>ramkmeno</ns2:value>
</ns2:item>
<ns2:item>
<ns2:name>location</ns2:name>
<ns2:value>CA</ns2:value>
</ns2:item>
</parameters>

XSLTParameters.xsd

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.oracle.com/service/bpel/common" targetNamespace="http://schemas.oracle.com/service/bpel/common" elementFormDefault="qualified">
<xsd:element name="parameters">
<xsd:annotation>
<xsd:documentation> A sample element </xsd:documentation> </xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="item" maxOccurs="unbounded"> <xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="value" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Within the XSLT, the parameters are accessible through their names. [in this case, the parameter names are "userName" and "location", and their values are "ramkmeno" and "CA" respectively.

Approach in nutshell

1) Add XSLTParameters.xsd to BPEL project
2) Declare a variable (xslt_Variable) is of the abovementioned data type- parameters [see XML above.]
3) Populate the variable with the contents of the BPEL variable you wish to pass into the XSLT
4) Invoke processXSLT() with the XSL File, source variable, and the parameters variable.
5) Access the parameter contents within the XSLT

Example BPEL Snippet

<!--Step 1: initialize the parameters variable from whatever BPEL variable whose information you need to access from within XSLT -->
<assign name="Assign_PassProcessIterator">
<copy>
<from expression='"userName"'/> <!-- Name of xslt parameter-->
<to variable="xslt_Variable"
query="/ns7:parameters/ns7:item/ns7:name"/>
</copy>
<copy>
<from variable="Praveen"/> <! Value of xslt parameter-->
<to variable="xslt_Variable"
query="/ns7:parameters/ns7:item/ns7:value"/>
</copy> </assign>
<!--Step 2: Invoke the XSLT with the parameters as the third argument -->
<assign name="executeXSLT">
<bpelx:annotation>
<bpelx:pattern>transformation</bpelx:pattern>
</bpelx:annotation>
<copy>
<from expression="ora:processXSLT('TestXSLParams.xsl',
bpws:getVariableData('inputVariable','payload'),
bpws:getVariableData('xslt_Variable'))"/>
<to variable="outputVariable" part="payload"/>
</copy>
</assign>

XSLT Snippet

<xsl:stylesheet version="1.0" ....>
<xsl:param name="userName"/>
<xsl:template match="/">
<ns1:TestXSLParamsProcessResponse>
<ns1:result>
<xsl:value-of select="concat('User : ', $userName, ' Location : ',$location)"/>
</ns1:result>
</ns1:TestXSLParamsProcessResponse>
</xsl:template>
</xsl:stylesheet>

No comments:

Post a Comment