Project

Profile

Help

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

he / tags / 9.6.0.7 / samples / cs / XPathExample.cs @ c74fd4aa

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
  * @author Michael H. Kay (Michael.H.Kay@ntlworld.com)
15
  * @version October 2004: rewritten in C# to use the .NET API
16
  */
17

    
18
public class XPathExample {
19

    
20
    private String currentWord;
21

    
22
    /**
23
      * main()<BR>
24
      * Expects one argument, the input filename<BR>
25
      */
26

    
27
    public static void Main(String[] args) {
28
        // Check the command-line arguments
29

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

    
38
    /**
39
    * Run the application
40
    */
41

    
42
    public void go(String filename) {
43

    
44
        Processor processor = new Processor();
45
        XPathCompiler xpe = processor.NewXPathCompiler();
46

    
47
        // Build the source document. 
48

    
49
        DocumentBuilder builder = processor.NewDocumentBuilder();
50
        builder.BaseUri = new Uri(filename);
51
        builder.WhitespacePolicy = WhitespacePolicy.StripAll;
52
        XdmNode indoc = builder.Build(
53
                new FileStream(filename, FileMode.Open, FileAccess.Read));
54

    
55

    
56
        // Compile the XPath expressions used by the application
57

    
58
        QName wordName = new QName("", "", "word");
59
        xpe.DeclareVariable(wordName);
60

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

    
68

    
69
        // Loop until the user enters "." to end the application
70

    
71
        while (true) {
72

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

    
76
            // Read the input
77
            String word = Console.ReadLine().Trim();
78
            if (word == ".") {
79
                break;
80
            }
81
            if (word != "") {
82

    
83
                // Set the value of the XPath variable
84
                currentWord = word;
85

    
86
                // Find the lines containing the requested word
87
                bool found = false;
88
                findLine.ContextItem = indoc;
89
                findLine.SetVariable(wordName, new XdmAtomicValue(word));
90
                foreach (XdmNode line in findLine) {
91

    
92
                    // Note that we have found at least one line
93
                    found = true;
94

    
95
                    // Find where it appears in the play
96
                    findLocation.ContextItem = line;
97
                    Console.WriteLine("\n" + findLocation.EvaluateSingle());
98

    
99
                    // Output the name of the speaker and the content of the line
100
                    findSpeaker.ContextItem = line;
101
                    Console.WriteLine(findSpeaker.EvaluateSingle() + ":  " + line.StringValue);
102

    
103
                }
104

    
105
                // If no lines were found, say so
106
                if (!found) {
107
                    Console.WriteLine("No lines were found containing the word '" + word + '\'');
108
                }
109
            }
110
        }
111

    
112
        // Finish when the user enters "."
113
        Console.WriteLine("Finished.");
114
    }
115

    
116
    /**
117
     * This class serves as a variable resolver. The only variable used is $word.
118
     * @param qName the name of the variable required
119
     * @return the current value of the variable
120
     */
121

    
122
    public Object resolveVariable(QName qName) {
123
        if (qName.LocalName == "word") {
124
            return currentWord;
125
        } else {
126
            return null;
127
        }
128
    }
129

    
130
}
(3-3/5)