Processor proc = new Processor(); InputStream input = new FileInputStream("/tmp/g.html"); XMLReader reader = new Parser(); reader.setFeature(Parser.namespacesFeature, false); Source source = new SAXSource(parser, input); DocumentBuilder builder = proc.newDocumentBuilder(); XdmNode input = builder.build(source); XPathCompiler compiler = proc.newXPathCompiler(); XdmValue result = compiler.evaluate("//span", input); System.out.println(result.size());
public static TestResults fromReport(String name, XdmNode xdmResult, XPathCompiler xpathCompiler, String time) { Builder builder = new TestResults.Builder(name).time(time); try { builder.addRuns( ((XdmAtomicValue) xpathCompiler.evaluateSingle( "count(//test)", xdmResult)).getLongValue()) .addFailures( ((XdmAtomicValue) xpathCompiler.evaluateSingle( "count(//test[@successful = 'false'])", xdmResult)).getLongValue()) .addSkipped( ((XdmAtomicValue) xpathCompiler.evaluateSingle( "count(//test[@pending = 'true'])", xdmResult)).getLongValue()); for (XdmItem xdmItem : xpathCompiler .evaluate( "//test[@successful='false']/string-join(ancestor-or-self::*/label,' ')", xdmResult)) { builder.addFailureDetail(xdmItem.getStringValue(), null); } } catch (SaxonApiException e) { throw new IllegalStateException(e); } return builder.build(); }
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); }
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) { .... }