/** * 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 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(); }
@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); } } };
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(); } }
try { selector = xPathExecutable.load(); selector.setContextItem(xdmNode); XdmValue nodes = selector.evaluate(); int length = nodes.size(); int indexToMatch = matchNumber; if(selector != null) { try { selector.getUnderlyingXPathContext().setContextItem(null); } catch (Exception e) { // NOSONAR Ignored on purpose
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());
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); } }
/** * Filter the content with the XPath expression specified when creating the XPathProcessor. * * @param content String to which the XPath expression will be applied * @return TRUE if the XPath expression evaluates to true, FALSE otherwise * @throws XPathException */ private boolean filter(StreamSource content) throws XPathException { try { // Prepare to evaluate the XPath expression against the content XdmNode xmlDoc = builder.build(content); xsel.setContextItem(xmlDoc); // Evaluate and return the boolean value for the XPath expression return xsel.effectiveBooleanValue(); } catch (SaxonApiException e) { log.error("Problems processing the content. EXPRESSION:" + xPathExpression + " " + e.getMessage(),e); throw new XPathException(e.getMessage()); } }
/** * 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); } }
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; } }
/** * Evaluate the expression, returning the result as an <code>Stream</code>. * * @return A stream delivering the results of the expression. * Each object in this stream will be an instance of <code>XdmItem</code>. Note * that the expression may be evaluated lazily, which means that a successful response * from this method does not imply that the expression has executed successfully: failures * may be reported later while retrieving items from the iterator. * @throws SaxonApiUncheckedException if a dynamic error occurs during XPath evaluation that * can be detected at this point. */ public XdmStream<XdmItem> stream() throws SaxonApiUncheckedException { return iterator().stream(); }
/** * Load the compiled XPath expression to prepare it for execution. * @return An XPathSelector. The returned XPathSelector can be used to set up the * dynamic context, and then to evaluate the expression. */ public XPathSelector load() { return new XPathSelector(exp, declaredVariables); }
use = selector.effectiveBooleanValue(); } catch (SaxonApiUncheckedException saue) { Throwable sae = saue.getCause();
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(); } }
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(); }
/** * 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); } }
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; } }
/** * Evaluate the expression, returning the result as an <code>Stream</code>. * * @return A stream delivering the results of the expression. * Each object in this stream will be an instance of <code>XdmItem</code>. Note * that the expression may be evaluated lazily, which means that a successful response * from this method does not imply that the expression has executed successfully: failures * may be reported later while retrieving items from the iterator. * @throws SaxonApiUncheckedException if a dynamic error occurs during XPath evaluation that * can be detected at this point. */ public XdmStream<XdmItem> stream() throws SaxonApiUncheckedException { return iterator().stream(); }
/** * Load the compiled XPath expression to prepare it for execution. * * @return An XPathSelector. The returned XPathSelector can be used to set up the * dynamic context, and then to evaluate the expression. */ public XPathSelector load() { Map<StructuredQName, XPathVariable> declaredVariables = new LinkedHashMap<StructuredQName, XPathVariable>(); for (Iterator iter = env.iterateExternalVariables(); iter.hasNext(); ) { XPathVariable var = (XPathVariable) iter.next(); declaredVariables.put(var.getVariableQName(), var); } return new XPathSelector(exp, declaredVariables); }
use = selector.effectiveBooleanValue(); } catch (SaxonApiUncheckedException saue) { Throwable sae = saue.getCause();