Search code examples
wso2esbwso2-esb

Use of Iterate Mediator in wso2 ESB


I have my input request as:

<body>
 <p:UpdateID xmlns:p="http://tempuri.org">
  <!--Exactly 1 occurrence-->
  <xs:newid xmlns:xs="http://tempuri.org">NewID</xs:newid>
  <!--1 or more occurrences-->
  <xs:oldid xmlns:xs="http://tempuri.org">OldID_001</xs:oldid>
  <xs:oldid xmlns:xs="http://tempuri.org">OldID_002</xs:oldid>
 </p:UpdateID>
</body>

I have written a proxy service which i not working as:

<proxy xmlns="http://ws.apache.org/ns/synapse" name="UpdateID" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <iterate xmlns:xs="http://tempuri.org" id="Iterate1" expression="count(//xs:oldid)">
            <target>
               <sequence>
                  <property name="newid" expression="//xs:newid" scope="default" type="STRING"/>
                  <property name="oldid" expression="//xs:oldid" scope="default" type="STRING"/>
                  <payloadFactory>
                     <format>
                        <p:UpdateID xmlns:p="http://tempuri.org">
                           <xs:newid>$1</xs:newid>
                           <xs:oldid>$2</xs:oldid>
                        </p:UpdateID>
                     </format>
                     <args>
                        <arg expression="get-property('newid')"/>
                        <arg expression="get-property('oldid')"/>
                     </args>
                  </payloadFactory>
                  <send>
                     <endpoint key="UpdateEP"/>
                  </send>
               </sequence>
            </target>
         </iterate>
      </inSequence>
   </target>
   <publishWSDL uri="http://xyz:9764/services/Update_DataService?wsdl"/>
   <description></description>
</proxy>

Now my question is how can i get the count of oldid so that i can iterate from 0 to count of oldid,Something like for loop. And second how can i assign oldid value to the payload one by one . Please Help. Thanks in advance


Solution

  • You can change the iterate expression to expression="//xs:oldid" which will iterate to number of available oldid elements. Then you can take assignment of the newid value, out of the iterator, since there will be only one newid element. I have update your proxy configuration with those changes.

    <proxy xmlns="http://ws.apache.org/ns/synapse" name="UpdateID" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
       <target>
          <inSequence>
        <property name="newid" expression="//xs:newid" scope="default" type="STRING" xmlns:xs="http://tempuri.org"/>
             <iterate xmlns:xs="http://tempuri.org" id="Iterate1" expression="//xs:oldid">
                <target>
                   <sequence>
                      <property name="oldid" expression="//xs:oldid" scope="default" type="STRING"/>
                      <payloadFactory>
                         <format>
                            <p:UpdateID xmlns:p="http://tempuri.org">
                               <xs:newid>$1</xs:newid>
                               <xs:oldid>$2</xs:oldid>
                            </p:UpdateID>
                         </format>
                         <args>
                            <arg expression="get-property('newid')"/>
                            <arg expression="get-property('oldid')"/>
                         </args>
                      </payloadFactory>
                      <send>
                         <endpoint key="UpdateEP"/>
                      </send>
                   </sequence>
                </target>
             </iterate>
          </inSequence>
       </target>
       <publishWSDL uri="http://xyz:9764/services/Update_DataService?wsdl"/>
       <description></description>
    </proxy>