This topic explains how to configure and apply the XML Sort tool, which sorts XML recursively or by selected sort target and sort key nodes.

Sections include:

Understanding XML Sort

Many services return lists of results that are in a different order each time that the service is invoked. Such random ordering of elements can make it challenging to define validations in test cases. The XML Sort tool is designed to address this challenge. For example, it could be used to take the book items returned by the Parasoft Book Store service, reorder them by price, and sort the authors list lexicographically under each book item. The XML Sort tool can be chained to the output of other XML tools, such as the SOAP Response of the SOAP Client. After you add the tool and define the desired sorting, it will output the XML content sorted according to the specified configuration. The sorted content can then be validated with XML Assertor, Diff, or other validation tools.

The Recursive XML sort option will sort all nodes recursively (for example, to remove ordering prior to diffing). First, it sorts nodes by type: text nodes come first, then processing instructions, then elements. Element attributes are sorted first by name, then by value. Elements are sorted first by element name, then by attribute name/value, then by text content, and finally by child nodes using the same criteria. The order of text nodes relative to each other is not changed.  Processing instructions are sorted by their target and data values.

The Custom XML sort allows you to define one or more sort instructions in this tool. If multiple sort instructions are defined, then the XML document will first be sorted according to the first instruction, then sorted again according to the second instruction, and so on. Each sort instruction is intended to sort a group of elements labeled as the “Sort Target,” and these target elements are sorted using the sort key criteria defined within the instruction.

Configuring XML Sort

To configure XML Sort:

  1. Add an XML Sort tool as an output of another tool (such as a SOAP Client or Messaging Client). In order to make the configuration process easier, it is recommended that you either:
  2. Specify which sorting mode you want to use.
  3. Click Add in the XML Sort tool editor. A wizard will launch to help you define the list of elements you want sorted. After your configuration, the Target XPath field should return a node list when evaluated against the XML input of the XML Sort tool. This is the node list that will be sorted.
  4. In the first wizard page, click Modify, then do one of the following.
  5. Click Next in the wizard, then define how the target elements should be sorted (that is, specify the node under the target element that should be used as the sort key). Note that:
  6. Click Finish to complete the wizard.