Saxon XQuery With Multiple Documents

Mashooq Badar · 14 Jan 2012

Saxon is a wonderful API for XML processing. It provides complete support for XPath, XQuery and XSLT. Although I'm always baffled with it's lack of adoption compared to Xalan and Xerces. Having said that the online documentation can definitely do with some improvement.  The following is a quick example of of how you may execute an XQuery that takes multiple XML documents as input. [sourcecode language="java"] @Test public void runXQueryWithMultipleInputDocuments() throws SaxonApiException { Processor processor = new Processor(false);

DocumentBuilder documentBuilder = processor.newDocumentBuilder();
XdmNode document = documentBuilder.build(
        new StreamSource(new StringReader("<my><document>content</document></my>")));
XdmNode document2 = documentBuilder.build(
        new StreamSource(new StringReader("<my><document>content2</document></my>")));

XQueryCompiler xQueryCompiler = processor.newXQueryCompiler();
XQueryExecutable xQueryExecutable = xQueryCompiler.compile(
        "declare variable $mydoc external; " +
        "declare variable $mydoc2 external; " +
        "<newdoc><doc1>{$mydoc/my/document/text()}</doc1>" +
        "<doc2>{$mydoc2/my/document/text()}</doc2></newdoc>");

XQueryEvaluator xQueryEvaluator = xQueryExecutable.load();

QName name = new QName("mydoc");
xQueryEvaluator.setExternalVariable(name, document);
QName name2 = new QName("mydoc2");
xQueryEvaluator.setExternalVariable(name2, document2);

System.out.println(xQueryEvaluator.evaluate());

} [/sourcecode] This result is an output of: [sourcecode language="xml"] content content2 [/sourcecode]

Mashooq Badar Image

Mashooq Badar

Mash is a pragmatic software craftsman always looking to improve his software creation skills and helping others do the same. He firmly believes that a well-rounded software craftsman must have a keen interest in all aspects of software creation, including; process, people, technology, user experience, development, operation, maintenance, and social impact. He relishes the daily challenges that Codurance brings to him–stretching his existing knowledge and expertise allowing him to constantly grow as a professional.

Mash is an advisor and a leader. During his diverse career, he has succeeded in invigorating large ailing software projects as well as creating highly effective software teams and departments. His broad and deep technical knowledge, organisational skills, craft focus, and empathy to people involved have been integral to his success. He has worked in many roles for charities, investment banks, consultancies, government, media and cloud providers. He prides himself at being a hands-on software developer and believes that software development skills are very hard to learn and the best way to maintain them is to apply them.

All author posts
Codurance Logo

Software is our passion.

We are software craftspeople. We build well-crafted software for our clients, we help developers to get better at their craft through training, coaching and mentoring, and we help companies get better at delivering software.

Latest Blogs




Contact Us

15 Northburgh Street
London EC1V 0JR

Phone: +44 207 4902967

Carrer Aragó, 208
08011, Barcelona

Phone: +34 689 723 737

Email: hello@codurance.com