In APP JS variables can be initialised 'global' and reused later with things like:
var global = {};
or
template.globalVar1 = 'foo';
But for XPath tests when NOT formatting you can use something like this in JS:
var stream = template.content.getStream('mainText');
//'mainText' would be the standard editorDOM from styler i'd guess
var tree = stream.xmlDocument;
var result = tree.evaluateXpath(stuff...);
But presumably your coding this from Styler and source edits not APP GUI?
Regards
Chris