/** * @return An XPathSelector. The returned XPathSelector can be used to set up the dynamic context, and then to evaluate the expression. * @see XPathExecutable#load() */ public XPathSelector load() { return exec.load(); } }
private Iterable<XdmItem> getIterableXpathResult(String source, LogicalSource logicalSource) { DocumentBuilder documentBuilder = xpathProcessor.newDocumentBuilder(); StringReader reader = new StringReader(source); setNamespaces(logicalSource); try { XdmNode item = documentBuilder.build(new StreamSource(reader)); XPathSelector selector = xpath.compile(logicalSource.getIterator()).load(); selector.setContextItem(item); return selector; } catch (SaxonApiException e) { throw new RuntimeException(e); } }
/** * Compile and evaluate an XPath expression, supplied as a character string, with a given * context item. * * @param expression A non-null string containing the source text of the XPath expression * @param contextItem The context item to be used for evaluating the expression. This * may be null if the expression is to be evaluated with no context item. * @return the result of evaluating the XPath expression with this context item. Note that * the result is an {@link Iterable}, so that it can be used in a "for-each" loop such as * <code>for (XdmItem item : xpath.evaluate("//x", doc) {...}</code> * @throws SaxonApiException if any static error is detected while analyzing the expression, * or if any dynamic error is detected while evaluating it. * @since 9.3 */ public XdmValue evaluate(String expression, /*@Nullable*/ XdmItem contextItem) throws SaxonApiException { Objects.requireNonNull(expression); XPathSelector selector = compile(expression).load(); if (contextItem != null) { selector.setContextItem(contextItem); } return selector.evaluate(); }
/** * Compile and evaluate an XPath expression, supplied as a character string, with a given * context item. * * @param expression A non-null string containing the source text of the XPath expression * @param contextItem The context item to be used for evaluating the expression. This * may be null if the expression is to be evaluated with no context item. * @return the result of evaluating the XPath expression with this context item. Note that * the result is an {@link Iterable}, so that it can be used in a "for-each" loop such as * <code>for (XdmItem item : xpath.evaluate("//x", doc) {...}</code> * @throws SaxonApiException if any static error is detected while analyzing the expression, * or if any dynamic error is detected while evaluating it. * @since 9.3 */ public XdmValue evaluate(String expression, /*@Nullable*/ XdmItem contextItem) throws SaxonApiException { Objects.requireNonNull(expression); XPathSelector selector = compile(expression).load(); if (contextItem != null) { selector.setContextItem(contextItem); } return selector.evaluate(); }
private Iterable<XdmItem> getIterableXpathResult(String source, LogicalSource logicalSource) { DocumentBuilder documentBuilder = xpathProcessor.newDocumentBuilder(); StringReader reader = new StringReader(source); setNamespaces(logicalSource); try { XdmNode item = documentBuilder.build(new StreamSource(reader)); XPathSelector selector = xpath.compile(logicalSource.getIterator()).load(); selector.setContextItem(item); return selector; } catch (SaxonApiException e) { throw new RuntimeException(e); } }
/** * Compile and evaluate an XPath expression whose result is expected to be * a single item, with a given context item. The expression is supplied as * a character string. * * @param expression A non-null string containing the source text of the XPath expression * @param contextItem The context item to be used for evaluating the expression. This * may be null if the expression is to be evaluated with no context item. * @return the result of evaluating the XPath expression with this context item. * If the result is a singleton it is returned as an XdmItem; if it is an empty * sequence, the return value is null. If the expression returns a sequence of more than one item, * any items after the first are ignored. * @throws SaxonApiException if any static error is detected while analyzing the expression, * or if any dynamic error is detected while evaluating it. * @since 9.3 */ public XdmItem evaluateSingle(String expression, XdmItem contextItem) throws SaxonApiException { Objects.requireNonNull(expression); XPathSelector selector = compile(expression).load(); if (contextItem != null) { selector.setContextItem(contextItem); } return selector.evaluateSingle(); }
/** * Compile and evaluate an XPath expression whose result is expected to be * a single item, with a given context item. The expression is supplied as * a character string. * * @param expression A non-null string containing the source text of the XPath expression * @param contextItem The context item to be used for evaluating the expression. This * may be null if the expression is to be evaluated with no context item. * @return the result of evaluating the XPath expression with this context item. * If the result is a singleton it is returned as an XdmItem; if it is an empty * sequence, the return value is null. If the expression returns a sequence of more than one item, * any items after the first are ignored. * @throws SaxonApiException if any static error is detected while analyzing the expression, * or if any dynamic error is detected while evaluating it. * @since 9.3 */ public XdmItem evaluateSingle(String expression, XdmItem contextItem) throws SaxonApiException { Objects.requireNonNull(expression); XPathSelector selector = compile(expression).load(); if (contextItem != null) { selector.setContextItem(contextItem); } return selector.evaluateSingle(); }
/** * this is primarily for internal use * @param xpath an xpath expression to evaluate * @return the result of evaluating the xpath expression with the last indexed as context * @throws SaxonApiException if there is an error during compilation or evaluation */ public XdmValue evaluateXPath(String xpath) throws SaxonApiException { XPathExecutable xpathExec = xpathCache.get(xpath); if (xpathExec == null) { xpathExec = getXPathCompiler().compile(xpath); xpathCache.put(xpath, xpathExec); } XPathSelector xps = xpathExec.load(); xps.setContextItem(getXdmNode()); return xps.evaluate(); }
/** * Apply XSLT 3.0 static parameters to a compilerInfo. Actually this sets all parameter values, whether static or dynamic. * This is possible because the stylesheet is compiled for once-only use. * * @param compiler The XsltCompiler object into which the parameters are copied * @throws SaxonApiException if invalid options are found */ public void applyStaticParams(XsltCompiler compiler) throws SaxonApiException { Processor processor = compiler.getProcessor(); for (Enumeration e = paramValues.propertyNames(); e.hasMoreElements(); ) { String name = (String) e.nextElement(); String value = paramValues.getProperty(name); compiler.setParameter(QName.fromClarkName(name), new XdmAtomicValue(value, ItemType.UNTYPED_ATOMIC)); } for (Enumeration e = paramExpressions.propertyNames(); e.hasMoreElements(); ) { String name = (String) e.nextElement(); String value = paramExpressions.getProperty(name); // parameters starting with "?" are taken as XPath expressions XPathCompiler xpc = processor.newXPathCompiler(); XPathExecutable xpe = xpc.compile(value); XdmValue val = xpe.load().evaluate(); compiler.setParameter(QName.fromClarkName(name), val); } }
@Override public Stream<? extends XdmItem> apply(XdmItem item) { try { XPathSelector selector = load(); selector.setContextItem(item); XdmSequenceIterator<XdmItem> result = selector.iterator(); return result.stream(); } catch (SaxonApiException e) { throw new SaxonApiUncheckedException(e); } } };
@Override public Stream<? extends XdmItem> apply(XdmItem item) { try { XPathSelector selector = load(); selector.setContextItem(item); XdmSequenceIterator<XdmItem> result = selector.iterator(); return result.stream(); } catch (SaxonApiException e) { throw new SaxonApiUncheckedException(e); } } };
Processor proc = new Processor(true); proc.getUnderlyingConfiguration().registerExternalObjectModel(JDOM2ObjectModel.getInstance()); DocumentBuilder db = proc.newDocumentBuilder(); Document doc1 = new Document(new Element("root", "This is a test")); Document doc2 = doc1.clone(); XdmNode xdmDoc1 = db.wrap(doc1); XdmNode xdmDoc2 = db.wrap(doc2); XPathCompiler xpath = proc.newXPathCompiler(); xpath.declareVariable(new QName("doc2")); XPathExecutable xx = xpath.compile("deep-equal(., $doc2)"); XPathSelector sel = xx.load(); sel.setContextItem(xdmDoc1); sel.setVariable(new QName("doc2"), xdmDoc2); XdmItem result = sel.evaluateSingle(); System.out.println(result.toString());
public XpathFilterCollector(String query) { xpath = query; config = new Config(); Processor processor = new Processor(config); XPathCompiler compiler = processor.newXPathCompiler(); try { selector = compiler.compile(xpath).load(); } catch (SaxonApiException e) { throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Unable to compile xpath: " + query, e); } }
/** * Counts all rule violations: failed asserts and successful reports). * * @param results * The validation results (svrl:schematron-output). * @return An integer value. */ private int countRuleViolations(XdmDestination results) { XPathCompiler xpath = processor.newXPathCompiler(); xpath.declareNamespace("svrl", ISO_SCHEMATRON_SVRL_NS); XdmAtomicValue totalCount = null; try { XPathExecutable exe = xpath.compile("count(//svrl:failed-assert) + count(//svrl:successful-report)"); XPathSelector selector = exe.load(); selector.setContextItem(results.getXdmNode()); totalCount = (XdmAtomicValue) selector.evaluateSingle(); } catch (SaxonApiException e) { LOGR.warning(e.getMessage()); } return Integer.parseInt(totalCount.getValue().toString()); }
private XdmItem computeGroup(XdmNode node) { try { XPathCompiler xcomp = runtime.getProcessor().newXPathCompiler(); xcomp.setBaseURI(step.getNode().getBaseURI()); for (String prefix : groupAdjacent.getNamespaceBindings().keySet()) { xcomp.declareNamespace(prefix, groupAdjacent.getNamespaceBindings().get(prefix)); } XPathExecutable xexec = xcomp.compile(groupAdjacent.getString()); XPathSelector selector = xexec.load(); selector.setContextItem(node); Iterator<XdmItem> values = selector.iterator(); if (values.hasNext()) { return values.next(); } else { return null; } } catch (SaxonApiException sae) { throw new XProcException(sae); } } }
public static boolean xpathEqual(Processor proc, XdmItem a, XdmItem b) { try { XPathCompiler c = proc.newXPathCompiler(); c.declareVariable(vara); c.declareVariable(varb); XPathExecutable xexec = c.compile("$vara = $varb"); XPathSelector selector = xexec.load(); selector.setVariable(vara,a); selector.setVariable(varb,b); Iterator<XdmItem> values = selector.iterator(); XdmAtomicValue item = (XdmAtomicValue) values.next(); boolean same = item.getBooleanValue(); return same; } catch (SaxonApiException sae) { return false; } }
private XdmItem computeGroup(XdmNode node) { try { XPathCompiler xcomp = runtime.getProcessor().newXPathCompiler(); xcomp.setBaseURI(step.getNode().getBaseURI()); for (String prefix : groupAdjacent.getNamespaceBindings().keySet()) { xcomp.declareNamespace(prefix, groupAdjacent.getNamespaceBindings().get(prefix)); } XPathExecutable xexec = xcomp.compile(groupAdjacent.getString()); XPathSelector selector = xexec.load(); selector.setContextItem(node); Iterator<XdmItem> values = selector.iterator(); if (values.hasNext()) { return values.next(); } else { return null; } } catch (SaxonApiException sae) { throw new XProcException(sae); } } }
public static boolean xpathEqual(Processor proc, XdmItem a, XdmItem b) { try { XPathCompiler c = proc.newXPathCompiler(); c.declareVariable(vara); c.declareVariable(varb); XPathExecutable xexec = c.compile("$vara = $varb"); XPathSelector selector = xexec.load(); selector.setVariable(vara,a); selector.setVariable(varb,b); Iterator<XdmItem> values = selector.iterator(); XdmAtomicValue item = (XdmAtomicValue) values.next(); boolean same = item.getBooleanValue(); return same; } catch (SaxonApiException sae) { return false; } }
private String computedLabel(XdmNode node) throws SaxonApiException { XPathCompiler xcomp = runtime.getProcessor().newXPathCompiler(); xcomp.setBaseURI(step.getNode().getBaseURI()); // Make sure any namespace bindings in-scope for the label are available for the expression for (String prefix : label.getNamespaceBindings().keySet()) { xcomp.declareNamespace(prefix, label.getNamespaceBindings().get(prefix)); } xcomp.declareVariable(p_index); XPathExecutable xexec = xcomp.compile(label.getString()); XPathSelector selector = xexec.load(); selector.setVariable(p_index,new XdmAtomicValue(count++)); selector.setContextItem(node); Iterator<XdmItem> values = selector.iterator(); XdmItem item = values.next(); return item.getStringValue(); } }
private String computedLabel(XdmNode node) throws SaxonApiException { XPathCompiler xcomp = runtime.getProcessor().newXPathCompiler(); xcomp.setBaseURI(step.getNode().getBaseURI()); // Make sure any namespace bindings in-scope for the label are available for the expression for (String prefix : label.getNamespaceBindings().keySet()) { xcomp.declareNamespace(prefix, label.getNamespaceBindings().get(prefix)); } xcomp.declareVariable(p_index); XPathExecutable xexec = xcomp.compile(label.getString()); XPathSelector selector = xexec.load(); selector.setVariable(p_index,new XdmAtomicValue(count++)); selector.setContextItem(node); Iterator<XdmItem> values = selector.iterator(); XdmItem item = values.next(); return item.getStringValue(); } }