Basic XPath expressions

XPath expressions are used in orchestrations and flat file schema definitions when you need to identify one or more nodes in a message or variable in order to access data or determine whether a condition has been met.

XPath is an XML standard that defines a grammar to identify nodes or sets of nodes (called node sets) in XML documents.

You can edit the default XPath expressions, generated in the following panes:

  • In the Configure panes of the For Each and Split activity activities, an XPath expression is generated to define the recurring element. You can edit the default XPath expression displayed in the Element Name field.
  • In the Configure panes of the While Loop and If..Then activities, an XPath expression is generated in the Expression Builder to define the conditions. You can edit the default XPath expression displayed by clicking Advanced.

XPath 2.0 is supported.

Selecting nodes by path

XPath describes the location of XML elements and attributes as a path similar to URLs or directories on your local file system. Each element or attribute is a node in a tree that begins with the root node. The root node contains the entire tree, so any node within the tree can be found by following a path down the branches.

The basic syntax is:

/root-node/child-node/.../final-element-node

/root-node/child-node/.../final-element-node/@attribute-node

You determine this path from the XML Schema that describes the messages.

XPath uses the @ symbol as a shorthand to identify attributes. Paths without @, or the attribute:: prefix, identify elements by default. For example:

 /PackingSlip/PONumber/Item

identifies the Item element found inside PONumber in the PackingSlip root node.

Node names and XML namespaces

Node names can also appear with namespaces, such as tns:OrderNumber or *:Item. The portion of the name up to the colon is a prefix identifying a namespace and the colon is the delimiter between the namespace and the node's local name.

Namespaces are an XML standard used to uniquely identify elements or attributes when documents share content from several schemas. Cast Iron® Studio automatically assigns and manages namespaces for the XML Schemas used in configuration documents.  

Note: Each schema within a WSDL definition can be defined by a namespace.

Select nodes conditionally with predicates

You might occasionally want to select a node only if some condition is met. These selection conditions are known as predicates.

You define predicates inside brackets, such as:

/PV1/PatientDepartment[text()='Nursery']

This expression selects the PatientDepartment node only if the data in the node is Nursery.

/Order/OrderDetail/Item[5]

This expression selects the fifth Item node within OrderDetail.

Valid operators for conditions

XPath expressions can also be boolean conditions that evaluate to true or false. These expressions can be used to select specific nodes or simply to identify a condition that must be met. You can use the Expression Builder to help you define conditions. XPath operators that can appear in expressions include:

  • = equals
  • != not equals
  • < less than
  • > greater than
  • <= less than or equals
  • >= greater than or equals

Functions in XPath expressions

XPath also contains functions that allow you to manipulate data from nodes. This can be useful in building subject lines that contain some constant text plus dynamic data from messages. XPath functions can also be used to handle requirements such as trimming space or ensuring that a job key is a string datatype. Some of the common XPath functions include:
concat()
you can combine any number of strings and the data from nodes in the order you specify the parameters. You can specify XPath paths as parameters. Specify constant text strings within quote marks. For example:

concat('New Order # ',/Orders/Order/OrdNumber)

count()
you can count the number of nodes in the parameter you specify. Typically the parameter is an XPath path expression that identifies several nodes, such as all the Item nodes that are children of PODetail. For example:

count(/Orders/Order)

normalize-space()
trims all leading and trailing space characters from the input parameter and also normalizes all space within the input to single space characters and single line feeds. For example:

normalize-space(/Job/Address/Line1)

number()
converts the parameter to a number datatype. This function can be used to ensure that strings are treated as numbers. For example:

number(/PurchaseOrder/POTotal)

string()
converts the parameter to a string datatype. This function can be used to ensure that numeric or date nodes are treated as strings. For example:

string(/PurchaseOrder/VendorID)

translate()
replaces individual characters with other characters in the first parameter you specify. The second parameter is the character or characters to replace and the third parameter is the replacement characters. This function can be useful for ensuring that job keys are all uppercase, for example.

translate(/Issue/ShortDescr,

'abcdefghijklmnopqrstuvwxyz',

'ABCDEFGHIJKLMNOPQRSTUVWXYZ')

For a complete list of XPath functions, see the XPath Specification ( http://www.w3.org/TR/xpath).

Testing XPath expressions

You can test your XPath Expressions against XML test data using the XPath Evaluator.




Feedback | Notices


Timestamp icon Last updated: Wednesday, 15 June 2016


http://pic.dhe.ibm.com/infocenter/wci/v7r0m0/topic/com.ibm.wci.doc/ref_Basic_XPath_Expressions.html