Cesar
Cesar

Reputation: 527

wso2 split a string and iterate values

What would be the best solution the split a string with ',' delimiter and iterate all values in wso2 ?

Can it be done in one time ? Or I have to store values in an array and then iterate ?

Sample : $ctx:keys = "key1,key2,key3,key4,key5"

Wanto to iterate simply those values and print

key1
key2
key3
key4
key5

I know it would be simple in any other language but in wso2 it seems to be a pain to do. Any simple way to do what I want ?

Upvotes: 0

Views: 1092

Answers (2)

Shanaka Sandanayaka
Shanaka Sandanayaka

Reputation: 21

This is possible with the script mediator, But WSO2 is encouraging more not to use script mediator most of the times. Therefore, with a class mediator is could be very easily achieve.

Please refer the WSO2 documentation.

Upvotes: 1

tmoasz
tmoasz

Reputation: 1331

There is no straight way to do this... I think the easiest way to do this, is use ScriptMediator, like this:

<script language="js">
     var sample = mc.getProperty('keys').split(',');
     for(var s in sample) {
         java.lang.System.out.println(sample[s]);  
         mc.getServiceLog().info('k: ' + sample[s]);
     }
</script>

If i remember in linux, the java.lang.System may not work, so you can use the Loging for just print. Or maybe you can use xslt mediator following this topic: split function in xslt 1.0

Upvotes: 0

Related Questions