try { XQueryEvaluator qe = entry.getValue().load(); qe.setContextItem(sourceRef.get()); XdmValue result = qe.evaluate();
/** * Set the source document for the query. This method is equivalent to building * a document from the supplied source object, and then supplying the document node of * the resulting document as the initial context node. * @param source the principal source document for the transformation */ public void setSource(Source source) throws SaxonApiException { setContextItem(processor.newDocumentBuilder().build(source)); }
/** * Set the source document for the query. * <p>If the source is an instance of {@link net.sf.saxon.om.NodeInfo}, the supplied node is used * directly as the context node of the query.</p> * <p>If the source is an instance of {@link javax.xml.transform.dom.DOMSource}, the DOM node identified * by the DOMSource is wrapped as a Saxon node, and this is then used as the context item</p> * <p>In all other cases a new Saxon tree is built, by calling * {@link net.sf.saxon.s9api.DocumentBuilder#build(javax.xml.transform.Source)}, and the document * node of this tree is then used as the context item for the query.</p> * * @param source the source document to act as the initial context item for the query. */ public void setSource(Source source) throws SaxonApiException { if (source instanceof NodeInfo) { setContextItem(new XdmNode((NodeInfo) source)); } else if (source instanceof DOMSource) { setContextItem(processor.newDocumentBuilder().wrap(source)); } else { setContextItem(processor.newDocumentBuilder().build(source)); } }
/** * Set the source document for the query. * <p>If the source is an instance of {@link net.sf.saxon.om.NodeInfo}, the supplied node is used * directly as the context node of the query.</p> * <p>If the source is an instance of {@link javax.xml.transform.dom.DOMSource}, the DOM node identified * by the DOMSource is wrapped as a Saxon node, and this is then used as the context item</p> * <p>In all other cases a new Saxon tree is built, by calling * {@link net.sf.saxon.s9api.DocumentBuilder#build(javax.xml.transform.Source)}, and the document * node of this tree is then used as the context item for the query.</p> * * @param source the source document to act as the initial context item for the query. */ public void setSource(Source source) throws SaxonApiException { if (source instanceof NodeInfo) { setContextItem(new XdmNode((NodeInfo) source)); } else if (source instanceof DOMSource) { setContextItem(processor.newDocumentBuilder().wrap(source)); } else { setContextItem(processor.newDocumentBuilder().build(source)); } }
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(); }
// 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; }
private XQueryEvaluator prepareEvaluation(QueryContext context, TransformErrorListener listener, XQueryExecutable xquery) { listener.setUserData(this); this.queryContext = context; XQueryEvaluator xqueryEvaluator = xquery.load(); xqueryEvaluator.setErrorListener(listener); if (context != null) { xqueryEvaluator.setContextItem((XdmItem) context.getContextItem()); if (context.getVariableBindings() != null) { for (Map.Entry<QName, Object> binding : context.getVariableBindings().entrySet()) { net.sf.saxon.s9api.QName saxonQName = new net.sf.saxon.s9api.QName(binding.getKey()); xqueryEvaluator.setExternalVariable(saxonQName, (XdmValue) binding.getValue()); } } } return xqueryEvaluator; }
query.setContextItem(doc); XdmValue result = query.evaluate(); System.out.println(result.itemAt(0));
LOG.trace("XQuery input document: {}", document); XQueryEvaluator evaluator = fragment.xQueryEvaluator; evaluator.setContextItem(document);
LOG.trace("XQuery input document: {}", document); XQueryEvaluator evaluator = fragment.xQueryEvaluator; evaluator.setContextItem(document);
XQueryExecutable xqexec = xqcomp.compile(queryexpr); XQueryEvaluator xqeval = xqexec.load(); xqeval.setContextItem(srcDoc);
XQueryExecutable xqexec = xqcomp.compile(queryexpr); XQueryEvaluator xqeval = xqexec.load(); xqeval.setContextItem(srcDoc);
listener.setUserData(this); xqueryEvaluator.setErrorListener(listener); xqueryEvaluator.setContextItem((XdmItem) context.getContextItem()); if (context.getVariableBindings() != null) { for (Map.Entry<QName, Object> binding : context.getVariableBindings().entrySet()) {
evaluator.setContextItem(doc);
evaluator.setContextItem(doc);
xqeval.setContextItem(node); xqeval.run();
xqeval.setContextItem(node); xqeval.run();
XQueryEvaluator xqeval = xqexec.load(); if (document != null) { xqeval.setContextItem(document);
XQueryEvaluator xqeval = xqexec.load(); if (document != null) { xqeval.setContextItem(document);