Thursday, December 2, 2010

Some XPath functions useful in BPEL


In BPEL programming, XPath is heavily used to manipulate XMLs. Here are some WS-BPEL specific XPath functions and common xpath functions that’ll be helpful in writing BPELs . Remember based on the BPEL run-time engine; functionalities of these functions might be changed.

There are some valuable BPEL run time engine related XPath functions, you need to know in XML manipulations.


bpel:getVaribleData(varName, partName, xpathStr) - 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.


bpel:getLinkStatus() - This is also not defined in WS-BPEL-2.0 specification. It evaluates and return a boolean whether a particular link is active/inactive.


bpel:getVariableProperty(string, string) - This is helpful in extracting properties in Variables.

eg - (From http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.html)
<condition>
    bpel:getVariableProperty('stockResult','inventory:level') > 100
</condition>

Remember to use ‘’ when passing parameters.


bpel:doXSLTTransform() - perform XSLT transformations.

eg - (From http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.html)
<copy>
    <from>
        bpel:doXslTransform("urn:stylesheets:A2B.xsl", $A)
    </from>
    <to variable="B"/>
</copy>


round(input) - used to get an integer closest to the $input. This is become important in converting numbers into integers.

eg - In arrays mostly need to use this operation to traverse.
<bpel:copy>
    <bpel:from>
        <![CDATA[$profileProcessorInput.payload/tns:profiles/tns:profile[round($itemIterator)]/]]>
    </bpel:from>
    <bpel:to variable="itemLine">
        <bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0">
            <![CDATA[@name]]></bpel:query>
    </bpel:to>
</bpel:copy>


string() - In WSO2 BPS and Apache ODE this method can be used to extract text content out-of elements rather using /text() .



string-length() - Name suggests what it does. But != operator seems not to work with the output from this function. So you can use > or < rather using != .


Avoid name-spaces in xpath manipulation - It becomes cumbersome sometime to concern on name-spaces when it’s not that much essential. We can use Regular expressions to bypass the name-space verification on XPath. eg -
<bpel:copy>
<bpel:from part="payload" variable="profileContext">
<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0">
<![CDATA[tns:profiles/*[local-name() = 'Profile'][round($itemIterator)]/*[local-name() = 'club_code']]]>
</bpel:query>

</bpel:from>
<bpel:to part="parameters" variable="Request">
<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0">
<![CDATA[ns2:BuildingNo]]>

</bpel:query>
</bpel:to>
</bpel:copy>


contains($regEx, $string) - use to match a $string with a particular $substring. Here you can use Reg-Ex as well.


count() - Returns the count of nodes. Here’s an example of it.

eg -
<bpel:copy>
    <bpel:from>
        <![CDATA[count(bpel:getVariableData('$VariableName','$partName')/ns:SubChildElementName)]]>
    </bpel:from>
    <bpel:to variable="itemCount"></bpel:to>
</bpel:copy>