Search in sources :

Example 6 with XQueryService

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;
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource)

Example 7 with XQueryService

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);
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService)

Example 8 with XQueryService

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());
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 9 with XQueryService

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();
    }
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet) CompiledExpression(org.xmldb.api.base.CompiledExpression)

Example 10 with XQueryService

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());
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Aggregations

XQueryService (org.xmldb.api.modules.XQueryService)129 EXistXQueryService (org.exist.xmldb.EXistXQueryService)71 XMLResource (org.xmldb.api.modules.XMLResource)33 ResourceSet (org.xmldb.api.base.ResourceSet)29 Test (org.junit.Test)28 Resource (org.xmldb.api.base.Resource)8 Node (org.w3c.dom.Node)6 EXistResource (org.exist.xmldb.EXistResource)5 Document (org.w3c.dom.Document)5 Collection (org.xmldb.api.base.Collection)5 Source (javax.xml.transform.Source)4 IndexQueryService (org.exist.xmldb.IndexQueryService)4 CompiledExpression (org.xmldb.api.base.CompiledExpression)4 XMLDBException (org.xmldb.api.base.XMLDBException)4 Diff (org.xmlunit.diff.Diff)4 Path (java.nio.file.Path)2 XmlRpcTest (org.exist.xmlrpc.XmlRpcTest)2 StringWriter (java.io.StringWriter)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1