-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunXQuery.java
More file actions
55 lines (52 loc) · 2.25 KB
/
Copy pathRunXQuery.java
File metadata and controls
55 lines (52 loc) · 2.25 KB
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
// Run an XQuery against FundsXML via the Saxon s9api (native Java, no JAXB).
//
// Standalone & cross-platform — run from the repo root with the Maven Wrapper:
// ./mvnw -q -pl XQuery_Examples/invocation compile exec:java \
// -Dexec.args="<query.xq> <input.xml> [k=v ...]"
// Exit: 0 success, 2 setup error.
//
// Dependencies (from Maven Central, see pom.xml): Saxon-HE + xmlresolver
// (+ its -data artifact); no other libraries.
//
// This is a generic, FundsXML-agnostic XQuery runner: it binds the input as
// the context item and passes string external variables — the FundsXML
// specifics (no XML namespace, Position↔Asset by UniqueID) live in the .xq
// files it runs. s9api is Saxon's idiomatic Java API; no JAXB.
//
// External query variables are passed as k=v args (string-typed), e.g. n=5.
import java.io.File;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XQueryCompiler;
import net.sf.saxon.s9api.XQueryEvaluator;
import net.sf.saxon.s9api.XQueryExecutable;
import net.sf.saxon.s9api.XdmAtomicValue;
public class RunXQuery {
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("usage: RunXQuery <query.xq> <input.xml> [k=v ...]");
System.exit(2);
}
try {
Processor proc = new Processor(false);
XQueryCompiler comp = proc.newXQueryCompiler();
XQueryExecutable exe = comp.compile(new File(args[0]));
XQueryEvaluator qe = exe.load();
qe.setSource(new javax.xml.transform.stream.StreamSource(new File(args[1])));
for (int i = 2; i < args.length; i++) {
int eq = args[i].indexOf('=');
if (eq > 0) {
qe.setExternalVariable(new QName(args[i].substring(0, eq)),
new XdmAtomicValue(args[i].substring(eq + 1)));
}
}
Serializer out = proc.newSerializer(System.out);
out.setOutputProperty(Serializer.Property.INDENT, "yes");
qe.run(out);
} catch (Exception e) {
System.err.println("error: " + e.getMessage());
System.exit(2);
}
}
}