Project

Profile

Help

Bug #2153 ยป RunXPathWithSaxon.java

Radu Pisoi, 2014-09-26 10:39

 

package ro.sync.test;

import java.util.Iterator;

import javax.xml.transform.TransformerConfigurationException;

import net.sf.saxon.event.Builder;
import net.sf.saxon.lib.FeatureKeys;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XPathCompiler;
import net.sf.saxon.s9api.XPathExecutable;
import net.sf.saxon.s9api.XPathSelector;
import net.sf.saxon.s9api.XdmItem;

import com.saxonica.config.ProfessionalConfiguration;


/**
* Test class for running an XPath expression with Saxon 9 API.
*/
public class RunXPathWithSaxon {

/**
* Runs an XPath 3.0 using Saxon9 API.
*/
public static void runQueryWithS9API(String userExpression) throws XPathException, TransformerConfigurationException, SaxonApiException {
// Create XPath processor
Processor proc = new Processor(new ProfessionalConfiguration());
proc.setConfigurationProperty(
FeatureKeys.NAME_POOL, proc.getUnderlyingConfiguration().getNamePool());
proc.setConfigurationProperty(FeatureKeys.TREE_MODEL, Builder.LINKED_TREE);

XPathCompiler comp = proc.newXPathCompiler();
comp.setLanguageVersion("3.0");
XPathExecutable exec = comp.compile(userExpression);

XPathSelector selector = exec.load();
Iterator<XdmItem> iter = selector.iterator();
if (iter != null) {
// Return empty query result
// Convert NodeInfo list to DOM Nodes
for (; iter.hasNext();) {
XdmItem nextItem = iter.next();
System.out.println("Next item: " + nextItem);
}
}
}

/**
* Run an XPath expression using Saxon API.
*/
public static void main(String[] args) throws Exception {
String expression = "parse-xml-fragment(\"<alpha>abcd</alpha><beta>abcd</beta>\")";
runQueryWithS9API(expression);
}
}
    (1-1/1)