8 Replies Latest reply: Dec 16, 2011 9:33 AM by tsuji RSS

    Passing variable in fn:doc

    vviswana
      Node result = null;
      try
      {
      String xq = "declare variable *$per as document-node()* external;" +
      "declare variable *$job as document-node()* external;" +
      "for $i in *$per*//pimergednodes/Get__CompIntfc__CI_PERSONAL_DATAResponse,\n" +
      " $j in *$job*//jobmergednodes/Get__CompIntfc__CI_JOB_DATAResponse[PROP_EMPLID = $i/PROP_EMPLID]\n" +
      "\n" +
      " return\n" +
      " <emp>\n" +
      " {\n" +
      " $i/PROP_EMPLID,\n" +
      " $i/PROP_BIRTHDATE,\n" +
      " <coll_names>\n" +
      " {\n" +
      " $i/COLL_NAME_TYPE_VW/COLL_NAMES\n" +
      " }\n" +
      " </coll_names>,\n" +
      "\n" +
      " $i/PROP_BIRTHDATE/COLL_NAME_TYPE_VW/PROP_FIRST_NAME,\n" +
      " $j/COLL_JOB/PROP_DEPTID\n" +
      "\n" +
      " }\n" +
      " </emp>";

      XQDataSource xds = new oracle.xquery.xqj.OXQDataSource();
      XQConnection conn = xds.getConnection();
      XQPreparedExpression pEx = conn.prepareExpression(xq);

      File per1 = new File("c:\personal.xml");
      InputSource perSource = new InputSource(new FileInputStream(per1));
      DOMParser mdParser = new DOMParser();
      mdParser.parse(perSource);
      Document document = mdParser.getDocument();
      Node perNode = document.getDocumentElement();

      File job1 = new File("c:\job_info.xml");
      InputSource jobSource = new InputSource(new FileInputStream(job1));
      mdParser = new DOMParser();
      mdParser.parse(jobSource);
      Document document1 = mdParser.getDocument();
      Node jobNode = document1.getDocumentElement();

      pEx.bindNode(new javax.xml.namespace.QName("per"), perNode, null);
      pEx.bindNode(new javax.xml.namespace.QName("job"), jobNode, null);
      System.out.println("Before executing");
      XQResultSequence rslt = pEx.executeQuery();
      System.out.println("After executing");
      while (rslt.next()) {
      System.out.println(rslt.getAtomicValue());
      }

      return rslt.getNode();
      }
      catch (Exception e)
      {
      }

      The above code gives me the error. What I am trying to achieve is iterating over the over the node passed by the Java.

      Can I solve this problem and is there any other easier way to achieve this?

      Thanks In Advance,
      Vidya
        • 1. Re: Passing variable in fn:doc
          odie_63
          The above code gives me the error.
          And the error is ... ?
          Can I solve this problem and is there any other easier way to achieve this?
          If the XML sources are stored in the file system, then you can access them through the fn:doc function instead of binding them as external variables :
          for $i in fn:doc('c:\personal.xml')//pimergednodes/Get__CompIntfc__CI_PERSONAL_DATAResponse,
              $j in fn:doc('c:\job_info.xml')//jobmergednodes/Get__CompIntfc__CI_JOB_DATAResponse[PROP_EMPLID = $i/PROP_EMPLID]
          return
           <emp>
           {
            $i/PROP_EMPLID,
            $i/PROP_BIRTHDATE,
            <coll_names>
            {
             $i/COLL_NAME_TYPE_VW/COLL_NAMES
            }
            </coll_names>,
            $i/PROP_BIRTHDATE/COLL_NAME_TYPE_VW/PROP_FIRST_NAME,
            $j/COLL_JOB/PROP_DEPTID
           }
           </emp>
          • 2. Re: Passing variable in fn:doc
            tsuji
            Since
            Node perNode = document.getDocumentElement();
            and
            Node jobNode = document1.getDocumentElement();
            are root node (an Element), I would suggest if you insist in passing perNode and jobNode rather than document and document 1, you can change a bit of the query like this.
            String xq = "declare variable $per as element() external;" +
            "declare variable $job as element() external;" + ...
            In the bindNode method, you can pass null or constant XQITEMKIND_ELEMENT or XQITEMKIND_NODE with the purpose of making the implicit casting, if any, flawless.

            Edited by: tsuji on Dec 8, 2011 7:31 AM
            • 3. Re: Passing variable in fn:doc
              vviswana
              Hi,

              Thanks for the reply.

              I am getting below error after changing that as an element.

              oracle.xquery.xqj.OXQException: cursor is not on item
                   at oracle.xquery.xqj.OXQSequence.doOnItemChecks(OXQSequence.java:692)
                   at oracle.xquery.xqj.OXQSequence.getHelper(OXQSequence.java:646)
                   at oracle.xquery.xqj.OXQSequence.getNode(OXQSequence.java:533)
                   at oracle.appsintg.mashup.MashUpRunner.doMashUp(MashUpRunner.java:91)
                   at oracle.appsintg.mashup.MashUpRunner.main(MashUpRunner.java:104)

              Am I missing anything?

              Thanks In Advance,
              Vidya
              • 4. Re: Passing variable in fn:doc
                tsuji
                I actually do not know what kind of syntax is \*$per\* or \*$job\* (edit note: I need to backslash * to render it!!!). Can you give a reference for that syntax?

                Edited by: tsuji on Dec 8, 2011 8:48 AM
                • 5. Re: Passing variable in fn:doc
                  vviswana
                  Hi,

                  I am trying to process the xml content. ie. trying to merge 2 xml nodes.

                  The problem is I don't want to read it from the file as this will lead to performance issue. Instead thought of using the local variable and assign the local variable the JavaNode object using 'bindNode()' method in XQJ.

                  Is there any other way to achieve this?

                  Thanks,
                  Vidya
                  • 6. Re: Passing variable in fn:doc
                    tsuji
                    Sure that's what we can see more or less the intention... But I meant why there is a star (*) in front of and after the variables ($per and $job)? what are they?
                    • 7. Re: Passing variable in fn:doc
                      vviswana
                      Hi,

                      I wanted to make it as bold, but som how the forum added the *. So please ignore that..

                      Thanks,
                      Vidya
                      • 8. Re: Passing variable in fn:doc
                        tsuji
                        I suppose [1] that you've change the xquery declarations from document-node() to element() --- not that document-node() has anything wrong, but I suggested the change for the only reason that you use bindNode() method. And then I suppose [2] that the xquery is working from outside of java vm programming environment, like via a command-line environment implicitly using java platform and explicitly use doc() to load up the document rather than declaring them external; and [3] you've got all the loading and parsing part correctly. If that's done, the code as outline should work as expected.

                        I tested it out, and it works just fine. This, at least, I can assure you. (But, you can have many things that can go wrong and you think they don't matter etc...)

                        Now that we are told you've got an error like "oracle.xquery.xqj.OXQException: cursor is not on item etc...", would you make sure that you are able to ascertain [2] or that you can show the document(s) involved.