how to modify xml soap response node value

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns1:recupererDetailDossierCreditResponse xmlns:ns1="http://ws.detaildossier.credit.traitement.com/">
            <return>
                <entete>
                    <duree>220</duree>
                </entete>
                
                <responseDetailDossierCredit>
                    <capitalRestantDu>1563.47</capitalRestantDu>
              </responseDetailDossierCredit>

            </return>
        </ns1:recupererDetailDossierCreditResponse>
    </soap:Body>
</soap:Envelope>
 
 
 


I'am trying to modify the value of capitalRestantDu with 200, how can i perform this, i tried with js policy (https://www.googlecloudcommunity.com/gc/Apigee/How-to-modify-XML-request-payload-using-Javascript/m-...)  but didn't work for me.

any ideas  with xslt or others ?
Solved Solved
0 2 609
1 ACCEPTED SOLUTION

@alaedine - This should do the trick

NOTE: In my sample, am just updating the value and setting that xml object as the response. 

var message = context.getVariable('request');
if (message && message.content) {
  var xml = new XML(message.content);
  var ns1 = new Namespace('ns1','http://ws.detaildossier.credit.traitement.com/');
  var soap = new Namespace('soap','http://schemas.xmlsoap.org/soap/envelope/');
  var body = xml.soap::Body;
  // modify the text value of a specific node
  var recupererDetailDossierCreditResponseObj = body.ns1::recupererDetailDossierCreditResponse;
  var capitalRestantDuTextNode = recupererDetailDossierCreditResponseObj.return.responseDetailDossierCredit.capitalRestantDu.text();
  //print("capitalRestantDuTextNode: "+capitalRestantDuTextNode);
  capitalRestantDuTextNode.parent().setChildren('200');
  //print(xml.toXMLString());
  response.content = xml.toXMLString();
}

View solution in original post

2 REPLIES 2

@alaedine - This should do the trick

NOTE: In my sample, am just updating the value and setting that xml object as the response. 

var message = context.getVariable('request');
if (message && message.content) {
  var xml = new XML(message.content);
  var ns1 = new Namespace('ns1','http://ws.detaildossier.credit.traitement.com/');
  var soap = new Namespace('soap','http://schemas.xmlsoap.org/soap/envelope/');
  var body = xml.soap::Body;
  // modify the text value of a specific node
  var recupererDetailDossierCreditResponseObj = body.ns1::recupererDetailDossierCreditResponse;
  var capitalRestantDuTextNode = recupererDetailDossierCreditResponseObj.return.responseDetailDossierCredit.capitalRestantDu.text();
  //print("capitalRestantDuTextNode: "+capitalRestantDuTextNode);
  capitalRestantDuTextNode.parent().setChildren('200');
  //print(xml.toXMLString());
  response.content = xml.toXMLString();
}

Whoo-hoo, love it! E4X solves it.

For other readers, you could ALSO solve this with an XSL, or ... I guess with a Java callout, like this one, which allows you to modify a single node in an XML document.

Which approach you choose depends on your preferences, which one is easiest to maintain and understand, etc.