CentraSite Documentation : CentraSite Developer's Guide : API for XQJ : Working with the XQJ Interface : Executing an XQuery with an XQPreparedExpression
Executing an XQuery with an XQPreparedExpression
To execute an XQuery with an XQPreparedExpression
1. Invoke the getXQConnection() method to get the XQConnection object from the JAXRConnection.
Example
/* Get the XQConnection from the JAXRConnection */
XQConnection connection = jaxrConnection.getXQConnection ();
You have now established an XQConnection.
2. Create an XQPreparedExpression object from the XQConnection object. The XQPreparedExpression is used to invoke several other methods to perform various tasks using the CentraSite XQJ interface. You may create more than one XQPreparedExpression from a single connection if required.
Example
/* Create XQPreparedExpression from XQConnection */
String pQuery = "for $q in input()/bib/book return $q";
XQPreparedExpression preparedExpression = conn.prepareExpression(pQuery);
3. Optionally, you can bind one or more external variables. An external variable is a type of variable that can be dynamically added to the query by declaring the variable in the query. The value of the variable can be set externally and added to the pre-set variable while executing the XQuery.
Example
/* Binding variables in Prepared Expressions */
String pQuery = "declare variable $int as xs:int external" +
"for $q in input()/bib/book where $q/@year = $int return $q";
 
XQPreparedExpression preparedExpression = conn.prepareExpression(pQuery);
 
/* Bind the appropriate value to the prepared expression */
/* using the matching binding API provided. */
Using bindInt() to bind an int value to the prepared expression
preparedExpression.bindInt(new QName("int"),
1994, XQItemTypeHelper.createIntXQItemType());
Using bindNode() to bind a node to the prepared expression
/* Get a node to bind by executing an expression */
XQExpression expression = connection.createExpression();
XQResultSequence xqResultSequence =
expression.executeQuery("for $q in input()/bib/book
where $q/@year = 1994 return $q/title");
xqResultSequence.next();
 
/* Get a node from the result sequence retrieved above */
Node node = xqResultSequence.getNode();
 
/* PreparedQuery */
String pquery = "declare variable $node external " +
"for $q in input()/bib/book where $q/title = $node return $q";
XQPreparedExpression prepared = connection.prepareExpression(pQuery);
 
/* Bind the above retrieved node to the prepared query */
prepared.bindNode(new QName("node"), node);
4. Invoke the executeQuery() method. This returns an XQResultSequence.
Example
/* Execute the prepared expression which returns an XQResultSequence */
XQResultSequence xqResultSequence = preparedExpression.executeQuery();
5. The XQResultSequence represents the XQuery result. Retrieve the query result and read/print it in XML format. The query result sequence is displayed item by item. Using XQJ, it is possible to get the result sequence in DOM, SAX, and StAX-compatible formats.
Example
/* Iterating the XQResultSequence */
XQResultSequence xqResultSequence = preparedExpression.executeQuery();
XMLStreamReader reader = null;
While(xqResultSequence.next())
{
reader = xqResultSequence.getItemAsStream();
/* Iterate the XML StreamReader using StAX-compatible APIs */
}
Example using the getInt() method
XQResultSequence xqResultSequence = preparedExpression.executeQuery();
xqResultSequence.next();
int I = xqResultSequence.getInt();
Example using the getAtomicValue() method
XQResultSequence xqResultSequence = preparedExpression.executeQuery();
xqResultSequence.next();
String str = xqResultSequence.getAtomicValue();
Example using the getNode() method
XQResultSequence xqResultSequence = preparedExpression.executeQuery();
xqResultSequence.next();
Node node = result.getNode();
Example using the writeItemToSAX() method
XQResultSequence xqResultSequence = preparedExpression.executeQuery();
xqResultSequence.next();
StringWriter sw = new StringWriter();
 
/* Provide an org.xml.sax.ContentHandler, which is saxhandler in our case */
XQSAXTextEventHandler saxhandler = new XQSAXTextEventHandler(sw);
resultSequence.writeItemToSAX(saxhandler);
System.out.println(sw);
6. Finally, invoke the XQConnection.close() method to close the connection to the registry/repository.
Example
/* Commit and close the XQConnection once you have completed working */
/* with it */
connection.commit();
connection.close();
Copyright © 2005-2015 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback