Search in sources :

Example 91 with ResourceSet

use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.

the class ValueAppendAction method query.

private void query(final XPathQueryService service) throws XMLDBException {
    ResourceSet result = service.queryResource(resourceName, "/items/item[value = 44.53]");
    assertEquals(1, result.getSize());
    result = service.queryResource(resourceName, "/items/item[@id=1]/name[.='abcdefg']/text()");
    assertEquals(1, result.getSize());
    assertEquals("abcdefg", result.getResource(0).getContent().toString());
}
Also used : ResourceSet(org.xmldb.api.base.ResourceSet)

Example 92 with ResourceSet

use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.

the class DocumentUpdateTest method execQuery.

private String execQuery(String query) throws XMLDBException {
    XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals(result.getSize(), 1);
    return result.getResource(0).getContent().toString();
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 93 with ResourceSet

use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.

the class DocumentUpdateTest method update.

/**
 * Test if the doc, collection and document functions are correctly
 * notified upon document updates. Call a function once on the empty collection,
 * then call it again after a document was added, and compare the results.
 */
@Test
public void update() throws XMLDBException {
    String imports = "import module namespace xdb='http://exist-db.org/xquery/xmldb';\n" + "import module namespace util='http://exist-db.org/xquery/util';\n";
    // TEST 1: doc() function
    String query = imports + "declare function local:get-doc($path as xs:string) {\n" + "    if (doc-available($path)) then doc($path) else ()\n" + "};\n" + "let $col := xdb:create-collection('/db', 'testup')\n" + "let $path := '/db/testup/test1.xml'\n" + "let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" + "let $d1 := local:get-doc($path)\n" + "let $remove := xdb:remove('/db/testup', 'test1.xml')\n" + "return string-join((string(count(local:get-doc($path))), string(doc-available($path))), ' ')";
    String result = execQuery(query);
    assertEquals(result, "0 false");
    // TEST 2: doc()
    query = imports + "declare function local:get-doc($path as xs:string) {\n" + "    if (doc-available($path)) then doc($path) else ()\n" + "};\n" + "let $col := xdb:create-collection('/db', 'testup')\n" + "let $path := '/db/testup/test1.xml'\n" + "let $d1 := local:get-doc($path)\n" + "let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" + "return string-join((string(count(local:get-doc($path))), string(doc-available($path))), ' ')";
    result = execQuery(query);
    assertEquals(result, "1 true");
    // TEST 3: collection()
    query = imports + "declare function local:xpath($collection as xs:string) {\n" + "    for $c in collection($collection) return $c//n\n" + "};\n" + "let $col := xdb:create-collection('/db', 'testup')\n" + "let $path := '/db/testup'\n" + "let $d1 := local:xpath($path)//n/text()\n" + "let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" + "return local:xpath($path)/text()";
    result = execQuery(query);
    assertEquals(result, "1");
    // TEST 4: 'update insert' statement
    query = imports + "declare function local:xpath($collection as xs:string) {\n" + "    collection($collection)\n" + "};\n" + "let $col := xdb:create-collection('/db', 'testup')\n" + "let $path := '/db/testup'\n" + "let $d1 := local:xpath($path)//n\n" + "let $doc := xdb:store($col, 'test1.xml', <test><n>1</n></test>)\n" + "return (\n" + "	update insert <n>2</n> into collection($path)/test,\n" + "	count(local:xpath($path)//n)\n" + ")";
    result = execQuery(query);
    assertEquals(result, "2");
    // TEST 5: 'update replace' statement
    query = imports + "let $doc := xdb:store('/db', 'test1.xml', " + "<test> " + "<link href=\"features\"/> " + "(: it works with only 1 link :) " + "<link href=\"features/test\"/> " + "</test>) " + "let $links := doc($doc)/test/link/@href " + "return " + "for $link in $links " + "return ( " + "update replace $link with \"123\", " + "(: without the output on the next line, it works :) " + "xs:string($link) " + ")";
    XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
    ResourceSet r = service.query(query);
    assertEquals(r.getSize(), 2);
    assertEquals(r.getResource(0).getContent().toString(), "123");
    assertEquals(r.getResource(1).getContent().toString(), "123");
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 94 with ResourceSet

use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.

the class AbstractDescendantOrSelfNodeKindTest method attributeCount.

@Test
public void attributeCount() throws XMLDBException {
    final ResourceSet result = executeQueryOnDoc("count($doc//attribute())");
    assertEquals(1, result.getSize());
    assertEquals(4, Integer.parseInt((String) result.getResource(0).getContent()));
}
Also used : ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 95 with ResourceSet

use of org.xmldb.api.base.ResourceSet in project exist by eXist-db.

the class AbstractDescendantOrSelfNodeKindTest method commentCount.

@Test
public void commentCount() throws XMLDBException {
    final ResourceSet result = executeQueryOnDoc("count($doc//comment())");
    assertEquals(1, result.getSize());
    assertEquals(1, Integer.parseInt((String) result.getResource(0).getContent()));
}
Also used : ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Aggregations

ResourceSet (org.xmldb.api.base.ResourceSet)383 XPathQueryService (org.xmldb.api.modules.XPathQueryService)123 Test (org.junit.Test)117 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)100 XMLResource (org.xmldb.api.modules.XMLResource)80 Collection (org.xmldb.api.base.Collection)55 Resource (org.xmldb.api.base.Resource)45 XQueryService (org.xmldb.api.modules.XQueryService)29 XMLDBException (org.xmldb.api.base.XMLDBException)23 EXistResource (org.exist.xmldb.EXistResource)20 BinaryResource (org.xmldb.api.modules.BinaryResource)17 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)14 Node (org.w3c.dom.Node)11 IndexQueryService (org.exist.xmldb.IndexQueryService)9 Diff (org.xmlunit.diff.Diff)9 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)8 UserManagementService (org.exist.xmldb.UserManagementService)8 Source (javax.xml.transform.Source)7 ResourceIterator (org.xmldb.api.base.ResourceIterator)7 XUpdateQueryService (org.xmldb.api.modules.XUpdateQueryService)6