import java.io.
File;
import net.sf.saxon.s9api.*;
public class XQueryExample {
public static void main(String[] args) {
try {
// Create a Processor instance (this will be used to handle XQuery)
Processor processor = new Processor(false); // 'false' for non-
validation mode
// Create a XQueryCompiler
XQueryCompiler compiler = processor.newXQueryCompiler();
// Define your XQuery expression
String xqueryString = "for $book in doc('courses.xml')/books/book
return $book/title";
// Compile the XQuery expression
XQueryExecutable executable = compiler.compile(xqueryString);
// Create an XQueryEvaluator
XQueryEvaluator evaluator = executable.load();
// Set the input document (books.xml) for the XQuery evaluation
// Note: Ensure the 'books.xml' file is available or replace it with an
appropriate XML file path
File inputFile = new File("C:\\Users\\DELL\\Desktop\\Xquery\\
courses.xml");
XdmNode document = processor.newDocumentBuilder().build(inputFile);
// Set the document as the context for the query
evaluator.setContextItem(document);
// Execute the query and get the result
XdmValue result = evaluator.evaluate();
// Iterate through the result (which will be a sequence of titles)
for (XdmItem item : result) {
System.out.println(item.getStringValue());
}
} catch (SaxonApiException e) {
e.printStackTrace();
}
}
}