What's wrong with my XQueryProcessor ?

Help
Anonymous
2004-08-23
2012-10-08
  • Anonymous - 2004-08-23

    Follow is My hello world program of saxon xqueryprocessor:

    Description:

    I wrap my xqueryprocessor  in a servlet,user post    query string to it via browser, then send  query result back.

    ----XQueryProcessor.java-----------
    import java.io.IOException;
    import java.io.File;
    import java.util.Properties;
    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.PrintWriter;

    import net.sf.saxon.*;
    import net.sf.saxon.Configuration;
    import net.sf.saxon.query.StaticQueryContext;
    import net.sf.saxon.query.QueryProcessor;
    import net.sf.saxon.query.XQueryExpression;
    import net.sf.saxon.xpath.XPathException;
    import net.sf.saxon.query.DynamicQueryContext;
    import net.sf.saxon.om.SequenceIterator;
    import net.sf.saxon.om.DocumentInfo;
    import net.sf.saxon.query.QueryResult;

    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.OutputKeys;
    /**
    * @version     1.0
    * @author
    */
    public class XQueryProcessor extends HttpServlet {

        /**
        * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
        */
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        }

        /**
        * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
        */
        public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            resp.setContentType("text/xml;charset=gb2312");
            String query = req.getParameter("query");
            PrintWriter out = resp.getWriter();
           
            Configuration conf = new Configuration();
            StaticQueryContext sqc = new StaticQueryContext(conf);
            QueryProcessor qp = new QueryProcessor(sqc);
            try {
                XQueryExpression xqe = qp.compileQuery(query);
               
                DynamicQueryContext dqc = new DynamicQueryContext();
               
                File xmlSource = new File("E:\\Java_tour\\x-xquery\\SaxOn\\saxonb8-0\\samples\\data\\book.xml");           
                dqc.setContextNode(qp.buildDocument(new StreamSource(xmlSource)));
                SequenceIterator si = xqe.iterator(dqc);   
                DocumentInfo documentInfo = QueryResult.wrap(si,conf);
               
                Properties prop = new Properties();
                prop.setProperty(OutputKeys.METHOD, "xml");
                prop.setProperty(OutputKeys.INDENT, "true");
               
                QueryResult.serialize(documentInfo, new StreamResult(out), prop, conf);
            } catch(XPathException xpe) {
                xpe.printStackTrace();
            } catch(TransformerException tfe) {
                tfe.printStackTrace();
            }   
           
            out.flush();
            out.close();
                   
           
        }

    }

    ---------end of XQueryProcessor--------------
    It was compiled well,but when I Post query to it ,server report following exception:

    --------------Exception----------------------

    [04-8-23 22:47:01:262 CST] 49728bfa WebGroup      I SRVE0180I: [XQWeb] [/XQWeb] [Servlet.LOG]: JSP 1.2 Processor: init
    [04-8-23 22:47:01:352 CST] 49728bfa WebGroup      I SRVE0180I: [XQWeb] [/XQWeb] [Servlet.LOG]: SimpleFileServlet: init
    [04-8-23 22:47:01:362 CST] 49728bfa WebGroup      I SRVE0180I: [XQWeb] [/XQWeb] [Servlet.LOG]: InvokerServlet: init
    [04-8-23 22:53:20:287 CST]  4c74bf9 WebGroup      I SRVE0180I: [XQWeb] [/XQWeb] [Servlet.LOG]: SaxOnProccessor: init
    [04-8-23 22:53:20:437 CST]  4c74bf9 WebGroup      E SRVE0026E: [Servlet 错误]-[java/net/URISyntaxException]:java.lang.NoClassDefFoundError: java/net/URISyntaxException
        at net.sf.saxon.query.StaticQueryContext.reset(StaticQueryContext.java:101)
        at net.sf.saxon.query.StaticQueryContext.<init>(StaticQueryContext.java:75)
        at SaxOnProccessor.doPost(SaxOnProccessor.java:51)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
        at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
        at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
        at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
        at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:258)
        at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
        at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
        at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:872)
        at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:491)
        at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:173)
        at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:79)
        at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:199)
        at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
        at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
        at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:331)
        at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
        at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:432)
        at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:343)
        at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:592)

    ----------end of exception------------------

    Any one can help,thanks a lot!!

     
    • Anonymous - 2004-08-23

      sorry for something wrong above: "SaxOnProcessor" should be "XQueryProcessor"

       
      • Michael Kay

        Michael Kay - 2004-08-23

        If it can't find java.net.URISyntaxException, then it's probably running under JDK 1.3 rather than JDK 1.4.

        I can't immediately tell what's wrong with your system configuration, but it's probably controlled by an environment variable somewhere.

        Michael Kay

        See http://sourceforge.net/project/project_donations.php?group_id=29872  

         
    • Anonymous - 2004-08-23

      I am using websphere v5  develop tools, and my program runing on its incidental test evironment,  it running under jdk1.3 ,I think it cause this problem.

      Thank you very much Michael Kay,thank for your quick response.

       
      • g_ball

        g_ball - 2004-10-19

        I would like to be able to manipulate global java variables from with in the scope of my xquery.
        for example;
        1) calling java class instantiate a hashmap.
        2) setparameter(hashmap)
        3) run query
        4) query calls map.add(name, "value")
        5) xquery terminates
        5) java caller can read the new value

        I am having two troubles:
        1) setparameter seems to copy the object value instead of giving the actual reference
        2) I am trying to find a way of declaring a custom object as external variable.

        here is what I have tried:

        declare namespace j = "http://saxon.sf.net/java-type";
        declare variable $test as j:java.util.HashMap external;

        let $empid := doc("ref/EmployeeId.xml")
        return
        <set>
        {$test:put("key2","val2")}
        </set>


        I get the following error

        Error
        XQuery syntax error on line 7 in ...turn &lt;set&gt; {$test:put(&quot;key2&quot;:
        expected "<name>", found "<function>("


        Thanks in advance,
        Greg

         
        • Michael Kay

          Michael Kay - 2004-10-19

          Please in future start a new thread to discuss a new topic - it's terribly confusing to overload an existing thread in this way.

          The syntax error is because function calls should not be preceded by a "$".

          It seems a bad idea to try to do what you are doing, because XQuery is a functional language and is supposed to be side-effect free - there is no guarantee whether or how often any particular expression will be evaluated. Nevertheless, you should be able to get away with it.

          Saxon will copy some kinds of value, e.g. a List gets copied to form a sequence. But values that it doesn't understand are simply wrapped as XPath objects.

          Michael Kay

           

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks