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! Go to 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:
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>
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!