Extract numeric and non-numeric part from a string with WSO2

Viewed 100

Thanks before. i want need help. I have simple process flow with wso2. The plan was validate and print string for alphabet and numeric. I can print both of them. but i think the formula was so much affort with that. i want find the simple way. i done try with regular expression. but when i try with that. i always get error result

My code :

<?xml version="1.0" encoding="UTF-8"?>
<api context="/split1" name="SplitAlphaNumber" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <property expression="//OperationValueRegex/Value" name="Value" scope="default" type="STRING"/>
            <payloadFactory media-type="xml">
                <format>
                    <OperationValueRegex xmlns="">
                        <Result1>$1</Result1>
                        <Result2>$2</Result2>
                    </OperationValueRegex>
                </format>
                <args>
                    <arg evaluator="xml" expression="translate(., translate($ctx:Value,'0123456789',''), '')"/>
                    <arg evaluator="xml" expression="translate(., translate($ctx:Value,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',''), '')"/>
                </args>
            </payloadFactory>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Expected Result was like this

<OperationValueRegex>
    <Result1>1234</Result1>
    <Result2>Mario Naga</Result2>
</OperationValueRegex>

Sample Input from postman :

<OperationValueRegex>
        <Value>Mario Naga 1234</Value>
</OperationValueRegex>

and actual output :

<OperationValueRegex>
    <Result1>
        1234
</Result1>
    <Result2>
        MarioNaga
</Result2>
</OperationValueRegex>

Please need suggestion with this. thanks

2 Answers

Here is a simpler way to achieve what you need. Make sure Xpath 2.0 is enabled in WSO2 Server.

Use the following two XPath expressions.

fn:tokenize($ctx:Value, ' ')[matches(., '\d+')] // Tokenize the String with space and extract the part with numeric values.

fn:replace($ctx:Value, ' \d+', '') //Replace the numeric part from the string 

PLFactory Mediator

<payloadFactory media-type="xml">
    <format>
        <OperationValueRegex xmlns="">
            <Result1>$1</Result1>
            <Result2>$2</Result2>
        </OperationValueRegex>
    </format>
    <args>
        <arg evaluator="xml" expression="fn:tokenize($ctx:Value, ' ')[matches(., '\d+')]" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
        <arg evaluator="xml" expression="fn:replace($ctx:Value, ' \d+', '')" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
    </args>
</payloadFactory>

It seems you do a second translate to get rid of leading/trailing spaces. This messes up your values. Instead you can use normalize-space() function as follows, this gets you the desired output:

<api xmlns="http://ws.apache.org/ns/synapse" name="SplitAlphaNumber" context="/split1">
   <resource methods="POST">
      <inSequence>
         <property name="Value" expression="//OperationValueRegex/Value" scope="default" type="STRING"/>
         <payloadFactory media-type="xml">
            <format>
               <OperationValueRegex xmlns="">
                  <Result1>$1</Result1>
                  <Result2>$2</Result2>
               </OperationValueRegex>
            </format>
            <args>
               <arg evaluator="xml" expression="normalize-space(translate($ctx:Value,'0123456789',''))"/>
               <arg evaluator="xml" expression="normalize-space(translate($ctx:Value,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',''))"/>
            </args>
         </payloadFactory>
         <respond/>
      </inSequence>
      <outSequence/>
      <faultSequence/>
   </resource>
</api>
Related