How can I modify an XML request payload?

I have a use case where I need to modify XML payload

Payload is :

<data>
    <criterion>
       <name>BusinessUnit</name>
       <operator>EQ</operator>
       <value>0987</value>
    </criterion>
</data>

I need to add one more element inside data element like:

<data>
    <criterion>
        <name>CN</name>
        <operator>EQ</operator>
        <value>K12345</value>
    </criterion>
    <criterion>
        <name>Business Unit</name>
        <operator>EQ</operator>
        <value>0987</value>
    </criterion>
</data>

How can I do this?

Thanks 

Solved Solved
1 2 1,180
1 ACCEPTED SOLUTION

There are multiple ways to do this in Apigee.

  1. One way, probably the most straightforward way technically speaking, is via an XSLT policy.  XSLT is "XML to XML transform".  It's designed specifically for the general problem of transforming one XML document to another.  The sheet is really simple. It looks like this:

     

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    
        <xsl:template match="/data">
          <data>
            <criterion>
              <name>CN</name>
              <operator>EQ</operator>
              <value>K12345</value>
            </criterion>
            <xsl:apply-templates/>
          </data>
        </xsl:template>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        
    </xsl:transform>
    

     

    You can even try it online here. The downside is, to use it you need to be comfortable with XSLT. That's not always acceptable. 

  2. Another way is to use the JavaScript policy and the E4X extension.  Here is a recent community thread discussing how to use it.

  3. A third option is to use THIS Java Callout, which allows you to insert elements into XML documents. There is an example in the README that describes how to insert a complex element, of the form you describe, into an existing document.

Which option you choose, depends on which thing you'd be more comfortable using and maintaining. They all work just fine.

View solution in original post

2 REPLIES 2

There are multiple ways to do this in Apigee.

  1. One way, probably the most straightforward way technically speaking, is via an XSLT policy.  XSLT is "XML to XML transform".  It's designed specifically for the general problem of transforming one XML document to another.  The sheet is really simple. It looks like this:

     

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    
        <xsl:template match="/data">
          <data>
            <criterion>
              <name>CN</name>
              <operator>EQ</operator>
              <value>K12345</value>
            </criterion>
            <xsl:apply-templates/>
          </data>
        </xsl:template>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        
    </xsl:transform>
    

     

    You can even try it online here. The downside is, to use it you need to be comfortable with XSLT. That's not always acceptable. 

  2. Another way is to use the JavaScript policy and the E4X extension.  Here is a recent community thread discussing how to use it.

  3. A third option is to use THIS Java Callout, which allows you to insert elements into XML documents. There is an example in the README that describes how to insert a complex element, of the form you describe, into an existing document.

Which option you choose, depends on which thing you'd be more comfortable using and maintaining. They all work just fine.

Thanks @dchiesa1  It worked.

 

I have extracted the certificate value in a variable "pid"  using AssignMessage policy and now, I am trying to set inside the value tag of XML:

like I Want to replace "K12345" with the value of pid variable.

 

<criterion>
     <name>CN</name>
     <operator>EQ</operator>
     <value>K12345</value>
</criterion>

 

  Could you please explain some way to do that.

 

Thanks & Regards,

Shivendra