About Arbortext Styler > Adding Generated Text > Using XPath Expressions in Generated Text
  
Using XPath Expressions in Generated Text
You can use XPath expressions to insert the contents of specific elements, attributes, or entire element node sets in generated text.
You may also embed the output of a script as an XPath string. Please refer to Accessing Scripts from Arbortext Styler for further information.
Using XPath to Insert the Contents of Specific Elements or Attributes
1. Select an element context and choose the Generated text category in the properties area.
2. Choose the Edit button associated with the Before-text or After-text field, depending on the intended placement of your generated text.
3. In the Generated Text Editor, choose Insert > XPath String.
4. In the Insert XPath String dialog box that opens, provide a valid XPath expression to insert the content of a specific element or attribute in generated text. Relative expressions are evaluated relative to the element being styled, except when you are inserting an XPath expression in the generated text of a header or footer, where the document element is used as the context node.
Some examples are shown below:
Content of a specified attribute in a specified element whose parent is a specified element that is the root of the document
The expression /doc/docinfo/@docno will insert the content of the docno attribute of the docinfo element, which is a child of the doc element. In this expression the doc element must be the root of the document.
Content of a specified attribute in a specified element that is a child of the current element
The expression ./docinfo/@docno will insert the content of the docno attribute of the docinfo element. Here the docinfo element must be a child of the element for which the generated text is being set.
You must understand XPath and its syntax to use this option. Refer to the World Wide Web Consortium (W3C) web site (http://www.w3.org/TR/xpath) for information on the XPath standard.
* 
It is not necessary to substitute &lt; for <, &amp; for &, or &quote; for ".
* 
The use of certain XPath expressions in generated text can cause increased processing times. Please refer to XPath Performance for information and suggested alternatives.
Using Arbortext Editor to Test an XPath Expression for Inserting Content
It often takes multiple attempts to define the correct the correct XPath expression to meet your requirements. It is much quicker to develop and test expressions in 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 generated text you are creating.
2. In the Arbortext Editor command line, enter the command eval oid_xpath_string(oid_caret(), "...") - replacing ... with the expression you wish to use.
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.
To Insert an Element-Node Set Using an XPath Expression
1. Select an element context and choose the Generated text category in the properties area.
2. Choose the Edit button associated with the Before-text or After-text field, depending on the intended placement of your generated text.
3. In the Generated Text Editor, choose Insert > Element Content.
4. In the Insert Element Content dialog box that opens, select the By XPath option then provide a valid XPath expression in the Expression field. The expression must result in an element-node set. Relative expressions are evaluated relative to the element being styled, except when you are inserting an XPath expression in the generated text of a header or footer, where the document element is used as the context node.
For example, the expression ancestor::chapter//endnote, when used in the generated text of an element that is the descendant of a chapter, will insert copies of all the chapter's endnote elements into that element.
You must understand XPath and its syntax to use this option. Refer to the World Wide Web Consortium (W3C) web site (http://www.w3.org/TR/xpath) for information on the XPath standard.
* 
It is not necessary to substitute &lt; for <, &amp; for &, or &quote; for ".
Using Arbortext Editor to Test an XPath Expression for Inserting Elements
It often takes multiple attempts to define the correct the correct XPath expression to meet your requirements. It is much quicker to develop and test expressions in 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 generated text you are creating.
2. In the Arbortext Editor command line, enter the following series of commands:
a. global xa[]
b. eval oid_xpath_nodeset(oid_caret(), $xa, "..." - replacing ... with the expression you wish to use. This expression will return the number of elements matched with your original expression.
c. goto_oid($xa[1]) - this expression will take you to the first element matched with your original expression. Repeat the command changing the number in square brackets to identify which match in the list you wish to be taken to, i.e. goto_oid($xa[2]) for the second match, goto_oid($xa[3]) for the third, and so on. Here you can use the number generated with the expression in point 2 to determine how far you need to proceed with this step.
* 
Certain XPath expressions may cause a considerable increase in the time taken to process generated text. Please refer to XPath Performance for information on the types of expression that may be problematic, and some alternative ways in which generated text can be defined.