Using XQuery from the command line


Pre-requisites

You must have installed the XQuery implementation, XDK and JDK and set the CLASSPATHs appropriately. After this you can
  • Run XQuery queries from the command line : "java oracle.xquery.XQueryContext"
    or
  • Use the new interactive command-line tool, XQLPlus : "java oracle.xquery.XQLPlus"

XQueryContext

Running XQueryContext

java oracle.xquery.XQueryContext [-xqfile ] [-baseurl ] [-debug ]
  • filename - name of a file containing the XQuery query.
  • url - (optional) a baseurl to be used when accessing file names inside document functions in the XQuery. If no baseurl is specified, use the location of the XQuery query file.
  • debug - (optional) prints out debugging statements.

XQueryContext Examples

 Using xqfile:

Assuming that you have exmpl1.xql in your C:\xquery directory, have bib.xml in the same directory and exmpl1.xql contains the following query
<bib>
  {
    FOR $b IN document("bib.xml")/bib/book
    WHERE $b/publisher = "Addison-Wesley" AND $b/@year > 1991
    RETURN
      <book year="{$b/@year }">
        { $b/title }
      </book>
  }
</bib>
you can execute that XQuery using the command line
java oracle.xquery.XQueryContext -xqfile exmpl1.xql
  This will execute the XQuery contained in the file exmpl1.xql and print out the results. If there are multiple nodes returned each one will be printed out on a separate line.

You can use the -debug option to see each node output.

 Using baseurl:

In the previous example, exmpl1.xql referenced bib.xml. By default this is picked up from the same location as xqfile. For example, if you had specified,
java oracle.xquery.XQueryContext -xqfile C:\tmp\exmpl1.xql
the document function will look for bib.xml in C:\tmp\bib.xml. You can override this using the baseurl,
java oracle.xquery.XQueryContext -baseurl C:\documents\  -xqfile C:\tmp\exmpl1.xql
 
This uses the exmpl1.xql from C:\tmp, but for the filenames referenced inside the XQuery, it uses C:\documents as the base url.
 

XQLPlus interactive command line utility

Running XQLPlus

java oracle.xquery.XQLPlus [<filename>]
  • filename - name of a file containing the XQuery query(ies)

XQLPlus Examples

Note: in the interactive examples below, red text is typed in by the user as part of an interactive session.
Each command is terminated by "/;".

Assuming that you have exmpl1.xql in your C:\xquery directory, have bib.xml in the same directory and exmpl1.xql contains the following query

<bib>
  {
    FOR $b IN document("bib.xml")/bib/book
    WHERE $b/publisher = "Addison-Wesley" AND $b/@year > 1991
    RETURN
      <book year="{ $b/@year }">
        { $b/title }
      </book>
  }
</bib>

 Run the XQuery from the command line

java oracle.xquery.XQLPlus exmpl1.xql
  This will execute the XQuery contained in the file exmpl1.xql and print out the results. If there are multiple nodes returned each one will be printed out on a separate line.

 Run the XQuery interactively, from the file

java oracle.xquery.XQLPlus
XQL > @exmpl1.xql

 Run the XQuery interactively

java oracle.xquery.XQLPlus
XQL >   <bib>
XQL >   {
XQL >     FOR $b IN document("bib.xml")/bib/book
XQL >     WHERE $b/publisher = "Addison-Wesley" AND $b/@year > 1991
XQL >     RETURN
XQL >       <book year="{ $b/@year }">
XQL >         { $b/title }
XQL >       </book>
XQL >   }
XQL > </bib>
XQL > /;
XQL >

 
 
 
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy