Usage of split-to-elements Function
Objective
This sample explains the usage of split-to-elements() function.
Introducing split-to-elements() Function
This function is a XPath extension supported by the Apache ODE engine ( http://ode.apache.org/xpath-extensions.html) which can be used to generate arrays from a string. The method accepts four input parameters as follows.
split-to-elements(stringToSplit, separator, targetElement, targetNamespace)
For example,
split-to-elements($input,'123','chunk','http://ode/bpel/unit-test.wsdl')
if $input is as follows,
<unit:split> <TestPart>hello123bill123smith</TestPart> </unit:split>
the output will be as follows.
<TestPartxmlns:unit="http://ode/bpel/unit-test.wsdl"> <chunkxmlns="http://ode/bpel/unit-test.wsdl">hello</chunk> <chunkxmlns="http://ode/bpel/unit-test.wsdl">bill</chunk> <chunkxmlns="http://ode/bpel/unit-test.wsdl">smith</chunk> </TestPart>
Note
split-to-elements()
method should be used under the namespace of "http://www.apache.org/ode/type/extension"
Deploying the Sample
WSO2 BPS provides a sample, which can be deployed and executed to understand the function. Follow the instructions below to deploy and trigger the process.
1. Log in into BPS server management console and select "Processes -> Add" under the "Main" menu.
2. Upload the TestSplit.zip. (Samples are located at our sample repository).
3. In the "Deployed Processes" window, click the "Process ID" to access its "Process Information" window.
4. Under the "WSDL Details" widget, trigger the process using the "TryIt" link to create an instance of it.
Example Usage
Refer to TestSplit.zip and the example usage of the function as given below:Â
<copy> <from>ode:split-to-elements($tmpVar, ',', 'chunk')</from> <tovariable="myVar"part="TestPart"/> </copy>
This method accept three parameters as follows.
$tmpVar
- input source varaible','
- delimiter string'chunk'
- the target XML element
Suppose initial value of tmpVar
is
<unit:split> <TestPart>1,2,3,4</TestPart> </unit:split>
After executing split-to-elements()
the value of myVar
is
<TestPartxmlns:unit="http://ode/bpel/unit-test.wsdl"> <chunk>1</chunk> <chunk>2</chunk> <chunk>3</chunk> <chunk>4</chunk> </TestPart>