This section covers XPath syntax and operators. It is designed as a reference guide for localization or development engineers who wish to use advanced XPath expressions in their ezParse rules.

An XPath expression is a string consisting of a number of navigation steps, joined together by the '/' character.

In all examples below, the quote marks should not be included in the expression.

Child nodes

To navigate to a child node from its parent, use the name of the node.  In cases where a parent has more than one child with the same name, it is advisable to use conditional statements within your XPath expression (see below).

Attributes

To navigate to an attribute of the current node, use '@attributeName', i.e., the name of the attribute preceded by the '@' symbol.  As XML nodes cannot have more than one attribute of the same name, this will always be unique.

Parent

To navigate to the parent of the current node, use '..'  (two consecutive full stops).

Current node

The symbol for the current node is '.'  (one full stop).

Root node

If the first character in the expression is a '/', this indicates that evaluation should begin at the root node of the XML document, not at the starting node.

Conditional steps

Each navigation step can have a condition placed after it in square brackets.  For instance, this can be useful when a node has several 'TranslationString' child nodes in different languages, and you wish to select the one with attribute lang="en".

The expression you would use to accomplish this is:

TranslationString[@lang="en"]

Note that this does not select the 'lang' attribute itself; the part within the square brackets is only accessed for the purpose of evaluating whether the condition is satisfied.

If you wished to select the lang attribute itself, you would use:

TranslationString/@lang[.="en"]

An abbreviated syntax exists for the latter operation.  If the conditional statement is testing the value of the current node (represented by '.'), then we can more simply write:

TranslationString/@lang="en"

Not operator

This operator can be used in conditional statements (within square brackets) only.  It reverses the boolean value of a conditional statement.  It uses the syntax 'not(expression)'.

E.g. source[not(@lang)]

Selects all of the node's children which are named 'source' which do not have an attribute named 'lang'.

Or operator

This operator can be used in conditional statements to create or conditionals within an expression. For example :-

                @translate="yes" | @translate="YES"

will check if the element translate is equal to yes or is equal to YES. If either of these two statements are true, the overall condition is true also.