modify xml request

I have incoming request has pay load as follows.

<?xml version="1.0" encoding="UTF-8"?>
  <abcMessage messageId="234723487234234" receiptDate="YYYY-MM-DD HH:MM:SS Z" attemptNumber="1">
    <source address="+919585" carrier="103" type="MDN" />
    <destination address="12345" type="SC" />
    <message>123857AB12</message>
  </abcMessage>

I want to change request to as follows.

<?xml version="1.0" encoding="UTF-8"?>
<InRequest>
	<abcMessage messageId="234723487234234" receiptDate="YYYY-MM-DD HH:MM:SS Z" attemptNumber="1">
    		<source address="+919585" carrier="103" type="MDN" />
    		<destination address="12345" type="SC" />
    		<message>123857AB12</message>
	</abcMessage>
</InRequest>

How do I change incoming request body to the above format?

0 2 163
2 REPLIES 2

There are a couple ways to modify XML.

If all you want to do is wrap an existing XML payload in a parent element, then, you can do that in Apigee's JS callout pretty easily.

The JS policy would look like this

<Javascript name='JS-WrapXml' timeLimit='1200' >
  <Properties>
    <Property name='sourceMessage'>request</Property>
    <Property name='newParentElementName'>InRequest</Property>
  </Properties>
  <ResourceURL>jsc://wrapXml.js</ResourceURL>
</Javascript>

And the JS itself would be like this:

// wrapXml.js
// ------------------------------------------------------------------
//
// wrap an XML message in a parent element using E4X.
//
var message = context.getVariable(properties.sourceMessage);
var newElementName = properties.newParentElementName;
if (message && message.content) {
  var origXml = new XML(message.content);
  var wrapper = new XML('<'+ newElementName +'/>');
  wrapper.appendChild(origXml);
  // replace the content of the message with the modified version
  message.content = wrapper.toXMLString();
}

If you want a more general way of modifying XML, then you can use the XSL policy in Apigee to accomplish that.

The XSL policy might look like this:

<XSL name='XSL-WrapXml'>
  <DisplayName>XSL-WrapXml</DisplayName>
  <Source>request</Source>
  <OutputVariable>request.content</OutputVariable>
  <ResourceURL>xsl://wrapXml.xsl</ResourceURL>
  <Parameters ignoreUnresolvedVariables='true'>
    <Parameter name='new_parent_element'>InRequest</Parameter>
  </Parameters>
</XSL>

And the trivial XSL to wrap a document in a parent element would look like this:

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"
              encoding="utf-8"
              indent="yes"
              xslt:indent-amount="2" xmlns:xslt="http://xml.apache.org/xslt" />
  <xsl:strip-space elements="*"/>
  <!-- this parameter is set in the XSL Policy config -->
  <xsl:param name="new_parent_element" select="'Unset'"/>
  <xsl:template match="/">
    <xsl:element name='{$new_parent_element}'>
      <xsl:copy-of select="."/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

attached is a working example showing both of these options.

apiproxy-wrap-xml-in-parent-20210329-164636.zip

Balaji, did my answer help?