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 (e.g., 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:
    • Invoke the other tool first so that traffic can be captured in the Traffic Viewer; the response message will help you configure the sorting later (based on the sample message coming back from the service) - OR-
    • If you cannot run it, then try configuring the client with the WSDL, Schema, or request message if possible.
  2. Specify which sorting mode you want to use.
    • To sort all nodes recursively, choose Recursive XML sort. In this case, no additional configuration is needed.
    • To define one or more sort instructions, choose Custom XML sort, then continue following the steps below.
  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.
    • If you performed step 1, the XPath editor will be populated with a sample message to assist you in constructing the XPath visually. In this case, you should select the parent of the nodes you want sorted. You can adjust the generated XPath expression and test it using Evaluate XPath button.
    • If you did not perform step 1, there will be no way of knowing what the XML is expected to look like. In this case, you need to provide the XPath manually.

  5. Click Next in the wizard, then define how the target elements should be sorted (i.e. specify the node under the target element that should be used as the sort key). Note that:
    • By default, an entry for using the target element text content as the key will be added.

    • The default entry can be modified by clicking the Modify button and customizing the key XPath
    • If step 1 was performed, then the XPaths generated by the editor will be relative to the target element. In other words, it will represent an XPath query starting from an individual target node. Absolute XPath expressions can be configured instead. If you do this, ensure that they are based off of the entire XML content root node.
    • Additional key XPaths can be configured to be used as secondary and ternary sorting keys. This means that the target elements will be sorted based on the first key XPath, then elements with matching keys will be sorted based on the second key XPath, and so on.
  6. Click Finish to complete the wizard.
  • No labels