queryResource(service, "ids.xml", "<results>{/idref('id2')}</results>", 1);
}
@Test
public void externalVars() throws XMLDBException {
XQueryService service =
storeXMLStringAndGetQueryService("strings.xml", strings);
String query =
"declare variable $x external;" +
"$x";
CompiledExpression expr = service.compile(query);
//Do not declare the variable...
boolean exceptionThrown = false;
try {
service.execute(expr);
} catch (XMLDBException e) {
exceptionThrown = true;
}
assertTrue("Expected XPTY0002", exceptionThrown);
query =
"declare variable $local:string external;" +
"/test/string[. = $local:string]";
expr = service.compile(query);
service.declareVariable("local:string", "Hello");
ResourceSet result = service.execute(expr);
final XMLResource r = (XMLResource) result.getResource(0);
Node node = r.getContentAsDOM();
if (node.getNodeType() == Node.DOCUMENT_NODE) {
node = node.getFirstChild();
}
assertEquals("string", node.getNodeName());
//Instanciate a new service to prevent variable reuse
//TODO : consider auto-reset ?
service = storeXMLStringAndGetQueryService("strings.xml", strings);
query =
"declare variable $local:string as xs:string external;" +
"$local:string";
expr = service.compile(query);
//TODO : we should virtually pass any kind of value
service.declareVariable("local:string", new Integer(1));
String message = "";
try {
service.execute(expr);
System.out.println(query);
} catch (XMLDBException e) {
e.printStackTrace();
message = e.getMessage();
}
assertTrue(message.indexOf("XPTY0004") > -1);
service = storeXMLStringAndGetQueryService("strings.xml", strings);
query =
"declare variable $x as xs:integer external; " +
"$x";
expr = service.compile(query);
//TODO : we should virtually pass any kind of value
service.declareVariable("x", "1");
message = "";
try {
System.out.println(query);
service.execute(expr);
} catch (XMLDBException e) {
e.printStackTrace();
message = e.getMessage();
}
assertTrue(message.indexOf("XPTY0004") > -1);