Conditionally removing XML elements from a message

Document ID : KB000057544
Last Modified Date : 14/02/2018
Show Technical Document Details

Solution

Backround

The Gateway is capable of removing individually selected elements using the?Add or Remove XML Elements assertion. This assertion can removed the results of a specific XPath evaluation from a message. This may be useful if the request message contains XPath credentials (such as a username/password combination or SAML tokens) that are accepted by the Gateway but may not be understood by the protected service.

In some circumstances, it may be desirable to remove certain elements from an XML message if they contain certain values. For example, a policy author may want to remove a node where a value or attribute matches a certain value. The?Add or Remove?XML Elements assertion is broad--insofar as it will remove any instances of an element regardless of the value of nodes or attributes. The?Add or Remove XML Elements?assertion relies on a pointer to select data to remove and cannot be aggressively selective in its implementation.

For conditional removal of XML elements based on the value of a node or attribtue, the?Apply XSL Transformation?assertion would be an appropriate choice. XSL transformations are more appropriate for conditional changes to XML documents based on the data within or structure of a document. In the example to be given--an element will be removed from a message if an attribute in a particular element matches a certain value

Implementation

The Apply XSL Transformation assertion can be added to any policy for a published SOAP, RESTful, or Web API service. When the assertion is added, the assertion properties dialog will appear. This assertion uses the XSL template element using the match attribute to specify a pattern to match. Since the XSL template element does not specify any output--the matched template will be replaced with nothing and will be effectively removed. A sample configuration is pictured below:

A screen capture displaying a properties dialog for an XSL transformation that deletes elements conditionally

The XML for the assertion is attached below for adding into any policy. This XML should be copied and pasted into the service policy to add it. It will not overwrite or override any existing policy assertions.

<?xml version="1.0" encoding="UTF-8"?>
<wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">
? ? <wsp:All wsp:Usage="Required">
? ? ? ? <L7p:XslTransformation>
? ? ? ? ? ? <L7p:ResourceInfo staticResourceInfo="included">
? ? ? ? ? ? ? ? <L7p:Document stringValueReference="inline"><![CDATA[<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
? <!-- the identity template --> ? ?
? <xsl:template match="@* | node()">
? ? <xsl:copy>
? ? ? <xsl:apply-templates select="@* | node()"/>
? ? </xsl:copy>
? </xsl:template>
<xsl:template match="element[@attribute='foo']"/>
</xsl:stylesheet>]]></L7p:Document>
? ? ? ? ? ? </L7p:ResourceInfo>
? ? ? ? ? ? <L7p:TransformName stringValue=""/>
? ? ? ? ? ? <L7p:XsltVersion stringValue="2.0"/>
? ? ? ? </L7p:XslTransformation>
? ? </wsp:All>
</wsp:Policy>

The XSL stylesheet used for reference is printed below. This XML should be copied and pasted into the?Apply XSL Transformation assertion itself:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
? <!-- the identity template --> ? ?
? <xsl:template match="@* | node()">
? ? <xsl:copy>
? ? ? <xsl:apply-templates select="@* | node()"/>
? ? </xsl:copy>
? </xsl:template>
<xsl:template match="element[@attribute='foo']"/>
</xsl:stylesheet>