Hi,

 

We are using saxon9.4HE

Our entire application is dependent on XQuery, we have all our data in a single big XML file,

We create XDmNode once and cache XDMNode and Processor( that was used to create XDmNode). When there is any addition/update of XML data, we re-create the XDMNode and update the cache.

For every query request, we get the XDMNode and Processor from cache and execute XQuery.

 

The reason why we are caching XDMNode is that, we are getting good performance if we re-use the XDMNode from cache instead of creating XDMNode from the Big XML file for every query request.

 

Following is the example code

 

Code to create XDMNode

private  XdmNode getNode(String xmlPayLoad)

{

                //create processor

                    Processor proc = new Processor(false);

                    //create buffered reader for the xml payload

                    BufferedReader reader = new BufferedReader(new StringReader(xmlPayLoad));

     //create stream source

     StreamSource streamSource = new StreamSource(reader);

     //create document builder

        DocumentBuilder docBuilder = proc.newDocumentBuilder();

        //create node

        XdmNode node = null;

                                try {

                                                node = docBuilder.build(streamSource);

                                } catch (SaxonApiException e) {

                                                e.printStackTrace();

                                }

         return node;

}

 

 

The XDMNode and processor is cached

 

And Following is the code to execute XQuery

 

 

                private String executeXQuery(XdmNode node,String Processor proc,String xQueryString)

                {

                                StringBuilder resultBuff = new StringBuilder();

 

        XQueryCompiler comp = proc.newXQueryCompiler();

       

        //compile the query

        XQueryExecutable exprExec;

                                try {

                                                exprExec = comp.compile(xQueryString);

                                } catch (SaxonApiException e) {

                                                e.printStackTrace();

                                                return resultBuff.toString();

                                }

        XQueryEvaluator queryEvaluator = exprExec.load();

        queryEvaluator.setExternalVariable(new QName("params"), node);

        //evaluate the query

        XdmValue result;

                                try {

                                                result = queryEvaluator.evaluate();

                                                int len = result.size();

                                                for(int i=0;i<len;i++)

                                                {

                                                                resultBuff.append(result.itemAt(i));

                                                }

 

                                } catch (SaxonApiException e) {

                                                e.printStackTrace();

                                }

                               

                                return resultBuff.toString();

 

                }

 

Following is the XQuery

declare variable $params external;  for $param in $params/offerings/offering    return $param/segments/segment

 

Following is the sample XML file

 

<offerings><offering><id>1</id><allowances><allowance><id>33</id><productName>AllowanceName</productName></allowance></allowances><segments><segment><id>11</id><name>segmentName</name></segment></segments></offering></offerings>

 

 

Everything was working fine for single node, once we move to multiple Node, it’s failing as XDMNode is not getting propogated to second mode as it’s not serialized.

 

Could you please let us know if we are doing the right way, if not, could you please suggest alternative method

 

Regards

Mohan