How to remove xml block from response

Not applicable

I am green. I have an xml similar to the following:

<b:Response xmlns:b="b">
  <b:document>
    <b:documentProperties>
      <b:name>Property1</b:name>
      <b:value>Valule1</b:value>
    </b:documentProperties>
    <b:documentProperties>
      <b:name>Property2</b:name>
      <b:value>Value2</b:value>
    </b:documentProperties>
  </b:document>
</b:Response>

I want to remove the name value pair associated to Property2. I have attempted this with the Assign Variable - Remove option, but without luck. Any suggestions or links to examples would be great.

Solved Solved
1 5 2,297
1 ACCEPTED SOLUTION

First, your XML wasn't quite well-formed. I introduced a namespace declaration for b, and removed a stray element. I'll use this form, for the examples below.

<b:Response xmlns:b="b">
  <b:document>
    <b:documentProperties>
      <b:name>Property1</b:name>
      <b:value>Valule1</b:value>
    </b:documentProperties>
    <b:documentProperties>
      <b:name>Property2</b:name>
      <b:value>Value2</b:value>
    </b:documentProperties>
  </b:document>
</b:Response>

There are two ways that I know of to do this:

  1. With the built-in XSL policy
  2. With the extension policy (Java callout): Edit-XML

The built-in XSL policy

XSL is the XML Stylesheet language, which allows you to transform an inbound XML document to something else (often another XML document). XSL is a general purpose language, but one thing you can do with the XSL policy is remove an element. Using XSL in this way to modify an XML document is independent of Apigee Edge. To make this work in your API proxy, just follow a recipe for XSL that you can find elsewhere. Here is one example.

The XSL in your case, given the valid document I posted above, might look like this:

<xsl:stylesheet version="1.0"
                xmlns:b="b"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="b:documentProperties[b:name='Property2']"/>
</xsl:stylesheet>

The Extension policy

Some people don't care to use XSL for these simple cases. They may find it hard to use or to read. For that purpose, there is a more special-purpose policy, an extension that is not part of the apigee Edge product. It allows you to modify the XML document by editing a single value, removing a single node ( element or attribute), or adding a single node (element or attribute).

The configuration of the policy in your case, given the valid document I posted above, might look like this:

<JavaCallout name='Java-RemoveElement'>
  <Properties>
    <Property name='xmlns:b'>b</Property>
    <Property name='source'>myMessage.content</Property>
    <Property name='xpath'>/b:Response/b:document/b:documentProperties[b:name/text()='Property2']</Property>
    <Property name='action'>remove</Property>
  </Properties>
  <ClassName>com.google.apigee.edgecallouts.EditXmlNode</ClassName>
  <ResourceURL>java://edge-custom-edit-xml-node-1.0.7.jar</ResourceURL>
</JavaCallout>

If your use-case calls for removing multiple nodes of a particular type, then you should use XSL!

View solution in original post

5 REPLIES 5

Not applicable

And if removing the Property2 block is not an option, changing the value from Value2 to ABC123 would also work.

First, your XML wasn't quite well-formed. I introduced a namespace declaration for b, and removed a stray element. I'll use this form, for the examples below.

<b:Response xmlns:b="b">
  <b:document>
    <b:documentProperties>
      <b:name>Property1</b:name>
      <b:value>Valule1</b:value>
    </b:documentProperties>
    <b:documentProperties>
      <b:name>Property2</b:name>
      <b:value>Value2</b:value>
    </b:documentProperties>
  </b:document>
</b:Response>

There are two ways that I know of to do this:

  1. With the built-in XSL policy
  2. With the extension policy (Java callout): Edit-XML

The built-in XSL policy

XSL is the XML Stylesheet language, which allows you to transform an inbound XML document to something else (often another XML document). XSL is a general purpose language, but one thing you can do with the XSL policy is remove an element. Using XSL in this way to modify an XML document is independent of Apigee Edge. To make this work in your API proxy, just follow a recipe for XSL that you can find elsewhere. Here is one example.

The XSL in your case, given the valid document I posted above, might look like this:

<xsl:stylesheet version="1.0"
                xmlns:b="b"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="b:documentProperties[b:name='Property2']"/>
</xsl:stylesheet>

The Extension policy

Some people don't care to use XSL for these simple cases. They may find it hard to use or to read. For that purpose, there is a more special-purpose policy, an extension that is not part of the apigee Edge product. It allows you to modify the XML document by editing a single value, removing a single node ( element or attribute), or adding a single node (element or attribute).

The configuration of the policy in your case, given the valid document I posted above, might look like this:

<JavaCallout name='Java-RemoveElement'>
  <Properties>
    <Property name='xmlns:b'>b</Property>
    <Property name='source'>myMessage.content</Property>
    <Property name='xpath'>/b:Response/b:document/b:documentProperties[b:name/text()='Property2']</Property>
    <Property name='action'>remove</Property>
  </Properties>
  <ClassName>com.google.apigee.edgecallouts.EditXmlNode</ClassName>
  <ResourceURL>java://edge-custom-edit-xml-node-1.0.7.jar</ResourceURL>
</JavaCallout>

If your use-case calls for removing multiple nodes of a particular type, then you should use XSL!

Thank you! I was able to implement the xslt successfully!

Great! In your cases you wanted to remove 2 nodes, it seems like (the ISNAM thing for both documents).... so for that you need XSL.

I'm glad it worked for you.

Not applicable

@Dino-at-Google Ok, I guess I was over simplifying the example.

The xml actually looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
  <soapenv:Body>
    <b:searchresponse
        xmlns:a="http://nationwide.com/schemas/SOASolutions/SystemInfo_4"
        xmlns:b="b"
        xmlns:c="http://www.w3.org/2004/11/xmlmime">
      <a:SystemInfo>
        <a:ServiceContext>
          <a:transactionId>ljcmemsrc001</a:transactionId>
          <a:sourceLogicalId>docmgmtmb</a:sourceLogicalId>
        </a:ServiceContext>
        <a:ReturnInfo>
          <a:returnSeverity>Successful</a:returnSeverity>
          <a:commonReturnCode>0</a:commonReturnCode>
          <a:commonReturnMessage>Search of documents successful!</a:commonReturnMessage>
        </a:ReturnInfo>
      </a:SystemInfo>
      <b:document>
        <b:documentProperties>
          <b:name>POLICYNO</b:name>
          <b:value>abc123</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>ACCNT</b:name>
          <b:value>abc123 </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>AGCYNUM</b:name>
          <b:value>12345 </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>EFFDAT</b:name>
          <b:value>07182017</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>INSNAM</b:name>
          <b:value>insuredname </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>LINOBS</b:name>
          <b:value>PACKAGE CONTROL </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>RUNDAT</b:name>
          <b:value>09012018</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>TRANSA</b:name>
          <b:value>AMENDMENT </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>REPORTID</b:name>
          <b:value>abc123 </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>VERSION</b:name>
          <b:value>abc123</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>SECTION</b:name>
          <b:value>abc123</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>AVAILABILITY</b:name>
          <b:value>DISK</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>REPOSITORY</b:name>
          <b:value>DSMPROD</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>REPORTNAME</b:name>
          <b:value>COMMERCIAL LINES - COMMERCIAL INSUREDS - CUSTOMER </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>SECTION_PAGES</b:name>
          <b:value>3</b:value>
        </b:documentProperties>
      </b:document>
      <b:document>
        <b:documentProperties>
          <b:name>POLICYNO</b:name>
          <b:value>abc123</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>ACCNT</b:name>
          <b:value>abc123 </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>AGCYNUM</b:name>
          <b:value>12345 </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>EFFDAT</b:name>
          <b:value>07182017</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>INSNAM</b:name>
          <b:value>insuredname</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>LINOBS</b:name>
          <b:value>PACKAGE CONTROL </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>RUNDAT</b:name>
          <b:value>09012018</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>TRANSA</b:name>
          <b:value>AMENDMENT </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>REPORTID</b:name>
          <b:value>PPZFCOPC </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>VERSION</b:name>
          <b:value>20180509052155</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>SECTION</b:name>
          <b:value>abc123</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>AVAILABILITY</b:name>
          <b:value>DISK</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>REPOSITORY</b:name>
          <b:value>DSMPROD</b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>REPORTNAME</b:name>
          <b:value>COMMERCIAL LINES - COMMERCIAL INSUREDS - CUSTOMER </b:value>
        </b:documentProperties>
        <b:documentProperties>
          <b:name>SECTION_PAGES</b:name>
          <b:value>3</b:value>
        </b:documentProperties>
      </b:document>
    </b:searchresponse>
  </soapenv:Body>
</soapenv:Envelope>


I set up the java callout like this:

<JavaCallout name="JavaCalloutRemoveElement">
  <Properties>
    <Property name='xmlns:b'>b</Property>
    <Property name='xmlns:soapenv'>http://www.w3.org/2003/05/soap-envelope</Property>
    <Property name='source'>response.content</Property>
    <Property name='xpath'>/soapenv:Envelope/soapenv:Body/b:searchElectronicDocumentResponse/b:document/b:documentProperties[b:name/text()='INSNAM']</Property>
    <Property name='action'>remove</Property>
  </Properties>
  <ClassName>com.google.apigee.edgecallouts.EditXmlNode</ClassName>
  <ResourceURL>java://edge-custom-edit-xml-node-1.0.7.jar</ResourceURL>
</JavaCallout>

What would you suggest for dealing with multiple document(s) within the search response, and removing the INSNAM block?

Thank you in advance for your patience and assistance!