Saxon XQuery With Multiple Documents

14 Jan 2012

SC London
The place where passionate Software Craftspeople, Community, Experts & Thought Leaders come together
Book your ticket now!

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]

Blogs Relacionados

Get content like this straight to your inbox!

Codurance Logo

Software es nuestra pasión.

Somos Software Craftspeople. Construimos software bien elaborado para nuestros clientes, ayudamos a los desarrolladores a mejorar en su oficio a través de la formación, la orientación y la tutoría y ayudamos a las empresas a mejorar en la distribución de software.

Últimos Blogs




Contacto

3 Sutton Lane, planta 3
Londres, EC1M 5PU

Teléfono: +44 207 4902967

2 Mount Street
Manchester, M2 5WQ

Teléfono: +44 161 302 6795

Carrer de Pallars 99, planta 4, sala 41
Barcelona, 08018

Teléfono: +34 937 82 28 82

Correo electrónico: hello@codurance.es