How To: Modifying or Filtering on XML Element Attribute Values

XML attributes are normally used to describe XML elements, or to provide additional information about elements.

For example, the XML element "person" may have an associated XML attribute "name" which lists the first name of the person as "user1".  This is represented as:

<person name="user1">

Where "name" is the XML attribute.

 

Sentry administrators may need to filter on or modify XML attribute values using the Identify Document task or a Mapping task.

To target an XML attribute value, rather than an element value, Sentry administrators will need to manually enter the appropriate XPath expression.

 

Examples

 

I. Modifying an XML attribute value

The use case is to modify the "status" XML attribute value in the following XML response message from "201" to "200" as the message passes through Sentry. 

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://crosschecknet.com/">

  <soap:Body>

    <tns:EchoResponse>

      <tns:EchoResult status="200">test</tns:EchoResult>

    </tns:EchoResponse>

  </soap:Body>

</soap:Envelope>

 

1. Load an example of the response XML document into Sentry on the Resources>>Documents page. 

2. Create a new task list and reference the document from step 1 as the Sample Document. 

3. Add a new "Map to XML" task with the following criteria:

a. Map From: Constant

b. Select target elements: select s0:EchoResult and click Apply

c. Change the Element value from: /soap:Envelope/soap:Body/s0:EchoResponse/s0:EchoResult  to /soap:Envelope/soap:Body/s0:EchoResponse/s0:EchoResult/@status

d. Change the Constant value to 201

4. Add the task list to a Task List Group and associate to the Content Policy in the correct location.

Notice that the attribute name is added to the end of the XPath using the @ symbol.

 

II. Matching a request by XML attribute value

The use case is to match an incoming request when the value of the second  XML attribute is "value2". Notice that there are multiple XML attribute values for this element.

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

  <soap:Body>

    <tns:Echo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://crosschecknet.com/">

      <tns:Buf Name="name1" Input="value2">test</tns:Buf>

    </tns:Echo>

  </soap:Body>

</soap:Envelope>

 

1. Load an example of the request XML document into Sentry on the Resources>>Documents page.

2. Create a new task list and reference the document from step 1 as the Sample Document.

3. Add a new "Identify Document" task with the following criteria:

a. Create a document filter by selecting the node s0:Buf and clicking Apply

b. Change the Path field from: /soap:Envelope/soap:Body/s0:Echo/s0:Buf to /soap:Envelope/soap:Body/s0:Echo/s0:Buf/@Input

c. Set the Comparator to =

d. Set the Value to "value2"  (note the system will add the quotes for you)

4. Add the task list to a Task List Group and associate to the Content Policy in the correct location.

 

III. Matching a request by XML attribute with duplicate XML Elements

If there are multiple XML Elements of the same name at the same level, and you only want to match or modify one of the XML attributes, use the notation [1] or [2] - or whichever iteration you want - in the expression.  

 For instance, in the XML below, to match the second tns:Buf Name attribute, use the expression:

 /soap:Envelope/soap:Body/s0:Echo/s0:Buf[2]/@Name

 <?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

  <soap:Body>

    <tns:Echo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://crosschecknet.com/">

      <tns:Buf Name="value1" Input="value1">test</tns:Buf>

      <tns:Buf Name="value2" Input="value2">test</tns:Buf>

    </tns:Echo>

  </soap:Body>

</soap:Envelope>

 

IV. Matching a Document by XML Namespace

Sentry's XPath support is not strictly limited by XPath namespace syntax.  While Sentry supports full xpath syntax, xpath namespace syntax is complex.  Sentry can hide the namespace matching from end users.

Sentry automatically checks the namespace values in the runtime message based on the namespaces used in the sample doc.  So it is typically not necessary to build a filter for an XML namespace.

Namespaces use a completely different xpath syntax from other attributes.

Below is the XPath syntax for checking the namespace:

/soapenv:Envelope/soapenv:Header/*[namespace-uri()='put_namespace_uri_here']

Set the comparator to exists.

 

 

 

Note that the Sample Document doesn't have to always match the XPath expression entered into the Path field. If it does not, you will receive the error "The expressions do not match the sample document."  This error does not necessarily mean that runtime processing with this configuration will fail, because the runtime message might differ slightly from the sample document. 

 

For more information on XML Attributes see:  http://www.w3schools.com/xml/xml_attributes.asp

 

Have more questions? Submit a request

0 Comments

Article is closed for comments.