title: How can I use a WSO2 ESB XSLT mediator to add an <?xml-multiple?> element into a specific place of a DSS output?
body: <p>I have the following problem with <strong>WSO2 ESB\DSS</strong>: the problem is well explained in this beautifoul post on YENLO: <a href="https://www.yenlo.com/blog/from-xml-to-json-how-to-handle-an-array-of-one" rel="nofollow noreferrer">https://www.yenlo.com/blog/from-xml-to-json-how-to-handle-an-array-of-one</a></p>
  
  <p>In my specific case it is related to the fact that when I perform a query using <strong>DSS</strong> inside an <strong>ESB</strong> flow, and then convert the obtained <strong>XML</strong> document into a <strong>JSON</strong> document this JSON will be formatted differently, depending on the number of entities returned. When multiple entries are returned, for example I perform a query via DSS and I obtain this result:</p>
  
  <pre><code>&lt;VaccinationDetails xmlns="http://ws.wso2.org/dataservice"&gt;
     &lt;VaccinationDetails&gt;
        &lt;vaccination_id&gt;1&lt;/vaccination_id&gt;
        &lt;vaccination_name_en&gt;Antrax&lt;/vaccination_name_en&gt;
        &lt;vaccination_name&gt;Antrax&lt;/vaccination_name&gt;
        &lt;description xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
        &lt;vaccination_goal xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
        &lt;vaccination_coverage xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
        &lt;image_link&gt;https://firebasestorage.googleapis.com/v0/b/myproject.appspot.com/o/img%2Ficons%2Flivestock%2Fvacccination.png?alt=media&amp;amp;token=19b15f9a-4706-4037-a928-c8151c823077&lt;/image_link&gt;
        &lt;DiseaseCoveredByAVaccinationInfo&gt;
           &lt;DiseaseCoveredByAVaccinationInfoList&gt;
              &lt;disease_id&gt;6&lt;/disease_id&gt;
              &lt;disease_name_en&gt;Antrax&lt;/disease_name_en&gt;
              &lt;disease_name&gt;Antrax&lt;/disease_name&gt;
              &lt;description&gt;Anthrax is an infection caused by the bacterium Bacillus anthracis. It can occur in four forms: skin, lungs, intestinal, and injection. Symptoms begin between one day and two months after the infection is contracted. The skin form presents with a small blister with surrounding swelling that often turns into a painless ulcer with a black center. The inhalation form presents with fever, chest pain, and shortness of breath. The intestinal form presents with diarrhea which may contain blood, abdominal pains, and nausea and vomiting. The injection form presents with fever and an abscess at the site of drug injection.&lt;/description&gt;
              &lt;image_link&gt;https://firebasestorage.googleapis.com/v0/b/myproject/o/assets%2FLivestock%2FDisease%2Fanthrax.png?alt=media&amp;amp;token=3a7245ca-f003-4823-b39f-cfaef539e98c&lt;/image_link&gt;
           &lt;/DiseaseCoveredByAVaccinationInfoList&gt;
        &lt;/DiseaseCoveredByAVaccinationInfo&gt;
        &lt;DiseaseCoveredByAVaccinationInfo&gt;
           &lt;DiseaseCoveredByAVaccinationInfoList&gt;
              &lt;livestock_species_id&gt;1&lt;/livestock_species_id&gt;
              &lt;parent_livestock_species_id xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
              &lt;livestock_species_name_en&gt;Cow&lt;/livestock_species_name_en&gt;
              &lt;livestock_species_name&gt;Cow&lt;/livestock_species_name&gt;
              &lt;description&gt;Cattle, cows are the most common type of large domesticated ungulates. They are a prominent modern member of the subfamily Bovinae, are the most widespread species of the genus Bos, and are most commonly classified collectively as Bos taurus. Cattle are commonly raised as livestock for meat (beef and veal), as dairy animals for milk and other dairy products, and as draft animals (oxen or bullocks that pull carts, plows and other implements). Other products include leather and dung for manure or fuel. In some regions, such as parts of India, cattle have significant religious meaning.&lt;/description&gt;
              &lt;image_link&gt;https://firebasestorage.googleapis.com/v0/b/myproject/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&amp;amp;token=c21866df-448a-4a72-9da2-d55d87f8b31c&lt;/image_link&gt;
              &lt;ls_vaccination_id&gt;1&lt;/ls_vaccination_id&gt;
           &lt;/DiseaseCoveredByAVaccinationInfoList&gt;
        &lt;/DiseaseCoveredByAVaccinationInfo&gt;
        &lt;DiseaseCoveredByAVaccinationInfo&gt;
           &lt;DiseaseCoveredByAVaccinationInfoList&gt;
              &lt;livestock_species_id&gt;1&lt;/livestock_species_id&gt;
              &lt;parent_livestock_species_id xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
              &lt;livestock_species_name_en&gt;Cow&lt;/livestock_species_name_en&gt;
              &lt;livestock_species_name&gt;Cow&lt;/livestock_species_name&gt;
              &lt;description&gt;Cattle, cows are the most common type of large domesticated ungulates. They are a prominent modern member of the subfamily Bovinae, are the most widespread species of the genus Bos, and are most commonly classified collectively as Bos taurus. Cattle are commonly raised as livestock for meat (beef and veal), as dairy animals for milk and other dairy products, and as draft animals (oxen or bullocks that pull carts, plows and other implements). Other products include leather and dung for manure or fuel. In some regions, such as parts of India, cattle have significant religious meaning.&lt;/description&gt;
              &lt;image_link&gt;https://firebasestorage.googleapis.com/v0/b/myproject/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&amp;amp;token=c21866df-448a-4a72-9da2-d55d87f8b31c&lt;/image_link&gt;
              &lt;ls_vaccination_id&gt;1&lt;/ls_vaccination_id&gt;
           &lt;/DiseaseCoveredByAVaccinationInfoList&gt;
        &lt;/DiseaseCoveredByAVaccinationInfo&gt;
        &lt;LivestockVaccinationTimeFrameInfo/&gt;
     &lt;/VaccinationDetails&gt;
  &lt;/VaccinationDetails&gt;
  </code></pre>
  
  <p>Now the <strong>DiseaseCoveredByAVaccinationInfoList</strong> element in this case is a single element but it can be multiple (it depends by the query output).</p>
  
  <p>The problem is that when in my ESB flow I convert this XML document into a JSON document by:</p>
  
  <pre><code>&lt;property name="messageType" scope="axis2" type="STRING" value="application/json"/&gt;
  &lt;property expression="json-eval($.)" name="JSONPayload" scope="default" type="STRING"/&gt; 
  </code></pre>
  
  <p>the result will be different if in the XML document</p>
  
  <pre><code>&lt;DiseaseCoveredByAVaccinationInfoList&gt;...&lt;/DiseaseCoveredByAVaccinationInfoList&gt;
  </code></pre>
  
  <p>is a single or multiple element.</p>
  
  <p>If it is a single element I will obtain something like this:</p>
  
  <pre><code>"DiseaseCoveredByAVaccinationInfoList": {
      "disease_id": 6,
      "disease_name_en": "Antrax",
      "disease_name": "Antrax",
      "description": "Anthrax is an infection caused by the bacterium Bacillus anthracis. It can occur in four forms: skin, lungs, intestinal, and injection. Symptoms begin between one day and two months after the infection is contracted. The skin form presents with a small blister with surrounding swelling that often turns into a painless ulcer with a black center. The inhalation form presents with fever, chest pain, and shortness of breath. The intestinal form presents with diarrhea which may contain blood, abdominal pains, and nausea and vomiting. The injection form presents with fever and an abscess at the site of drug injection.",
      "image_link": "https://firebasestorage.googleapis.com/v0/b/my-project.appspot.com/o/assets%2FLivestock%2FDisease%2Fanthrax.png?alt=media&amp;token=3a7245ca-f003-4823-b39f-cfaef539e98c"
  }
  </code></pre>
  
  <p>If in the XML document I have multiple </p>
  
  <p><code>&lt;DiseaseCoveredByAVaccinationInfoList&gt;...&lt;/DiseaseCoveredByAVaccinationInfoList&gt;</code></p>
  
  <p>elements the JSON converstion will creatre a JSON array, something like:</p>
  
  <pre><code>"DiseaseCoveredByAVaccinationInfoList":  
  [
  
  {
      "disease_id": 6,
      "disease_name_en": "YYY",
      "disease_name": "YYY",
      "description": "DESCRIPTION",
      "image_link": "ULR"
  },
  {
      "disease_id": 7,
      "disease_name_en": "XXX",
      "disease_name": "XXX",
      "description": "DESCRIPTION",
      "image_link": "ULR"
  },
  ]
  </code></pre>
  
  <p>Reading the previous <strong>YENLO</strong> post it seems that I have to add this XML attribute:</p>
  
  <pre><code>&lt;?xml-multiple?&gt;
  </code></pre>
  
  <p>I think that I need to have something like this:</p>
  
  <pre><code>&lt;?xml-multiple?&gt;&lt;DiseaseCoveredByAVaccinationInfoList&gt;&lt;/DiseaseCoveredByAVaccinationInfoList&gt;
  </code></pre>
  
  <p>So when it is converted into JSON this element will be always put into a JSON array and not as a single element.</p>
  
  <p>I also found this other post: <a href="http://www.hkmconsultingllc.com/blog/xml/json-array-control-with-staxon/" rel="nofollow noreferrer">http://www.hkmconsultingllc.com/blog/xml/json-array-control-with-staxon/</a></p>
  
  <p>reffering to the previous YENLO one that says to use <strong>XSLT</strong> to take a SOAP response (the DSS output) and add the <strong>xml-multiple</strong> processing instructions. But I really don't know what exactly I have to do to implement this behavior and use <strong>XSLT</strong> into my <strong>ESB</strong> flow to add this attribute.</p>
  
  <p>In my ESB flow I have defined this API:</p>
  
  <pre><code>&lt;resource methods="GET" protocol="http" uri-template="/country/{country_id}/vaccination/{vaccination_id}?lang={lang_id}"&gt;
      &lt;inSequence&gt;
          &lt;log level="full"/&gt;
          &lt;property expression="get-property('uri.var.country_id')" name="country_id" scope="default" type="STRING"/&gt;
          &lt;property expression="get-property('uri.var.vaccination_id')" name="vaccination_id" scope="default" type="STRING"/&gt;
          &lt;property expression="get-property('uri.var.lang_id')" name="lang_id" scope="default" type="STRING"/&gt;
          &lt;log level="custom"&gt;
              &lt;property expression="$ctx:country_id" name="country_id"/&gt;
              &lt;property expression="$ctx:vaccination_id" name="vaccination_id"/&gt;
              &lt;property expression="$ctx:lang_id" name="lang_id"/&gt;
          &lt;/log&gt;
          &lt;property name="messageType" scope="axis2" type="STRING" value="application/xml"/&gt;
          &lt;payloadFactory media-type="xml"&gt;
              &lt;format&gt;
                  &lt;ds:GetVaccinationDetails xmlns:ds="http://ws.wso2.org/dataservice"&gt;
                      &lt;ds:vaccination_id&gt;$1&lt;/ds:vaccination_id&gt;
                      &lt;ds:language_id&gt;$2&lt;/ds:language_id&gt;
                      &lt;ds:country_id&gt;$3&lt;/ds:country_id&gt;
                  &lt;/ds:GetVaccinationDetails&gt;
              &lt;/format&gt;
              &lt;args&gt;
                  &lt;arg evaluator="xml" expression="$ctx:vaccination_id"/&gt;
                  &lt;arg evaluator="xml" expression="$ctx:country_id"/&gt;
                  &lt;arg evaluator="xml" expression="$ctx:lang_id"/&gt; 
              &lt;/args&gt;
          &lt;/payloadFactory&gt;
          &lt;header name="Action" scope="default" value="urn:GetVaccinationDetails"/&gt;
          &lt;call&gt;
              &lt;endpoint key="livestock_Endpoint"/&gt;
          &lt;/call&gt;
          &lt;log level="full"/&gt;
          &lt;property name="messageType" scope="axis2" type="STRING" value="application/json"/&gt;
          &lt;property expression="json-eval($.)" name="JSONPayload" scope="default" type="STRING"/&gt;
  
  
          &lt;property name="RESPONSE" scope="default" type="STRING" value="true"/&gt;
          &lt;header action="remove" name="To" scope="default"/&gt;
          &lt;send/&gt;
      &lt;/inSequence&gt;
      &lt;outSequence/&gt;
      &lt;faultSequence/&gt;
  &lt;/resource&gt;
  </code></pre>
  
  <p>I think that I have to add an <strong>XSLT mediator</strong> after the DSS call but reading the documentation and example I can't understand how exactly works and how to add this <strong>xml-multiple</strong> element to my original XML before transforming it into a JSON document.</p>
  
  <p>What am I missing? How can I implement this behavior?</p>