Using the Command Line Utility


The DataDirect XQuery command line utility allows you to quickly run and test XQueries through a console window.

To invoke this utility, enter the following command at a prompt from the /lib subdirectory of your DataDirect XQuery installation directory (for example, ddxq/lib):

java -jar ddxq.jar 

Alternatively, you can specify the path to the lib directory in the command line, for example:

java -jar ddxq/lib/ddxq.jar 

NOTE: If your XQuery needs to locate classes other than the DataDirect XQuery classes – if you are specifying a custom URI resolver, for example – you must perform one of the following actions:

The following table lists the options available for the utility.

Table 1-1. Command Line Utility Options
Option
Description
-cr classname 
Specifies the CollectionURIResolver class to use. See the NOTE about setting your CLASSPATH for custom URI resolvers.
-e [xhtml|xml] 
Generates an XQuery execution plan and, optionally, specifies the format of the plan. If a format is not specified, XHTML is generated.
-jdbc jdbcurl 
Specifies a connection URI. See Relational Data Source Connections.
NOTE: On UNIX and Linux, the value for this option must be enclosed with double quotes, for example:
java -jar ddxq.jar -jdbc 
"jdbc:xquery:sqlserver://localhost:1433
;databaseName=pubs;user=sa" 
-mr classname 
Specifies the ModuleURIResolver class to use. See the NOTE about setting your CLASSPATH for custom URI resolvers.
-noext 
Disallows calls to Java methods.
-o filename 
Sends results (output) to specified file.
-option 
property=value 
Specifies XQuery or JDBC global options. See Using Option Declarations and Extension Expressions for more information.
-prop 
property=value 
Specifies data source and connection options. See DDXQDataSource and DDXQJDBCConnection Properties for more information.
-p 
Displays a stack trace in case of an exception.
-r classname 
Specifies the URIResolver class to use. See the NOTE about setting your CLASSPATH for custom URI resolvers.
-s file|URI 
Specifies an initial context item in the form of a file name or a URI.
-t 
Displays version and timing information.
-u 
Enables automatic updating of sources.
-version 
Display version information.
-? 
Displays the help for the command-line utility.
param=value 
Specifies a query string parameter and its value.
#param=value 
Specifies a query number parameter and its value. On UNIX and Linux, the value for this option must be enclosed with double quotes, for example:
java -jar ddxq.jar q.xq "#i=2"
+param=value 
Specifies a query document parameter and its value.
!option=value 
Specifies a serialization option and its value.

Example 1: Executes a Simple XQuery

This example executes the simple query {2+5}.

java -jar ddxq.jar {2+5} 

Example 2: Retrieves Values from an Initial Context Item

This example retrieves all values for UserId from the initial context item users.xml.

java -jar ddxq.jar -s ..\..\examples\xml\users.xml 
{//users/UserId} 

Example 3: Retrieves Values and Writes Them to a File

This example retrieves all values for UserId and writes the results to a file named out.xml.

java -jar ddxq.jar -o out.xml 
{doc('..\..\examples\xml\users.xml')/users/UserId} 

Example 4: Executes an XQuery in a File

This example executes the XQuery contained in the file myXQuery.xq using the initial context item input.xml.

java -jar ddxq.jar -s input.xml myXQuery.xq 

Example 5: Binds a Query Document Parameter

This example executes the XQuery contained in the file myXQuery.xq binding the query document parameter inputDoc to the input.xml document.

java -jar ddxq.jar myXQuery.xq +inputDoc=input.xml 

Example 6: Binds a Query String Parameter and Sets an Option

This example executes the XQuery contained in the file myXQuery.xq binding the query string parameter param1 to the character string Jonathan and setting the serialization option indent to yes so that results are indented.

java -jar ddxq.jar myXQuery.xq param1=Jonathan !indent=yes 

Example 7: Accesses a Relational Data Source

This example executes the XQuery contained in the file myXQuery2.xq that accesses a relational data source. See the NOTE about specifying connection URLs.

java -jar ddxq.jar -jdbc 
"jdbc:xquery:sqlserver://localhost:1433;databaseName=pubs; 
user=sa" myXQuery2.xq 

Example 8: Specifies a Document URI

This example retrieves all values for UserId, specifies a document URI, and writes the results to a file named out.xml.

java -cp c:\myClasses com.ddtek.xquery.Query 
-r myURIResolver -o out.xml {doc('users.xml')/users/UserId}