XQueryEvaluator qe = entry.getValue().load(); qe.setContextItem(sourceRef.get()); XdmValue result = qe.evaluate();
private String getSchemaTNS(XdmItem schema) throws SaxonApiException { XQueryEvaluator e = processor.newXQueryCompiler().compile("declare namespace xsd = \"http://www.w3.org/2001/XMLSchema\"; xsd:string(xsd:schema/@targetNamespace)").load(); e.setContextItem(schema); XdmValue v = e.evaluate(); if (v instanceof XdmEmptySequence) return null; else return v.toString(); }
Processor saxon = new Processor(false); saxon.registerExtensionFunction(new MyExtension()); XQueryCompiler compiler = saxon.newXQueryCompiler(); XQueryExecutable exec = compiler.compile(new File("input/names.xq")); XQueryEvaluator query = exec.load(); DocumentBuilder builder = saxon.newDocumentBuilder(); String students = "<xml>...</xml>"; Source src = new StreamSource(new StringReader(students)); XdmNode doc = builder.build(src); query.setExternalVariable(new QName("student_list"), doc); XdmValue result = query.evaluate();
// the Saxon processor object Processor saxon = new Processor(false); // compile the query XQueryCompiler compiler = saxon.newXQueryCompiler(); XQueryExecutable exec = compiler.compile(new File("yours.xqy")); // parse the string as a document node DocumentBuilder builder = saxon.newDocumentBuilder(); String input = "<xml>...</xml>"; Source src = new StreamSource(new StringReader(input)); XdmNode doc = builder.build(src); // instantiate the query, bind the input and evaluate XQueryEvaluator query = exec.load(); query.setContextItem(doc); XdmValue result = query.evaluate();
import javax.xml.parsers.DocumentBuilderFactory; import net.sf.saxon.s9api.*; import org.w3c.dom.Document; ... Document merge(Document context, Document other) throws Exception { Processor processor = new Processor(false); XQueryExecutable executable = processor.newXQueryCompiler().compile( "declare variable $other external; element {node-name(*)} {*/*, $other/*/*}"); XQueryEvaluator evaluator = executable.load(); DocumentBuilder db = processor.newDocumentBuilder(); evaluator.setContextItem(db.wrap(context)); evaluator.setExternalVariable(new QName("other"), db.wrap(other)); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); processor.writeXdmValue(evaluator.evaluate(), new DOMDestination(doc)); return doc; }
/** * Evaluates an XQuery 1.0 expression using the Saxon s9api interfaces. * * @param source The XML Source. * @param query The query expression. * @param nsBindings A collection of namespace bindings required to evaluate * the query, where each entry maps a namespace URI (key) to a prefix * (value). * @return An XdmValue object representing a value in the XDM data model. * @throws SaxonApiException If an error occurs while evaluating the query * (this always wraps some other underlying exception). */ public static XdmValue evaluateXQuery(Source source, String query, Map<String, String> nsBindings) throws SaxonApiException { Processor proc = new Processor(false); XQueryCompiler xqCompiler = proc.newXQueryCompiler(); if (null != nsBindings) { for (String nsURI : nsBindings.keySet()) { xqCompiler.declareNamespace(nsBindings.get(nsURI), nsURI); } } XQueryExecutable xqExec = xqCompiler.compile(query); XQueryEvaluator xqEval = xqExec.load(); xqEval.setSource(source); return xqEval.evaluate(); }
XdmValue result = query.evaluate(); System.out.println(result.itemAt(0));
/** * Evaluates an XQuery 1.0 expression using the Saxon s9api interfaces. * * @param source * The XML Source. * @param query * The query expression. * @param nsBindings * A collection of namespace bindings required to evaluate the * query, where each entry maps a namespace URI (key) to a prefix * (value). * @return An XdmValue object representing a value in the XDM data model. * @throws SaxonApiException * If an error occurs while evaluating the query (this always * wraps some other underlying exception). */ public static XdmValue evaluateXQuery(Source source, String query, Map<String, String> nsBindings) throws SaxonApiException { Processor proc = new Processor(false); XQueryCompiler xqCompiler = proc.newXQueryCompiler(); if (null != nsBindings) { for (String nsURI : nsBindings.keySet()) { xqCompiler.declareNamespace(nsBindings.get(nsURI), nsURI); } } XQueryExecutable xqExec = xqCompiler.compile(query); XQueryEvaluator xqEval = xqExec.load(); xqEval.setSource(source); return xqEval.evaluate(); }
@Override public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException { String query = arguments[0].head().getStringValue(); Evaluator eval = SearchBase.getEvaluator(context); XQueryCompiler xqueryCompiler = eval.getCompiler().getXQueryCompiler(); xqueryCompiler.setErrorListener(eval.getErrorListener()); try { // TODO: cache compiled xslt somewhere XQueryExecutable xqueryExec= xqueryCompiler.compile(query); evaluator = xqueryExec.load(); evaluator.setErrorListener(eval.getErrorListener()); if (arguments.length > 1) { bindParameters(arguments[1]); } XdmValue result = evaluator.evaluate(); ArrayList<TransformerException> runtimeErrors = eval.getErrorListener().getErrors(); if (!runtimeErrors.isEmpty()) { throw new XPathException(runtimeErrors.get(0).getMessage(), runtimeErrors.get(0).getLocator(), runtimeErrors.get(0)); } return result.getUnderlyingValue(); } catch (SaxonApiException e) { throw new XPathException (e); } }
XQueryEvaluator qe = entry.getValue().load(); qe.setContextItem(sourceRef.get()); XdmValue result = qe.evaluate();
XdmValue value = xqueryEvaluator.evaluate(); return new XdmResultSet (value); } catch (SaxonApiException e) {
xdmValue = queryEvaluator.evaluate();