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(); } }
/** * Create an XPathCompiler. An XPathCompiler is used to compile XPath expressions. * * @return a newly created XPathCompiler */ /*@NotNull*/ public XPathCompiler newXPathCompiler() { return new XPathCompiler(this); }
private static XPathCompiler newXPathCompiler(final XPathVersion xpathVersion) throws IllegalArgumentException { final XPathCompiler xpathCompiler = SAXON_PROCESSOR.newXPathCompiler(); xpathCompiler.setLanguageVersion(xpathVersion.getVersionNumber()); /* * No need for caching since we are only using this for XPaths in Policy/PolicySet (AttributeSelector and xpathExpression), not in the Request (not supported) */ xpathCompiler.setCaching(false); xpathCompiler.setSchemaAware(false); return xpathCompiler; }
XPathCompiler c = new Processor(false).newXPathCompiler(); c.setAllowUndeclaredVariables(true); XPathExecutable exp = c.compile(xpathExpression);
private void test(Document doc, String xpathString) throws Exception { Processor proc = new Processor(false); XdmNode docNode = proc.newDocumentBuilder().wrap(doc); XPathCompiler xpath = proc.newXPathCompiler(); xpath.declareNamespace("", "http://www.xfa.org/schema/xfa-template/2.8/"); XdmValue result = xpath.evaluate(xpathString, docNode); int fieldHits = 0; for (XdmItem item : result) { String name = ((XdmNode)node).getNodeName().getLocalName(); fieldHits = "field".equals(name) ? fieldHits + 1 : fieldHits; } System.out.println("#hits total: " + nodes.getLength()); System.out.println("#hits 'field': " + fieldHits); }
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); } } }
@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); } }
/** * 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; }
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; } }
xpath.declareNamespace("saxon", "http://saxon.sf.net/"); // not actually used, just for demonstration xpath.importSchemaNamespace(""); // import schema for the non-namespace XPathSelector verify = xpath.compile(". instance of document-node(schema-element(BOOKLIST))").load(); verify.setContextItem(booksDoc); if (((XdmAtomicValue)verify.evaluateSingle()).getBooleanValue()) { XPathSelector selector = xpath.compile("//schema-element(ITEM)").load(); selector.setContextItem(booksDoc); QName titleName = new QName("TITLE");
while (it.hasNext()) { Entry<String, String> entry = it.next(); xpathCompiler.declareNamespace(entry.getKey(), entry.getValue()); xpathCompiler.declareNamespace("xml",NamespaceConstant.XML); xpathCompiler.declareNamespace("xs",NamespaceConstant.SCHEMA); xpathCompiler.declareNamespace("fn",NamespaceConstant.FN);
xpathCompiler.setCaching(true); xpathCompiler.declareNamespace("", XSPEC_NAMESPACE);
public XPathResolver(boolean autoNodeTextExtraction) { this.autoNodeTextExtraction = autoNodeTextExtraction; this.xpathProcessor = new Processor(false); this.xpath = xpathProcessor.newXPathCompiler(); this.xpath.setCaching(true); }
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); } } }
/** * 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 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 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 XPathCompiler getXPathCompiler () { XPathCompiler xpathCompiler = processor.newXPathCompiler(); xpathCompiler.declareNamespace("lux", FunCall.LUX_NAMESPACE); return xpathCompiler; }
Processor proc = new Processor(); XdmNode docw = proc.newDocumentBuilder().wrap(doc); XPathCompiler xpath = proc.newXPathCompiler(); xpath.declareNamespace("ns", "http://www.example.com/XMLSchema"); XdmValue bookNodes = xpath.evaluate( "//ns:book[matches(./ns:title, '^ *XML.*?Developer.*?Guide *$', 'i')]", docw); for (XdmItem book : bookNodes) { .... }
public XPathResolver(boolean autoNodeTextExtraction) { this.autoNodeTextExtraction = autoNodeTextExtraction; this.xpathProcessor = new Processor(false); this.xpath = xpathProcessor.newXPathCompiler(); this.xpath.setCaching(true); }