title: Iterate Mediator sends duplicate payload almost always
body: <p>We are trying out a scenario where we want to iterate over a list of nodes and make a POST call to some service with each individual request payload. We are seeing the iterate mediator actually sends two elements in that call and
  that causes issues on the API end.</p>
  
  <p>I have a mock service deployed locally that returns response say :</p>
  
  <pre><code>&lt;result&gt;
     &lt;row&gt;
        &lt;product_id&gt;8351&lt;/product_id&gt;
        &lt;event_key&gt;17708&lt;/event_key&gt;
        &lt;event_code&gt;AEONBM&lt;/event_code&gt;
        &lt;show_title&gt;Some Show&lt;/show_title&gt;
        &lt;venue_name&gt;Eugene ONeill Theatre&lt;/venue_name&gt;
        &lt;area&gt;ORCHC&lt;/area&gt;
        &lt;row&gt;C&lt;/row&gt;
        &lt;seat_num&gt;103&lt;/seat_num&gt;
        &lt;seat_increment&gt;1&lt;/seat_increment&gt;
        &lt;marketing_code&gt;PREMIUM&lt;/marketing_code&gt;
        &lt;Cost&gt;352.0000&lt;/Cost&gt;
     &lt;/row&gt;
     &lt;row&gt;
        &lt;product_id&gt;8351&lt;/product_id&gt;
        &lt;event_key&gt;17708&lt;/event_key&gt;
        &lt;event_code&gt;AEONBM&lt;/event_code&gt;
        &lt;show_title&gt;Some Show&lt;/show_title&gt;
        &lt;venue_name&gt;Eugene ONeill Theatre&lt;/venue_name&gt;
        &lt;area&gt;ORCHC&lt;/area&gt;
        &lt;row&gt;C&lt;/row&gt;
        &lt;seat_num&gt;104&lt;/seat_num&gt;
        &lt;seat_increment&gt;1&lt;/seat_increment&gt;
        &lt;marketing_code&gt;PREMIUM&lt;/marketing_code&gt;
        &lt;Cost&gt;352.0000&lt;/Cost&gt;
     &lt;/row
  &lt;/result&gt;
  </code></pre>
  
  <p>Here is how my proxy service looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;proxy xmlns="http://ws.apache.org/ns/synapse"
         name="CreateListingFromGetLocation"
         transports="https,http"
         statistics="disable"
         trace="disable"
         startOnLoad="true"&gt;
     &lt;target&gt;
        &lt;inSequence&gt;
           &lt;log level="full"&gt;
              &lt;property name="text" value="Triggering getLocation API call.."/&gt;
           &lt;/log&gt;
           &lt;send receive="createListingsFromGetLocationResponseSequence"&gt;
              &lt;endpoint&gt;
                 &lt;http method="get" uri-template="http://localhost:8989/GetLocation/"/&gt;
              &lt;/endpoint&gt;
           &lt;/send&gt;
           &lt;property name="OUT_ONLY" value="true"/&gt;
        &lt;/inSequence&gt;
        &lt;outSequence&gt;
          &lt;send/&gt;
        &lt;/outSequence&gt;
        &lt;faultSequence/&gt;
     &lt;/target&gt;
     &lt;description/&gt;
  &lt;/proxy&gt;
  </code></pre>
  
  <p>Here is my receiving sequence that is using the iterate mediator:</p>
  
  <pre><code>&lt;sequence xmlns="http://ws.apache.org/ns/synapse" name="createListingsFromGetLocationResponseSequence"&gt;
     &lt;iterate xmlns:tem="http://tempuri.org" xmlns:ns="http://org.apache.synapse/xsd" expression="//result/row"&gt;
        &lt;target&gt;
           &lt;sequence&gt;
              &lt;log level="full"&gt;
                 &lt;property name="LocationRow" value="Row element from GetLocationResponse"&gt;&lt;/property&gt;
              &lt;/log&gt;
              &lt;payloadFactory media-type="xml"&gt;
                 &lt;format&gt;
                    &lt;listing xmlns=""&gt;
                       &lt;eventId&gt;$1&lt;/eventId&gt;
                       &lt;eventDescription&gt;$2&lt;/eventDescription&gt;
                       &lt;pricePerTicket&gt;
                          &lt;amount&gt;$3&lt;/amount&gt;
                          &lt;currency&gt;USD&lt;/currency&gt;
                       &lt;/pricePerTicket&gt;
                       &lt;quantity&gt;$4&lt;/quantity&gt;
                       &lt;section&gt;$5&lt;/section&gt;
                       &lt;rows&gt;$6&lt;/rows&gt;
                       &lt;seats&gt;$7&lt;/seats&gt;
                       &lt;splitOption&gt;NONE&lt;/splitOption&gt;
                    &lt;/listing&gt;
                 &lt;/format&gt;
                 &lt;args&gt;
                    &lt;arg expression="//event_key" evaluator="xml"&gt;&lt;/arg&gt;
                    &lt;arg expression="//show_title" evaluator="xml"&gt;&lt;/arg&gt;
                    &lt;arg expression="//Cost" evaluator="xml"&gt;&lt;/arg&gt;
                    &lt;arg expression="//seat_increment" evaluator="xml"&gt;&lt;/arg&gt;
                    &lt;arg expression="//area" evaluator="xml"&gt;&lt;/arg&gt;
                    &lt;arg expression="//row/row" evaluator="xml"&gt;&lt;/arg&gt;
                    &lt;arg expression="//seat_num" evaluator="xml"&gt;&lt;/arg&gt;
                 &lt;/args&gt;
              &lt;/payloadFactory&gt;
              &lt;log level="full"&gt;
                 &lt;property name="ListingRequest" value="Listing request xml"&gt;&lt;/property&gt;
              &lt;/log&gt;
              &lt;property name="Content-Type" value="application/xml" scope="transport" type="STRING"&gt;&lt;/property&gt;
              &lt;property name="messageType" value="application/xml" scope="transport" type="STRING"&gt;&lt;/property&gt;
              &lt;property name="TARGET_HOST" value="srwd30" scope="transport" type="STRING"&gt;&lt;/property&gt;
              &lt;property name="HTTP_METHOD" value="POST" scope="transport" type="STRING"&gt;&lt;/property&gt;
              &lt;send&gt;
                 &lt;endpoint&gt;
                    &lt;http format="pox" method="post" uri-template="http://www.srwd30.com/listings/v1/"&gt;&lt;/http&gt;
                 &lt;/endpoint&gt;
              &lt;/send&gt;
           &lt;/sequence&gt;
        &lt;/target&gt;
     &lt;/iterate&gt;
  &lt;/sequence&gt;
  </code></pre>
  
  <p>Firstly, I see that it is iterating over each xml node properly, here are some logs indicating that:</p>
  
  <pre><code>[2014-04-22 13:29:42,020]  INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:3bc14a33-3a7a-478e-bdbf-720f1ec855a5, Direction: response, LocationRow = Row element from GetLocationResponse, Envelope: &lt;?xml version="1.0" encoding="utf-8
  "?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body&gt;&lt;row&gt;
        &lt;product_id&gt;8351&lt;/product_id&gt;
        &lt;event_key&gt;17708&lt;/event_key&gt;
        &lt;event_code&gt;AEONBM&lt;/event_code&gt;
        &lt;show_title&gt;Some Show&lt;/show_title&gt;
        &lt;venue_name&gt;Eugene ONeill Theatre&lt;/venue_name&gt;
        &lt;area&gt;ORCHC&lt;/area&gt;
        &lt;row&gt;D&lt;/row&gt;
        &lt;seat_num&gt;103&lt;/seat_num&gt;
        &lt;seat_increment&gt;1&lt;/seat_increment&gt;
        &lt;marketing_code&gt;PREMIUM&lt;/marketing_code&gt;
        &lt;Cost&gt;352.0000&lt;/Cost&gt;
     &lt;/row&gt;&lt;/soapenv:Body&gt;&lt;/soapenv:Envelope&gt;
  [2014-04-22 13:29:42,021]  INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:0e04ac15-a0bf-41a3-a7d7-80a1401d3efc, Direction: response, ListingRequest = Listing request xml, Envelope: &lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;soapenv:En
  velope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;soapenv:Body&gt;&lt;listing&gt;&lt;eventId&gt;17708&lt;/eventId&gt;&lt;eventDescription&gt;Some Show&lt;/eventDescription&gt;&lt;pricePerTicket&gt;&lt;amount&gt;352.0000&lt;/amount&gt;&lt;currency&gt;USD&lt;/currency&gt;&lt;/pricePerTicket&gt;&lt;quantity&gt;1&lt;/quantity&gt;&lt;section&gt;ORCHC&lt;/section&gt;&lt;rows
  &gt;C&lt;/rows&gt;&lt;seats&gt;103&lt;/seats&gt;&lt;splitOption&gt;NONE&lt;/splitOption&gt;&lt;/listing&gt;&lt;/soapenv:Body&gt;&lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>But on my API I see that we are receiving duplicate or two root nodes:</p>
  
  <pre><code>Content-Type: application/xml
  Headers: {cache-control=[no-cache], connection=[Keep-Alive],
  content-type=[application/xml], host=[www.srwd30.com], http_method=[POST], messagetype=[application/xml], target_host=[srwd30], transfer-encoding=
  [chunked], user-agent=[Synapse-PT-HttpComponents-NIO]}
  Payload: &lt;listing&gt;&lt;eventId&gt;17708&lt;/eventId&gt;&lt;eventDescription&gt;Some Show&lt;/eventDescription&gt;&lt;pricePerTicket&gt;&lt;amount&gt;352.0000&lt;/amount&gt;&lt;currency&gt;USD&lt;/currenc
  y&gt;&lt;/pricePerTicket&gt;&lt;quantity&gt;1&lt;/quantity&gt;&lt;section&gt;ORCHC&lt;/section&gt;&lt;rows&gt;C&lt;/rows&gt;&lt;seats&gt;103&lt;/seats&gt;&lt;splitOption&gt;NONE&lt;/splitOption&gt;&lt;/listing&gt;&lt;listing&gt;&lt;eventId&gt;1770
  8&lt;/eventId&gt;&lt;eventDescription&gt;Some Show&lt;/eventDescription&gt;&lt;pricePerTicket&gt;&lt;amount&gt;352.0000&lt;/amount&gt;&lt;currency&gt;USD&lt;/currency&gt;&lt;/pricePerTicket&gt;&lt;quantity&gt;1&lt;
  /quantity&gt;&lt;section&gt;ORCHC&lt;/section&gt;&lt;rows&gt;D&lt;/rows&gt;&lt;seats&gt;104&lt;/seats&gt;&lt;splitOption&gt;NONE&lt;/splitOption&gt;&lt;/listing&gt;
  --------------------------------------
  2014-04-22 19:01:51,468 [e14f#fbf/http://www.srwd30.com/listings/v1/] priority=WARN  app_name=shared-stubhubjobs thread=http-0.0.0.0-
  8080-8 location=AbstractJAXBProvider line=112 javax.xml.bind.UnmarshalException
   - with linked exception:
  [com.ctc.wstx.exc.WstxParsingException: Illegal to have multiple roots (start tag in epilog?).
   at [row,col {unknown-source}]: [1,291]]
  </code></pre>
  
  <p>From the looks of it, iterator is sending two elements when I make that send call. Am i missing something or doing something wrong? How can make each call independent from other?</p>