How to add an additional xml node into xml response received from target endpoint

Not applicable

Final response should be xml response received from called target endpoint plus an additional xml tag indicator with fixed boolean value.Please suggest

How to add an additional xml node into xml response received from target endpoint

Solved Solved
0 3 2,468
1 ACCEPTED SOLUTION

@paridhi agarwal, you can use a generic xsl transformation that just adds your new element to the root element (whatever that may be) and copies all its children: (XSLT adapted from https://stackoverflow.com/questions/25957167/xslt-to-add-new-elements-to-the-root-element-of-an-xml)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <!-- match the root element of unknown name -->
  <xsl:template match="/*">
     <xsl:copy>
       <!-- copy its children -->
       <xsl:copy-of select="@*|node()"/>
       <!-- add a new element at the end -->
       <name>abc</name>
     </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

View solution in original post

3 REPLIES 3

@Paridhi Agarwal ,

Use Apigee XSLT Policy, More about same here & See it in action here using Apigee 4 Minute Videos.

-------------------------------

Anil Sagar

5997-screen-shot-2017-11-23-at-75916-pm.png Learn Apigee Concepts in 4 Minutes HandsOn

Thanks much for the response Anil. I tried implementing it , but now the challenge is xml structure returned by target url is not always fixed and known. So I will not be having any xsl stylesheet to refer to. I just know watever be the response , we have to add " <name>abc</name> " tag in the end and send back to client. Please suggest .

@paridhi agarwal, you can use a generic xsl transformation that just adds your new element to the root element (whatever that may be) and copies all its children: (XSLT adapted from https://stackoverflow.com/questions/25957167/xslt-to-add-new-elements-to-the-root-element-of-an-xml)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <!-- match the root element of unknown name -->
  <xsl:template match="/*">
     <xsl:copy>
       <!-- copy its children -->
       <xsl:copy-of select="@*|node()"/>
       <!-- add a new element at the end -->
       <name>abc</name>
     </xsl:copy>
  </xsl:template>
</xsl:stylesheet>