Project

Profile

Help

How to connect?
Download (3.71 KB) Statistics
| Branch: | Revision:

he / src / samples / cs / XPathExample.cs @ d1236b02

1
using System;
2
using System.IO;
3
using System.Collections;
4
using System.Xml;
5
using Saxon.Api;
6

    
7
/**
8
  * Class XPathExample:
9
  * This class illustrates the use of the JAXP XPath API. It is a simple command-line application,
10
  * which prompts the user for a word, and replies with a list of all the lines containing that
11
  * word within a Shakespeare play.
12
  */
13

    
14
public class XPathExample {
15

    
16
    private String currentWord;
17

    
18
    /**
19
      * main()<BR>
20
      * Expects one argument, the input filename<BR>
21
      */
22

    
23
    public static void Main(String[] args) {
24
        // Check the command-line arguments
25

    
26
        if (args.Length != 1) {
27
            Console.WriteLine("Usage: XPathExample input-file");
28
            return;
29
        }
30
        XPathExample app = new XPathExample();
31
        app.go(args[0]);
32
    }
33

    
34
    /**
35
    * Run the application
36
    */
37

    
38
    public void go(String filename) {
39

    
40
        Processor processor = new Processor();
41
        XPathCompiler xpe = processor.NewXPathCompiler();
42

    
43
        // Build the source document. 
44

    
45
        DocumentBuilder builder = processor.NewDocumentBuilder();
46
        builder.BaseUri = new Uri(filename);
47
        builder.WhitespacePolicy = WhitespacePolicy.StripAll;
48
        XdmNode indoc = builder.Build(
49
                new FileStream(filename, FileMode.Open, FileAccess.Read));
50

    
51

    
52
        // Compile the XPath expressions used by the application
53

    
54
        QName wordName = new QName("", "", "word");
55
        xpe.DeclareVariable(wordName);
56

    
57
        XPathSelector findLine =
58
            xpe.Compile("//LINE[contains(., $word)]").Load();
59
        XPathSelector findLocation =
60
            xpe.Compile("concat(ancestor::ACT/TITLE, ' ', ancestor::SCENE/TITLE)").Load();
61
        XPathSelector findSpeaker =
62
            xpe.Compile("string(ancestor::SPEECH/SPEAKER[1])").Load();
63

    
64

    
65
        // Loop until the user enters "." to end the application
66

    
67
        while (true) {
68

    
69
            // Prompt for input
70
            Console.WriteLine("\n>>>> Enter a word to search for, or '.' to quit:\n");
71

    
72
            // Read the input
73
            String word = Console.ReadLine().Trim();
74
            if (word == ".") {
75
                break;
76
            }
77
            if (word != "") {
78

    
79
                // Set the value of the XPath variable
80
                currentWord = word;
81

    
82
                // Find the lines containing the requested word
83
                bool found = false;
84
                findLine.ContextItem = indoc;
85
                findLine.SetVariable(wordName, new XdmAtomicValue(word));
86
                foreach (XdmNode line in findLine) {
87

    
88
                    // Note that we have found at least one line
89
                    found = true;
90

    
91
                    // Find where it appears in the play
92
                    findLocation.ContextItem = line;
93
                    Console.WriteLine("\n" + findLocation.EvaluateSingle());
94

    
95
                    // Output the name of the speaker and the content of the line
96
                    findSpeaker.ContextItem = line;
97
                    Console.WriteLine(findSpeaker.EvaluateSingle() + ":  " + line.StringValue);
98

    
99
                }
100

    
101
                // If no lines were found, say so
102
                if (!found) {
103
                    Console.WriteLine("No lines were found containing the word '" + word + '\'');
104
                }
105
            }
106
        }
107

    
108
        // Finish when the user enters "."
109
        Console.WriteLine("Finished.");
110
    }
111

    
112
    /**
113
     * This class serves as a variable resolver. The only variable used is $word.
114
     * @param qName the name of the variable required
115
     * @return the current value of the variable
116
     */
117

    
118
    public Object resolveVariable(QName qName) {
119
        if (qName.LocalName == "word") {
120
            return currentWord;
121
        } else {
122
            return null;
123
        }
124
    }
125

    
126
}
(5-5/5)