// Build the source document. This is outside the scope of the XPath API, and
// is therefore Saxon-specific.
InputSource is = new InputSource(new File(filename).toURL().toString());
SAXSource ss = new SAXSource(is);
NodeInfo doc = ((XPathEvaluator)xpe).setSource(ss);
// Declare a variable resolver to return the value of variables used in XPath expressions
xpe.setXPathVariableResolver(this);
// Compile the XPath expressions used by the application
//xpe.setNamespaceContext(this);
XPathExpression findLine =
xpe.compile("//LINE[contains(., $word)]");
XPathExpression findLocation =
xpe.compile("concat(ancestor::ACT/TITLE, ' ', ancestor::SCENE/TITLE)");
XPathExpression findSpeaker =
xpe.compile("string(ancestor::SPEECH/SPEAKER[1])");
// Create a reader for reading input from the console
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Loop until the user enters "." to end the application
while (true) {
// Prompt for input
System.out.println("\n>>>> Enter a word to search for, or '.' to quit:\n");
// Read the input
String word = in.readLine().trim();
if (word.equals(".")) {
break;
}
if (!word.equals("")) {
// Set the value of the XPath variable
currentWord = word;
// Find the lines containing the requested word
List matchedLines = (List)findLine.evaluate(doc, XPathConstants.NODESET);
// Process these lines
boolean found = false;
if (matchedLines != null) {
for (Iterator iter = matchedLines.iterator(); iter.hasNext();) {
// Note that we have found at least one line
found = true;
// Get the next matching line
NodeInfo line = (NodeInfo)iter.next();
// Find where it appears in the play
System.out.println('\n' + findLocation.evaluate(line));
// Output the name of the speaker and the content of the line
System.out.println(findSpeaker.evaluate(line) + ": " + line.getStringValue());
}
}
// If no lines were found, say so
if (!found) {