Need to transform xml payload with dynamic value using XSLT transformation.

Input xml payload below. Need to update the Data64Binary tag with updated dynamic string value (value extracted in previous policy).

<S:Envelope
    xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <attachmentRequest
        xmlns:ns3="http://abc.com"
        xmlns:ns4="http://www.w3.org/2004/08/xop/include">
      <ns3:FormInstance>
        <ns3:Attachment id="xxx00">
          <ns3:DateCreated>2008-04-24</ns3:DateCreated>
          <ns3:BasicType tc="3">File</ns3:BasicType>
          <ns3:Data64Binary>
            <ns4:Include href="cid:xyz"/>
          </ns3:Data64Binary>
          <ns3:Type tc="10">Other</ns3:Type>
        </ns3:Attachment>
      </ns3:FormInstance>
    </attachmentRequest>
  </S:Body>
</S:Envelope>

 

Response (with updated Data64Binary tag)

<S:Envelope
    xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <attachmentRequest
        xmlns:ns3="http://abc.com"
        xmlns:ns4="http://www.w3.org/2004/08/xop/include">
      <ns3:FormInstance>
        <ns3:Attachment id="xxx00">
          <ns3:DateCreated>2008-04-24</ns3:DateCreated>
          <ns3:BasicType tc="3">File</ns3:BasicType>
          <ns3:Data64Binary>
            dynamic value
          </ns3:Data64Binary>
          <ns3:Type tc="10">Other</ns3:Type>
        </ns3:Attachment>
      </ns3:FormInstance>
    </attachmentRequest>
  </S:Body>
</S:Envelope>
Solved Solved
0 5 901
1 ACCEPTED SOLUTION

yes you can do it with XSLT.  Did you read my response?  Did you look into the Java callout?  Does it NOT meet your needs?  The XOP handler does exactly what you described. 

If you don't like that answer I can work on an XSLT example, but ... remember, you don't need me to solve that. I'm glad to help, but it's just XSLT.  There's nothing Apigee-specific about the XSLT you would use.  Stackoverflow has tons of advice on how to use XSL to solve basic problems like this.

EDIT

This simple XSLT sheet works for me. 

<xsl:stylesheet version="1.0"
                xmlns:ns3="http://abc.com"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml"
              encoding="utf-8"
              indent="yes" omit-xml-declaration="yes"/>

  <xsl:param name="newvalue" select="'default-value-here'"/>

  <!-- pass through for all elements and attributes -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- replace the value of one element -->
  <xsl:template match="//ns3:Data64Binary">
    <ns3:Data64Binary>
      <xsl:value-of select="$newvalue"/>
    </ns3:Data64Binary>
  </xsl:template>

</xsl:stylesheet>

 

The XSLT policy in Apigee needs to pass in a parameter called "newvalue".  This  looks like this: 

<XSL name='XSL-Replace-Data64Binary'>
  <Source>request</Source> <!-- or whatever you like -->
  <OutputVariable>transformedContent</OutputVariable>
  <ResourceURL>xsl://the-sheet-dino-wrote.xsl</ResourceURL>

  <!--
      Parameters are optional for any XSL sheet. Reference them in the XSL as:
      <xsl:param name="paramname" select="'default-value-1'"/>
  -->
  <Parameters ignoreUnresolvedVariables='true'>
    <Parameter name='newvalue' ref='context-variable-containing-dynamic-value' />   
  </Parameters>
</XSL>

 

When I try this on the source XML you provided, I get: 

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
      <attachmentRequest xmlns:ns3="http://abc.com" xmlns:ns4="http://www.w3.org/2004/08/xop/include">
         <ns3:FormInstance>
            <ns3:Attachment id="xxx00">
               <ns3:DateCreated>2008-04-24</ns3:DateCreated>
               <ns3:BasicType tc="3">File</ns3:BasicType>
               <ns3:Data64Binary>the-value-to-inject</ns3:Data64Binary>
               <ns3:Type tc="10">Other</ns3:Type>
            </ns3:Attachment>
         </ns3:FormInstance>
      </attachmentRequest>
  </S:Body>
</S:Envelope>

 

View solution in original post

5 REPLIES 5

@dchiesa1 Appreciate your Support.

Need to transform xml payload with dynamic value using XSLT transformation.

I am rooting for you! Anyone who needs to use XSLT, deserves compassion and support.

If what you want to do is take a multi-part message that uses XOP Include, and transform that to a SOAP message that embeds the content directly, then this Java callout may help you.  

Let me know if this is what you want. There is a simple XSLT solution to the problem as you posed it.  But it's possible there is a simpler way to get to the goal you have, if you're just dealing with XOP. 

Can we do this with XSLT? I need to replace the value of tag with a string which will be dynamic. As I posted earlier xml payload will remain same, only one tag value gets change.

@dchiesa1 

yes you can do it with XSLT.  Did you read my response?  Did you look into the Java callout?  Does it NOT meet your needs?  The XOP handler does exactly what you described. 

If you don't like that answer I can work on an XSLT example, but ... remember, you don't need me to solve that. I'm glad to help, but it's just XSLT.  There's nothing Apigee-specific about the XSLT you would use.  Stackoverflow has tons of advice on how to use XSL to solve basic problems like this.

EDIT

This simple XSLT sheet works for me. 

<xsl:stylesheet version="1.0"
                xmlns:ns3="http://abc.com"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml"
              encoding="utf-8"
              indent="yes" omit-xml-declaration="yes"/>

  <xsl:param name="newvalue" select="'default-value-here'"/>

  <!-- pass through for all elements and attributes -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- replace the value of one element -->
  <xsl:template match="//ns3:Data64Binary">
    <ns3:Data64Binary>
      <xsl:value-of select="$newvalue"/>
    </ns3:Data64Binary>
  </xsl:template>

</xsl:stylesheet>

 

The XSLT policy in Apigee needs to pass in a parameter called "newvalue".  This  looks like this: 

<XSL name='XSL-Replace-Data64Binary'>
  <Source>request</Source> <!-- or whatever you like -->
  <OutputVariable>transformedContent</OutputVariable>
  <ResourceURL>xsl://the-sheet-dino-wrote.xsl</ResourceURL>

  <!--
      Parameters are optional for any XSL sheet. Reference them in the XSL as:
      <xsl:param name="paramname" select="'default-value-1'"/>
  -->
  <Parameters ignoreUnresolvedVariables='true'>
    <Parameter name='newvalue' ref='context-variable-containing-dynamic-value' />   
  </Parameters>
</XSL>

 

When I try this on the source XML you provided, I get: 

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
      <attachmentRequest xmlns:ns3="http://abc.com" xmlns:ns4="http://www.w3.org/2004/08/xop/include">
         <ns3:FormInstance>
            <ns3:Attachment id="xxx00">
               <ns3:DateCreated>2008-04-24</ns3:DateCreated>
               <ns3:BasicType tc="3">File</ns3:BasicType>
               <ns3:Data64Binary>the-value-to-inject</ns3:Data64Binary>
               <ns3:Type tc="10">Other</ns3:Type>
            </ns3:Attachment>
         </ns3:FormInstance>
      </attachmentRequest>
  </S:Body>
</S:Envelope>

 

Thanks Dino, its working for me.