Hi,
I want to replace the SubscriberID="1234" to SubscriberID="5678" before sending to the target server. How to achieve this in Apigee Edge.
Below is the xml request.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://xml.company.com/transaction">
<SOAP-ENV:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wsse:UsernameToken wsu:Id="SecurityToken-ea4855e0-675b-4ac5-uur9-e17kjfc5f1eb">
<wsse:Username>xxxxx</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxxx</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<tns:GetData xmlns:s0="http://xml.company.com/globals" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:TransactionRequest XMLVersion="2.0" Timestamp="2019-12-03T14:09:11.246">
<tns:Subscriber SubscriberID="1234" UserName="xxx"/>
</tns:TransactionRequest>
</tns:GetData>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Hi
the first hit on this search will bring you to a Q&A on this forum that talks about three options that I like for modifying an XML document: XSLT, JavaScript (E4X), or a specific Java callout. The answer I gave there also applies to your case.
Hi dchiesa1,
I am not able to set the attribute. It is failing. I have tried the same way mentioned in above sample. Below is my javascript code.
var message = context.getVariable('request');
if (message && message.content) {
var xml = new XML(message.content);
var tns = new Namespace('tns','http://xml.company.com/transaction');
var soap = new Namespace('SOAP-ENV','http://schemas.xmlsoap.org/soap/envelope/');
var body = xml.soap::Body;
var subscriberId = body.tns::GetData.tns::TransactionRequest.tns::Subscriber.SubscriberID.text();
subscriberId.parent().setChildren('5678');
}
Alternate use https://github.com/DinoChiesa/Apigee-Java-Edit-Xml-Node & works well..
The SubscriberID is an attribute in your document, not an element. With E4X, you must use different syntax to manipulate elements and attributes. Also, please use context.getVariable('message.content')
to get the message content. I think you need something like this:
var xml = new XML(context.getVariable('message.content'));
var tns = new Namespace('tns','http://xml.company.com/transaction');
var soap = new Namespace('SOAP-ENV','http://schemas.xmlsoap.org/soap/envelope/');
var body = xml.soap::Body;
var subscriber = body.tns::GetData.tns::TransactionRequest.tns::Subscriber;
// if you want to examine the existing attribute value, retrieve it from the element
// var subID = subscriber.@SubscriberID;
// print('SubId: ' + subID);
// update the attribute value
subscriber.@SubscriberID = '5678';
// xml.toXMLString() will be pretty-printed
//print('\nafter:\n' + xml.toXMLString());
context.setVariable('message.content', xml.toXMLString());
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |