PTC ALD in Arbortext Styler > Components of Documents and Templates > Conditional Text Formatting > Conditional Formatting > Content and Context Tests with XPath
  
Content and Context Tests with XPath
Using XPath to Test Content
XPath can be used to test for items which cannot be tested using the context conditions available through PTC Advanced Print Publisher or PTC Advanced Print Publisher’s context control streams. For example, it is sometimes useful to use XPath to test for certain text content in different contexts.
The fFormatting.evaluateXPath() method is the basis for using XPath in PTC Advanced Print Publisher through JavaScript. This method returns the string result of an XPath expression.
The fxDOM object fxNode, PTC Advanced Print Publisher’s extension to the DOM Level 2 Node object, also includes an evaluateXPath() method. This method allows you to evaluate XPath expressions relative to that node. The result is returned in the form of an fXPathResult object.
Testing Context with XPath
XPath is particularly useful for testing the context of the current element to drive formatting. Using JavaScript, this can be used within a simple switch statement. For example:
var s = new fStyle;
var parent = formatting.evaluateXPath('local-name(..)');

switch (parent) {
case "chapter":
s.height = "25pt";
break;
case "figure":
s.height = "10pt";
break;
case "section":
s.height = "18pt";
s.color = "red";
break;
default:
s.height = "12pt";
break;
}
Outputting the Result of XPath Expressions
The fFormatting.write() method allows you to display the result of the XPath expression inline. For example, this sample code displays the count of para elements at the current hierarchical level:
formatting.write(formatting.evaluateXPath('count(../para)'));