/** * Set the context item for evaluating the expression to be a node. If this method is not called, * the context node will be undefined. The context node is available as the value of * the expression ".". * To obtain a NodeInfo by parsing a source document, see the method * {@link net.sf.saxon.Configuration#buildDocument buildDocument} * in class QueryProcessor. * * @param node The node that is to be the context node for the query * @since 8.4 * @deprecated From Saxon 8.7, the method {@link #setContextItem(Item)} is preferred */ public void setContextNode(NodeInfo node) { if (node==null) { throw new NullPointerException("Context node cannot be null"); } setContextItem(node); }
/** * Set the context item for evaluating the expression to be a node. If this method is not called, * the context node will be undefined. The context node is available as the value of * the expression ".". * To obtain a NodeInfo by parsing a source document, see the method * {@link net.sf.saxon.Configuration#buildDocument buildDocument} * in class QueryProcessor. * * @param node The node that is to be the context node for the query * @since 8.4 * @deprecated From Saxon 8.7, the method {@link #setContextItem(Item)} is preferred */ public void setContextNode(NodeInfo node) { if (node==null) { throw new NullPointerException("Context node cannot be null"); } setContextItem(node); }
/** * Set the initial context item for the query * @param item the initial context item, or null if there is to be no initial context item */ public void setContextItem(XdmItem item) { context.setContextItem(item == null ? null : (Item)item.getUnderlyingValue()); }
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import net.sf.saxon.Configuration; import net.sf.saxon.om.DocumentInfo; import net.sf.saxon.query.DynamicQueryContext; import net.sf.saxon.query.StaticQueryContext; import net.sf.saxon.query.XQueryExpression; import org.xml.sax.InputSource; // inside a method Configuration config = new Configuration(); StaticQueryContext sqc = config.newStaticQueryContext(); DynamicQueryContext dqc = new DynamicQueryContext(config); String xq = "XQUERY_EXPRESSION"; try (InputStream xmlFileInput = new FileInputStream("data.xml"); OutputStream xmlFileOutput = new FileOutputStream("data-filtered.xml")) { XQueryExpression expression = sqc.compileQuery(xq); SAXSource source = new SAXSource(new InputSource(xmlFileInput)); DocumentInfo di = config.buildDocument(source); dqc.setContextItem(di); expression.run(dqc, new StreamResult(xmlFileOutput), null); } catch (Exception e) { System.err.println(e.getMessage()); }
@Override public List<?> evaluate(XmlObject xmlObjectContextNode, String xqueryString) throws XQueryException { LOG.finest(String.format("Executing XQuery query '%s'.", xqueryString)); XQueryExpression exp; try { exp = staticQueryContext.compileQuery(xqueryString); DynamicQueryContext dynamicContext = new DynamicQueryContext(configuration); dynamicContext.setContextItem(new XmlObjectNodeInfo(xmlObjectContextNode, configuration)); List<?> result = exp.evaluate(dynamicContext); LOG.finest(String.format("Executed XQuery expression successfully.")); return result; } catch (XPathException xpe) { throw new XQueryException( String.format("Error while evaluating XQuery expression '%s'.", xqueryString), xpe); } }
exp = staticQueryContext.compileQuery(xqueryString); DynamicQueryContext dynamicContext = new DynamicQueryContext(configuration); dynamicContext.setContextItem(new XmlObjectNodeInfo(xmlObjectContextNode, configuration)); List<?> result = exp.evaluate(dynamicContext); for (Object item : result) {
/** * Run a previously-compiled query * * @param context The dynamic context * @param query The compiled query * @param source The initial context item for the query (may be null) * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query, Item source) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); if (source != null) { dqc.setContextItem(source); } return query.iterator(dqc); }
exp = staticQueryContext.compileQuery(xqueryString); DynamicQueryContext dynamicContext = new DynamicQueryContext(configuration); dynamicContext.setContextItem(new XmlObjectNodeInfo(xmlObjectContextNode, configuration)); Object result = exp.evaluateSingle(dynamicContext); if (result == null) {
/** * Run a previously-compiled query * * @param context The dynamic context * @param query The compiled query * @param source The initial context item for the query (may be null) * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query, Item source) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); if (source != null) { dqc.setContextItem(source); } return query.iterator(dqc); }
/** * Run a previously-compiled query * * @param context The dynamic context * @param query The compiled query * @param source The initial context item for the query (may be null) * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query, Item source) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); if (source != null) { dqc.setContextItem(source); } return query.iterator(dqc); }
dc.setContextItem(contextItem); dc.setParameter(contextVar, contextItem);
protected void queryContent(XQueryExpression expression, MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception { Configuration config = getConfiguration(); DynamicQueryContext dynamicEnv = new DynamicQueryContext(config); DocumentInfo doc = staticEnv.buildDocument(in.getContent()); dynamicEnv.setContextItem(doc); configureQuery(dynamicEnv, exchange, in); Properties props = outputProperties != null ? outputProperties : EMPTY_PROPS; if (RESULT_BYTES.equalsIgnoreCase(getResult())) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); Result result = new StreamResult(buffer); expression.pull(dynamicEnv, result, props); out.setContent(new BytesSource(buffer.toByteArray())); } else if (RESULT_STRING.equalsIgnoreCase(getResult())) { StringWriter buffer = new StringWriter(); Result result = new StreamResult(buffer); expression.pull(dynamicEnv, result, props); out.setContent(new StringSource(buffer.toString())); } else { DOMResult result = new DOMResult(); expression.pull(dynamicEnv, result, props); out.setContent(new DOMSource(result.getNode())); } }
/** * Run a previously-compiled query. The initial context item for the query is taken from the context * in which the query is called (if there is one); no parameters are supplied * * @param context The dynamic context * @param query The compiled query * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); Item c = context.getContextItem(); if (c != null) { dqc.setContextItem(c); } return query.iterator(dqc); }
/** * Run a previously-compiled query. The initial context item for the query is taken from the context * in which the query is called (if there is one); no parameters are supplied * * @param context The dynamic context * @param query The compiled query * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); Item c = context.getContextItem(); if (c != null) { dqc.setContextItem(c); } return query.iterator(dqc); }
/** * Run a previously-compiled query. The initial context item for the query is taken from the context * in which the query is called (if there is one); no parameters are supplied * * @param context The dynamic context * @param query The compiled query * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); Item c = context.getContextItem(); if (c != null) { dqc.setContextItem(c); } return query.iterator(dqc); }
/** * Set the initial context item for the query * * @param item the initial context item, or null if there is to be no initial context item * @throws SaxonApiException if the query declares the context item and does not define * it to be external */ public void setContextItem(XdmItem item) throws SaxonApiException { if (item != null) { GlobalContextRequirement gcr = expression.getExecutable().getGlobalContextRequirement(); if (gcr != null && !gcr.isExternal()) { throw new SaxonApiException("The context item for the query is not defined as external"); } context.setContextItem(item.getUnderlyingValue()); } }
/** * Set the initial context item for the query * * @param item the initial context item, or null if there is to be no initial context item * @throws SaxonApiException if the query declares the context item and does not define * it to be external */ public void setContextItem(XdmItem item) throws SaxonApiException { if (item != null) { GlobalContextRequirement gcr = expression.getExecutable().getGlobalContextRequirement(); if (gcr != null && !gcr.isExternal()) { throw new SaxonApiException("The context item for the query is not defined as external"); } context.setContextItem(item.getUnderlyingValue()); } }
dynamicEnv.setContextItem(doc);
dynamicEnv.setContextItem(doc);
dqc.setContextItem(source);