Copyright © TIBCO Software Inc. All Rights Reserved


Chapter 20 XPath Formula Builder : XPath Basics

XPath Basics
XPath (XML Path Language) is an expression language developed by the World Wide Web Consortium (W3C) for addressing parts of XML documents. XPath also has basic manipulation functions for strings, numbers, and Boolean values.
To use XPath in TIBCO BusinessEvents, you need only be familiar with the basic XPath concepts, but you may wish 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).
TIBCO BusinessEvents uses XPath (XML Path Language) to identify elements whose content may be used, for, example in an event payload. You can also use XPath to perform basic manipulation and comparison of strings, numbers, and Boolean values.
Addressing Schema Elements
All Scope Variables and Function arguments are represented as an XML schema. Regardless of where the data comes from or its format, TIBCO BusinessEvents represents the data as a schema tree. The data can be simple (strings, numbers, Boolean values, and so on), or it can be a complex element. Complex elements are structures that contain other schema elements, either simple elements or other complex elements. Both simple and complex elements can also repeat. That is, they can be lists that store more than one element of the given type.
XPath is used to specify which schema element you would like to refer to. Each schema has its own associated structure, for example, a set of simple values or simple data and other complex data.
To reference a particular data item in a schema, you start with the root node and then use slashes (/) to indicate a path to the desired data element. For example, if you wish to specify the Street attribute in the ShipName complex element that is in the GetOrderInformation node, you would use the following syntax:
 
$GetOrderInformation/ShipName/Street
The path starts with a dollar sign to indicate it begins with a root node, then continues with node names using slashes, like a file or directory structure, until the desired location is named.
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 $GetOrderInformation/ShipName, then you can reference the sub-items of ShipName without specifying the entire path. If you wish to reference $GetOrderInformation/RequiredDate, the relative path would be ../RequiredDate. The path is relative to the evaluation context — RequiredDate is one level higher in the schema tree than the elements of ShipName.
Namespaces
Some schema elements must be prefixed with their namespace. The namespace is automatically added to elements that require this when creating mappings or when dragging and dropping data in the XPath formula builder.
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, a $GetOrderInformation/OrderDetails/OrderItem item is a repeating element. If you wish to select only the first item in the repeating element, you would specify the following:
 
$GetOrderInformation/OrderDetails/OrderItem[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 ProductId is equal to "3A54", you would specify the following:
 
$GetOrderInformation/OrderDetails/OrderItem[ProductId="3A54"]
You can also use functions and expressions in the search predicate. For example, if you wish to find all elements after the first, you would specify the following:
 
$GetOrderInformation/OrderDetails/OrderItem[position()>1]
See the Functions tab of the XPath Formula Builder for a list of available functions available and online documentation.
Testing for Nil
Some elements can be explicitly set to nil. You can test an element to determine if it is set to nil or not. For example, the following XPath expression returns true if the $Order/Item/OnSale element is set to nil:
 
$Order/Item/OnSale/@xsi:nil="true"
Comments
You can add comments to XPath expressions using the XPath 2.0 syntax for comments. The syntax is:
 
{-- <comment here> --}
For example, the following XPath expression contains a comment:
$GetOrderInformation/ShipName/Street {-- returns the street --}

Copyright © TIBCO Software Inc. All Rights Reserved