Executing Queries
Next, we create an XQExpression object, which executes an XQuery expression that is read from a file and returns a sequence of results.
First, here is the XQuery expression, flwor.xq:
for $u in fn:collection('users')/users return <user> <name>{ $u/firstname, $u/lastname }</name> { for $h in collection('holdings')/holdings where $h/userid = $u/userid return <stock>{ $h/stockticker, $h/shares }</stock> }</user>An XQConnection can create an XQExpression:
XQExpression xqExpression = conn.createExpression(); FileReader fileReader = new FileReader("flwor.xq"); XQSequence xqSequence = xqExpression.executeQuery(fileReader);Now that the query results are in a sequence, you can serialize this sequence using the getSequenceAsString() method. (Serializing is just one way to handle an XQuery result.)
The following result sequence contains a single node, the user element (whitespace has been modified for readability).
<user> <name> <firstname>Jonathan</firstname> <lastname>Robie</lastname> </name> <stock> <stockticker>AMZN</stockticker> <shares>3000</shares> </stock> <stock> <stockticker>EBAY</stockticker> <shares>4000</shares> </stock> <stock> <stockticker>IBM</stockticker> <shares>2500</shares> </stock> ... </user>Other similar examples can be found in the XQJExecute example.