About Arbortext Styler > Creating Conditions > Using XPath in Conditions
  
Using XPath in Conditions
XPath expressions can be used as tests when creating conditions. When a document is processed the expression is evaluated and the result converted to a boolean value. If that value is true, the condition is considered to match. If that value is false, the condition does not match.
Some examples of using expressions in this way are detailed below. The assumption with these examples is that each condition includes a single test, based on the XPath expression shown:
Element with a specified ancestor
The expression ancestor::doc will return true for an element that has an ancestor element named <doc>.
Element with a specified ancestor, where that ancestor contains a specified child
The expression ancestor::doc/docinfo will return true for an element that has ancestor element named <doc>, where that ancestor has a child element named <docinfo>.
Element with a specified ancestor, where that ancestor contains a specified child and that child includes a specified attribute
The expression ancestor::doc/docinfo@lang will return true for an element that has an ancestor element named <doc> where that ancestor has a child element named <docinfo>. The specified child contains the attribute lang.
Element within a specified ancestor, where that ancestor contains a specified child and that child includes a specified attribute with a specified value
The expression ancestor::doc/docinfo@lang=”es” will return true for an element that has an ancestor element named <doc> where that ancestor has a child element named <docinfo>. The specified child contains the attribute lang, which has been given the value “es”.
Element that contains text
The expression text() will return true for an element that contains text.
You may also use XPath to create a test that will evaluate the result of an ACL or a JavaScript script.
To add an XPath expression as a condition, select New XPath Test in the Condition dialog box, and type the expression in the resulting field.
* 
Expressions that contain position() or last() cannot be tested in this way. Expressions of this type are not permitted inside XPath tests of conditions with FOSI outputs in Arbortext Styler. Use the preceding-sibling or following-sibling axes instead to create comparable tests.
For example:
The expression count(preceding-sibling::para)=1, when used as a condition of the para element, will test that the para is the second para in its parent
The expression count(following-sibling::para)=0 is a viable alternative to position()=last()
The use of certain XPath expressions can cause increased processing times. Please refer to XPath Performance for information and suggested alternatives.
Using Arbortext Editor to Test an XPath Expression
It often takes multiple attempts to define the correct XPath expression to meet your requirements. It is much quicker to develop and test expressions Arbortext Editor than in Arbortext Styler - the steps below explain how:
1. Open your document in Arbortext Editor and place the cursor inside the element whose context will contain the condition.
2. In the Arbortext Editor command line, enter the command eval oid_xpath_boolean(oid_caret(), "self::node()[...]") - replacing ... with the expression you wish to use for the XPath test.
3. Look at the resulting value in the Eval Output window - if the value is 1, the test is true. If a value of 0 returned, the test is false.