Search code examples
xpathwso2bpel

How to extract an atomic value from a message based variable and assign to an output variable using XPATH


Just to rephrase the question I am using the WSO2 BPS server to run a business process to invoke a RestProxy service on the WSO2 ESB. I get a response back through an output variable called LVCounterOut. The LVCounterOut is a message type based variable and the response is in XML format as shown below:

<message>
 <payload>
  <Response>
     <Terminal>
       <Name>RValue</Name>
       <Value>0.000000</Value>
     </Terminal>
     <Terminal>
       <Name>SValue</Name>
       <Value>**1.000000**</Value>
     </Terminal>
  </Response>
 </payload>
</message>

Ho can I assign an atomic value in the above variable to the output variable of the business process. For example in the above response I would like to assign the numeric part of the "Value" node that is "1.0000000" which would be in string format to an output variable of the process. The next question would be is how to convert this string value into an integer so that i can use it in an If control in my Business process. for example "If Value > 10" invoke ServiceA else invoke ServiceB".

Here is what i have already tried to do to assign the output from the LVCounterOut variable to the output variable "ProcessOutput".

<bpel:assign validate="no" name="AssignProxyOut">
        <bpel:copy>
            <bpel:from>
                <bpel:literal>
                    <tns:LVCounterProcessResponse xmlns:tns="http://wso2.org/bps/sample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><tns:return>tns:return</tns:return>

                    </tns:LVCounterProcessResponse>
                </bpel:literal>
            </bpel:from>
            <bpel:to variable="ProcessOutput" part="payload"></bpel:to>
        </bpel:copy>
        <bpel:copy>
            <bpel:from>
              <![CDATA[$LVCounterOut.payload/ns:Response/ns:Terminal[2]/ns:Value/text()]]>
            </bpel:from>
            <bpel:to part="payload" variable="ProcessOutput">
                <bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:return]]></bpel:query>
            </bpel:to>
        </bpel:copy>
    </bpel:assign>

When i run the business process on the BPS server using tryit I get the following error

[2012-11-20 16:41:59,882] ERROR - BpelEngineImpl - Scheduled job failed; jobDetail=JobDetails( instanceId: null mexId: hqejbhcnphr7rsjnbxjhrs processId:
 {http://wso2.org/bps/sample}LVSelectProcess-127 type: INVOKE_INTERNAL channel: null correlatorId: null correlationKeySet: null retryCount: null inMem:
false detailsExt: {enqueue=false}) java.lang.ClassCastException: java.lang.String cannot be cast to org.w3c.dom.Node at
org.jaxen.dom.DocumentNavigator.getChildAxisIterator(DocumentNavigator.java:152)
at org.jaxen.util.DescendantAxisIterator.next(DescendantAxisIterator.java:129)

I assume that the use of XPATH here is wrong or am i going about totally the wrong way? I do apologies but XML and XPATH are not my strongest point and i am still trying to get to grips with it.


Solution

  • For anyone who does come across a similar problem, i have solved this query as follows:

    To extract atomic values from the output message variable of an invoked service you can use

    bpel:getVaribleData(varName, partName, xpathStr) function.

    This function is not defined in WS-BPEL-2.0 specification. But this works in WSO2-BPS and Apache-ODE. It can be used to extract a set of elements from a variable, using a XPath expression.

    eg -

    <bpel:copy>
    <bpel:from>
        <![CDATA[count(bpel:getVariableData(‘$Variable','$partName')/ns:return)]]>
    </bpel:from>
    <bpel:to variable="itemCount"></bpel:to>
    </bpel:copy>
    

    Remember to use ‘’ when passing varName and partName. And here “/ns:return” is the xpath expression.