CentraSite Documentation : CentraSite Developer's Guide : API for XQJ : Working with the XQJ Interface : Executing an XQuery with a Standard XQExpression
Executing an XQuery with a Standard XQExpression
To execute an XQuery with a standard XQExpression
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 XQExpression object from the XQConnection object. The XQExpression is used to invoke several other methods to perform various tasks using the CentraSite XQJ interface. You may create more than one XQExpression from a single connection if required.
Example
/* Create XQExpression from XQConnection to execute an XQuery. */
XQExpression expression = connection.createExpression();
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
String xquery = "declare variable $year as xs:int external" +
"for $q in input()/bib/book where $q/@year > $year return $q" ;
XQExpression expression = connection.createExpression();
expression.bindInt(new QName("year"),
1993,XQItemTypeHelper.createIntXQItemType());
XQResultSequence xqResultSequence = expression.executeQuery(xquery);
4. Invoke the executeQuery() method. This returns an XQResultSequence.
Example
/* Executing an XQuery */
/* Instance of the query string: */
String xquery = "for $b in input()/book return $b/title";
/* Execute the above XQuery String, which returns an XQResultSequence */
XQResultSequence xqResultSequence = expression.executeQuery(xquery);
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.
Note:  
You cannot scroll the XQResultSequences backwards.
Example
/* Iterating the XQResultSequence */
XMLStreamReader reader = null;
While(xqResultSequence.next())
{
reader = xqResultSequence.getItemAsStream();
/* Iterate the XML StreamReader using StAX-compatible APIs */
}
connection.commit();
connection.close();
Example using the getInt() method
/* Instance of the XQuery String */
String xquery =" for $b in input()/bib/book return xs:int($b/@year) ";
 
/* This query on execution will return the year as an integer value */
XQResultSequence xqResultSequence = expression.executeQuery(xquery);
 
xqResultSequence.next();
int I = xqResultSequence.getInt();
Example using the getAtomicValue() method
/* Instance of the XQuery String */
String xquery = "for $p in input()/book return xs:string($p/title)";
XQResultSequence xqResultSequence = expression.executeQuery(xquery);
 
/* This query on execution will return the title as a String */
xqResultSequence.next();
String str = xqResultSequence.getAtomicValue();
Example using the getNode() method
/* Instance of the XQuery String */
String xquery = "for $q in input()/bib/book return $q";
XQResultSequence xqResultSequence = expression.executeQuery(xquery);
xqResultSequence.next();
Node node = result.getNode();
Example using the writeItemToSAX() method
xqResultSequence.next();
StringWriter sw = new StringWriter();
 
/* Provide a 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