DataPower Gateway (Classic)
only

Constructing XPath expressions to redact fields

To define a field for redaction when using the DataPower® Gateway (v5 compatible), you supply an XPath expression that specifies the field that you want to redact. If your API requests and responses use XML format, you can base your XPath statements directly on the XML content. If your API requests and responses use JSON format, you must use the DataPower XML representation of the JSON content, JSONx, to construct your XPath expression.

About this task

The following sections provide examples for constructing XPath expressions to redact a field; examples are provided for API responses in both XML and JSON format:

XPaths for XML

Consider the following example of an XML response:

<xml>
  <primaryAddress>
    <streetAddress>21 2nd Street</streetAddress>
    <city>New York</city>
    <state>NY</state>
    <postalCode>10021</postalCode>
  </primaryAddress>
  <secondaryAddress>
    <streetAddress>412 Brooklyn Avenue</streetAddress>
    <city>New Jersey</city>
    <state>NJ</state>
    <postalCode>12302</postalCode>
  </secondaryAddress>
</xml>
To redact "streetAddress" in the preceding example the XPath expression would be: //streetAddress
where the components of the expression have the following meaning:
Expression Meaning
// search anywhere in the XML structure
//streetAddress search anywhere in the XML structure for an element of type "streetAddress"
The XML response that results from applying this XPath expression is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <primaryAddress>
    <streetAddress>*******</streetAddress>			
    <city>New York</city>
    <state>NY</state>
    <postalCode>10021</postalCode>
  </primaryAddress>
  <secondaryAddress>
    <streetAddress>*******</streetAddress>			
    <city>New Jersey</city>
    <state>NJ</state>
    <postalCode>12302</postalCode>
  </secondaryAddress>
</xml>
Note: Both incidences of "streetAddress" have been redacted.
To redact one specific incidence of "streetAddress" in the initial example, the XPath expression would be: //secondaryAddress/streetAddress
where the components of the expression have the following meaning:
Expression Meaning
//secondaryAddress search anywhere in the XML structure for an element of type "secondaryAddress"
//secondaryAddress/streetAddress search under the preceding element for a child element of type "streetAddress"
The XML response that results from applying this XPath expression is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <primaryAddress>
    <streetAddress>21 2nd Street</streetAddress>
    <city>New York</city>
    <state>NY</state>
    <postalCode>10021</postalCode>
  </primaryAddress>
  <secondaryAddress>
    <streetAddress>*******</streetAddress>			
    <city>New Jersey</city>
    <state>NJ</state>
    <postalCode>12302</postalCode>
  </secondaryAddress>
</xml>
Note: Only the secondary address has been redacted.
XPaths for JSON
Consider the following example of a JSON response:
{
  "primaryAddress": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  },
  "secondaryAddress": {
    "streetAddress": "412 Brooklyn Avenue",
    "city": "New Jersey",
    "state": "NJ",
    "postalCode": 12302
  }
}
Unlike the preceding XML example, to construct an XPath for this example you must use the corresponding DataPower XML representation, JSONx. The JSONx equivalent of this JSON response is as follows:
<json:object xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <json:object name="primaryAddress">
    <json:string name="streetAddress">21 2nd Street</json:string>
    <json:string name="city">New York</json:string>
    <json:string name="state">NY</json:string>
    <json:number name="postalCode">10021</json:number>
  </json:object>
  <json:object name="secondaryAddress">
    <json:string name="streetAddress">412 Brooklyn Avenue</json:string>
    <json:string name="city">New York</json:string>
    <json:string name="state">NY</json:string>
    <json:number name="postalCode">10021</json:number>
  </json:object>
</json:object>]
To redact the element "streetAddress" from the preceding JSONx structure, the XPath expression would be: //*[@name='streetAddress']
where the components of the expression have the following meaning:
Expression Meaning
//* find any element anywhere in the structure
[@name='streetAddress'] this element has a 'name' property with value "streetAddress"
The JSON response that results from applying this XPath expression is as follows:
{
  "primaryAddress": {
    "streetAddress": "*******",			
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  },
  "secondaryAddress": {
    "streetAddress": "*******",			
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  }
}
Note: All incidences of "streetAddress" have been redacted in the preceding JSON structure.
To redact a specific occurrence of the "streetAddress" element, the XPath expression would be: //*[@name='secondaryAddress']/*[@name='streetAddress']
where the components of the expression have the following meaning:
Expression Meaning
//* find any element anywhere in the structure
//*[@name='secondaryAddress'] find an element anywhere in the structure that is named "secondaryAddress"
//*[@name='secondaryAddress']/*[@name='streetAddress'] find a child element of any type (/* ) where the child element has a name of "streetAddress"
The JSON response that results from applying this XPath expression is as follows:
{
  "primaryAddress": {
    "streetAddress": "21 2nd Street",		
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  },
  "secondaryAddress": {
    "streetAddress": "*******",			
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  }
}
Note: Only the street address for the secondary address has been redacted.

What to do next

See Including elements in your OpenAPI 2.0 API assembly, Including elements in your OpenAPI 2.0 API assembly to learn how to apply a redact policy using XPath.