use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class NodeTypeTest method load.
/**
* Loads the xml document identified by <code>document</code> from the database.
*
* @param document the document to load
*/
@SuppressWarnings("unused")
private Node load(final String document) throws XMLDBException {
final StringBuilder query = new StringBuilder();
query.append("let $result := doc(string-join(('" + XmldbURI.ROOT_COLLECTION + "', $document), '/'))");
query.append("return ($result)");
final XQueryService service = (XQueryService) server.getRoot().getService("XQueryService", "1.0");
service.declareVariable("document", document);
final CompiledExpression cQuery = service.compile(query.toString());
final ResourceSet set = service.execute(cQuery);
if (set != null && set.getSize() > 0) {
return ((XMLResource) set.getIterator().nextResource()).getContentAsDOM();
}
return null;
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class NodeTypeTest method store.
/**
* Stores the given xml fragment into the database.
*
* @param xml the xml document
* @param document the document name
*/
private final void store(final String xml, final String document) throws XMLDBException {
final StringBuilder query = new StringBuilder();
query.append("declare namespace xmldb='http://exist-db.org/xquery/xmldb';");
query.append("let $isLoggedIn := xmldb:login('" + XmldbURI.ROOT_COLLECTION_URI + "', '" + TestUtils.ADMIN_DB_USER + "', '" + TestUtils.ADMIN_DB_USER + "'),");
query.append("$doc := xmldb:store('" + XmldbURI.ROOT_COLLECTION + "', $document, $data)");
query.append("return <result/>");
final XQueryService service = (XQueryService) server.getRoot().getService("XQueryService", "1.0");
service.declareVariable("document", document);
service.declareVariable("data", xml);
final CompiledExpression cQuery = service.compile(query.toString());
service.execute(cQuery);
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class OptimizerTest method execute.
private void execute(String query, boolean optimize, String message, long expected) throws XMLDBException {
XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
if (optimize) {
query = NAMESPACES + OPTIMIZE + query;
} else {
query = NAMESPACES + NO_OPTIMIZE + query;
}
ResourceSet result = service.query(query);
assertEquals(message, expected, result.getSize());
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class StoredModuleTest method testQuery.
@Test
public void testQuery() throws Exception {
Collection c = createCollection("test");
writeModule(c, "test.xqm", MODULE);
String query = "import module namespace itg-modules = \"http://localhost:80/itg/xquery\" at " + "\"xmldb:exist://" + XmldbURI.ROOT_COLLECTION + "/test/test.xqm\"; itg-modules:check-coll()";
String[] cols = { "one", "two", "three" };
final XQueryService xqService = (XQueryService) existEmbeddedServer.getRoot().getService("XQueryService", "1.0");
xqService.setNamespace("itg-modules", "http://localhost:80/itg/xquery");
CompiledExpression compiledQuery = xqService.compile(query);
for (int i = 0; i < cols.length; i++) {
xqService.declareVariable("itg-modules:coll", cols[i]);
ResourceSet result = xqService.execute(compiledQuery);
result.getResource(0).getContent();
}
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class NamespaceUpdateTest method updateAttribute.
@Test
public void updateAttribute() throws XMLDBException {
XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
String query = "declare namespace t='http://www.foo.com';\n" + "<test xmlns='http://www.foo.com'>\n" + "{\n" + " update insert attribute { 'ID' } { 'myid' } into /t:test\n" + "}\n" + "</test>";
service.query(query);
query = "declare namespace t='http://www.foo.com';\n" + "/t:test/@ID/string(.)";
ResourceSet result = service.query(query);
assertEquals(1, result.getSize());
assertEquals("myid", result.getResource(0).getContent().toString());
}
Aggregations