Using XPath

The Input tab uses XPath as the language for locating and manipulating data. XPath (XML Path Language) is an expression language developed by the World Wide Web Consortium (W3C) for addressing parts of XML documents.

XPath also provides basic manipulation functions for strings, numbers, and booleans. To use XPath in the Input tab, you need only be familiar with the basic XPath concepts, but you might want to learn more about XPath when building complex expressions. For a complete description of XPath, refer to the XPath specification (which can be obtained from www.w3.org).

Addressing Schema Components

All data in the context and message panel is represented as an XML schema. The data can be a simple (strings, numbers, booleans, and so on) or a complex component. Complex components are structures that contain other schema components, either simple components or other complex components. Both simple and complex components can also repeat. That is, they can be lists that store more than one component of the given type.

XPath is used to specify which schema component you would like to refer to. For example, this schema may be available in the context panel.

The context panel of the example shows the schema available for a mediation operation.

Three top-level items, each a root node in the context panel, are present:

  • MediationFlowProperties
  • MessageContext
  • and MessageData

Each of these nodes has its own associated structure. MediationFlowProperties has a complex component named properties and MesageData has a complex component named searchHotelRequest.

References to a particular data item in any of these schema start with the root node and slashes (/) indicate a path to the data component. For example, the country element in the SearchHotel complex component that is in the paramenters component would look like this in an XPath mapping field:

$MessageData/searchHotelRequest/parameters/searchHotel/country

The path starts with a dollar sign, then continues with node names using slashes, like a file or directory structure, until the location is named.

Some schema components must be prefixed with their namespace prefix. The prefix is automatically added to components that require this when dragging and dropping data in the XPath Expression field.

Evaluation Context

XPath also has a method for referencing relative paths from a particular node. If you have an evaluation context, or a particular starting node in a schema tree, you can specify the relative path to other elements in the tree.

For example, if your evaluation context is $MessageData/searchHotelRequest/parameters/searchHotel, you can reference the sub-items of ShipName without specifying the entire path. If you want to reference $MessageData/searchHotelRequest/parameters/searchHotel/country, the relative path would be ./country. The path is relative to the evaluation context — country is at the same level in the schema tree as the evaluation context.

Search Predicates

An XPath expression can have a search predicate. The search predicate is used to locate a specific element of a repeating schema item. For example, consider a schema where the $MessageData/searchReservations/todaysReservations item is a repeating element. If you want to select only the first item in the repeating element, you would specify this:

$MessageData/searchReservations/todaysReservations[1]

The [1] specifies the first element of a repeating item.

Sub-items can also be examined and used in a search predicate. For example, to select the element whose reservationId is equal to "3A54", you would specify:

$MessageData/searchReservations/todaysReservations[reservationId= "3A54"]

In the example above, the evaluation context of a predicate is set to the item containing the predicate. Therefore, reservationId is assumed to be within the todaysReservations component.

You can also use functions and expressions in the search predicate. For example, if you want to find all elements after the first, you would specify:

$MessageData/searchReservations/todaysReservations[position()>1]

Testing Boolean Values

To test the value of a boolean node, you can use the data() function to obtain the value of the node. A common error in XPath functions is to supply a boolean node in a condition and expect that the condition will evaluate to true or false based on the value in the node. For example:

if ($MessgeData/searchHotelRequest/parameters/searchHotel/nonSmoking) then ...

The condition in the if statement above would return true when the nonSmoking component is present, regardless of whether the value of the component is true or false. To evaluate the value of a boolean element, use this expression:

if (data($MessageData/searchHotelRequest/parameters/searchHotel/nonSmoking)) then ...

You can also use the string() function to coerce the comparison to the string value of the Boolean node and then compare to the value of "true" or "false". For example:

string($MessageData/searchHotelRequest/parameters/searchHotel/ nonSmoking) = "true"

Comments

You can add comments to XPath expressions using the XPath 2.0 syntax for comments. The syntax is:

{-- <comment here> --}

For example, this XPath expression contains a comment:

$MessageData/searchHotelRequest/parameters/searchHotel/country {-- returns the country --}