variables in simple XQuery syntax queries
588792Jul 19 2007 — edited Jul 26 2007Hi,
I'm experiencing a problem with using variables in simple queries and have run out of ideas or documents to read.
I'm running version 2.3.10 on a MacOSX platform and I'm using the Java API. The system is functioning and I'm able to do queries without variable OK.
The problem:
The examples indicated in the document http://www.oracle.com/technology/documentation/berkeley-db/xml/gsg_xml/java/dbxmlqueries.html#queries
suggest that I should be able to do something like this:
<pre>
String queryString = "collection('container1')//records/book[title=$myVar]";
queryContext.setVariableValue("varName",new XmlValue(XmlValue.STRING,"variable value");
queryExpression = xmlManager.prepare(queryString,queryContext);
queryExpresson.execute(queryContext);
</pre>
Running this gives an error:
<pre>
Error: Variable :myvar does not exist [err:XPST0008], <query>:1:84, errcode = XPATH_PARSER_ERROR
</pre>
caught at the point of compiling the XmlQueryExpression.
I have read this forum thoroughly and have tried various seemingly related suggestions such as declaring an external variable in the query syntax. So if I run the same code but prepending the variable declaration to the query syntax like this:
<pre>
String queryString = "declare variable $myvar as xs:string external;collection('container1')//records/book[title=$myVar]";
</pre>
then this generates the following error:
<pre>
Error: A value for the external variable 'myvar' has not been provided [err:XPTY0002], <query>:1:1, errcode = XPATH_EVALUATION_ERROR
</pre>
I have checked that the queryContext.setVariableValue() function is setting the variable by invoking queryContext.getVariableValue.
It appears to me that the XmlQueryExpression simply ignores the variables set in the XmlQueryContext.
Am I missing something here? Can someone please advise?
Thanks.