/** * Creates instance * * @param path * XPath executable * @param xPathCompiler * XPath compiler * @throws IllegalArgumentException * in case of invalid XPath */ public XPathEvaluator(final String path, final XPathCompiler xPathCompiler) throws IllegalArgumentException { try { this.exec = xPathCompiler.compile(path); } catch (final SaxonApiException e) { throw new IllegalArgumentException(this + ": Invalid XPath", e); } this.expr = path; }
XPathCompiler c = new Processor(false).newXPathCompiler(); c.setAllowUndeclaredVariables(true); XPathExecutable exp = c.compile(xpathExpression);
@Override public XPathExecutable load(ImmutablePair<String, String> key) throws Exception { String xPathQuery = key.left; String namespacesString = key.right; Processor processor = XPathUtil.getProcessor(); XPathCompiler xPathCompiler = processor.newXPathCompiler(); List<String[]> namespacesList = XPathUtil.namespacesParse(namespacesString); log.debug("Parsed namespaces:{} into list of namespaces:{}", namespacesString, namespacesList); for (String[] namespaces : namespacesList) { xPathCompiler.declareNamespace(namespaces[0], namespaces[1]); } log.debug("Declared namespaces:{}, now compiling xPathQuery:{}", namespacesList, xPathQuery); return xPathCompiler.compile(xPathQuery); } }
/** * 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); } }
String vs_source = "Z:/Code_JavaDOCX/1.xml"; Processor proc = new Processor(false); net.sf.saxon.s9api.DocumentBuilder builder = proc.newDocumentBuilder(); XPathCompiler xpc = proc.newXPathCompiler(); try{ XPathSelector selector = xpc.compile("//output").load(); selector.setContextItem(builder.build(new File(vs_source))); for (XdmItem item: selector) { System.out.println(item.getStringValue()); } } catch(Exception e) { e.printStackTrace(); }
/** * 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); } }
public static void main(String[] args) { try { Processor proc = new Processor(false); DocumentBuilder builder = proc.newDocumentBuilder(); XPathCompiler xpc = proc.newXPathCompiler(); XPathSelector selector = xpc.compile("//book/title").load(); selector.setContextItem(builder.build(new File("books.xml"))); for (XdmItem item: selector) { System.out.println(item.getStringValue()); } } catch(Exception e) { e.printStackTrace(); } }
/** * 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(); }
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); } } }
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; } }
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(); } }