333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Variable Resolvers
 
The methods on the [XPathEvaluator](#) type can optionally take a variable resolver to resolve 
variable references in the XPath expression being evaluated.
 
There are three ways to specify a variable resolver and you can use any one of them depending on which is 
most suited to your particular situation.
 
Note that if your variables are in a namespace (e.g. `$myVars:var`), you must use the second or third 
type as the plain object implementation does not support namespaces.
 
## Variable values
 
You can use any of five types of values to specify the values of variables:
 
- string
- number
- boolean
- single node (will be treated as a node set)
- array of nodes or array-like collection of nodes (will be treated as a node set)
 
## Variable Resolver Type 1: Plain object
 
A plain object with variable names as the keys and variable values as the values.
 
Example usage:
 
````
var evaluator = xpath.parse('concat($character1, ", ", $character2, ", and ", $character3)');
var mainCharacters = evaluator.evaluateString({
    variables: {
        character1: 'Harry',
        character2: 'Ron',
        character3: 'Hermione'
    }
});
````
 
## Variable Resolver Type 2: Function
 
A function that takes a variable name as its first parameter and an optional namespace URI as its second parameter 
and returns a value based on the name and namespace.
 
Example usage:
 
````
var evaluator = xpath.parse('concat($hp:character1, ", ", $hp:character2, ", and ", $hp:character3)');
var mainCharacters = evaluator.evaluateString({
    variables: function (name, namespace) {
        if (namespace === 'http://sample.org/harrypotter/') {
            switch (name) {
                case 'character1': return 'Harry';
                case 'character2': return 'Ron';
                case 'character3': return 'Hermione';
            }
        }
    },
    namespaces: {
        hp: 'http://sample.org/harrypotter/'
    }
});
````
 
## Function Resolver Type 3: Object with `getFunction` method
 
An object with a method named `getVariable` that works in the same way as the function-based variable resolver 
described above.
 
Example usage:
 
````
var evaluator = xpath.parse('concat($hp:character1, ", ", $hp:character2, ", and ", $hp:character3)');
var mainCharacters = evaluator.evaluateString({
    variables: {
        getVariable: function (name, namespace) {
            if (namespace === 'http://sample.org/harrypotter/') {
                switch (name) {
                    case 'character1': return 'Harry';
                    case 'character2': return 'Ron';
                    case 'character3': return 'Hermione';
                }
            }
        }
    },
    namespaces: {
        hp: 'http://sample.org/harrypotter/'
    }
});
````