Search in sources :

Example 16 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class ValueIndexTest method indexScan.

@Test
public void indexScan() throws XMLDBException, URISyntaxException {
    configureCollection(CONFIG_PATH);
    String queryBody = "declare namespace f=\'http://exist-db.org/xquery/test\';\n" + "declare namespace mods='http://www.loc.gov/mods/v3';\n" + "import module namespace u=\'http://exist-db.org/xquery/util\';\n" + "\n" + "declare function f:term-callback($term as item(), $data as xs:int+)\n" + "as element()+ {\n" + "    <item>\n" + "        <term>{$term}</term>\n" + "        <frequency>{$data[1]}</frequency>\n" + "    </item>\n" + "};\n" + "\n";
    XPathQueryService service = storeXMLFileAndGetQueryService(ITEMS_FILENAME, ITEMS_FILE);
    String query = queryBody + "u:index-keys(//item/name, \'\', util:function(xs:QName(\'f:term-callback\'), 2), 1000)";
    ResourceSet result = service.query(query);
    for (ResourceIterator i = result.getIterator(); i.hasMoreResources(); ) {
        i.nextResource().getContent();
    }
    assertEquals(7, result.getSize());
    query = queryBody + "u:index-keys(//item/stock, 0, util:function(xs:QName(\'f:term-callback\'), 2), 1000)";
    result = service.query(query);
    for (ResourceIterator i = result.getIterator(); i.hasMoreResources(); ) {
        i.nextResource().getContent();
    }
    assertEquals(5, result.getSize());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) ResourceIterator(org.xmldb.api.base.ResourceIterator) Test(org.junit.Test)

Example 17 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class ValueIndexTest method strings.

@Test
public void strings() throws XMLDBException, URISyntaxException {
    configureCollection(CONFIG_PATH);
    XPathQueryService service = storeXMLFileAndGetQueryService(ITEMS_FILENAME, ITEMS_FILE);
    queryResource(service, ITEMS_FILENAME, "//item[@id = 'i2']", 1);
    queryResource(service, ITEMS_FILENAME, "//item[name = 'Racing Bicycle']", 1);
    queryResource(service, ITEMS_FILENAME, "//item[name > 'Racing Bicycle']", 4);
    queryResource(service, ITEMS_FILENAME, "//item[itemno = 3]", 1);
    queryResource(service, ITEMS_FILENAME, "//item[itemno eq 3]", 1);
    ResourceSet result = queryResource(service, ITEMS_FILENAME, "for $i in //item[stock <= 10] return $i/itemno", 5);
    for (long i = 0; i < result.getSize(); i++) {
        Resource res = result.getResource(i);
    }
    queryResource(service, ITEMS_FILENAME, "//item[stock > 20]", 1);
    queryResource(service, ITEMS_FILENAME, "declare namespace x=\"http://www.foo.com\"; //item[x:rating > 8.0]", 2);
    queryResource(service, ITEMS_FILENAME, "declare namespace xx=\"http://test.com\"; //item[@xx:test = 123]", 1);
    queryResource(service, ITEMS_FILENAME, "declare namespace xx=\"http://test.com\"; //item[@xx:test eq 123]", 1);
    queryResource(service, ITEMS_FILENAME, "//item[mixed = 'uneven']", 1);
    queryResource(service, ITEMS_FILENAME, "//item[mixed eq 'uneven']", 1);
    queryResource(service, ITEMS_FILENAME, "//item[mixed = 'external']", 1);
    queryResource(service, ITEMS_FILENAME, "//item[fn:matches(mixed, 'un.*')]", 2);
    queryResource(service, ITEMS_FILENAME, "//item[price/@specialprice = false()]", 2);
    queryResource(service, ITEMS_FILENAME, "//item[price/@specialprice = true()]", 1);
    queryResource(service, ITEMS_FILENAME, "//item[price/@specialprice eq true()]", 1);
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) ResourceSet(org.xmldb.api.base.ResourceSet) Test(org.junit.Test)

Example 18 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class ValueIndexTest method qnameIndex.

@Test
public void qnameIndex() throws XMLDBException, URISyntaxException {
    configureCollection(CONFIG_QNAME);
    XPathQueryService service = storeXMLFileAndGetQueryService(ITEMS_FILENAME, ITEMS_FILE);
    queryResource(service, ITEMS_FILENAME, "//((#exist:optimize#) { item[stock = 10] })", 1);
    queryResource(service, ITEMS_FILENAME, "//((#exist:optimize#) { item[stock > 20] })", 1);
    queryResource(service, ITEMS_FILENAME, "//((#exist:optimize#) { item[stock < 16] })", 6);
    queryResource(service, ITEMS_FILENAME, "declare namespace x=\"http://www.foo.com\"; " + "//((#exist:optimize#) { item[x:rating > 8.0] })", 2);
    queryResource(service, ITEMS_FILENAME, "//((#exist:optimize#) { item[mixed = 'uneven'] })", 1);
    queryResource(service, ITEMS_FILENAME, "//((#exist:optimize#) { item[mixed = 'external'] })", 1);
    queryResource(service, ITEMS_FILENAME, "//((#exist:optimize#) { item[@id = 'i1'] })", 1);
    queryResource(service, ITEMS_FILENAME, "declare namespace xx=\"http://test.com\";" + "//((#exist:optimize#) { item[@xx:test = 123] })", 1);
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Test(org.junit.Test)

Example 19 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class ValueIndexTest method updates.

@Test
public void updates() throws Exception {
    configureCollection(CONFIG_PATH);
    storeXMLFileAndGetQueryService(ITEMS_FILENAME, ITEMS_FILE);
    for (int i = 100; i <= 150; i++) {
        String append = "<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" + "   <xu:append select=\"/items\">" + "       <item id=\"i" + i + "\">" + "           <itemno>" + i + "</itemno>" + "           <name>New Item</name>" + "           <price>55.50</price>" + "       </item>" + "   </xu:append>" + "</xu:modifications>";
        String remove = "<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" + "   <xu:remove select=\"/items/item[itemno=" + i + "]\"/>" + "</xu:modifications>";
        XPathQueryService query = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
        XUpdateQueryService update = (XUpdateQueryService) testCollection.getService("XUpdateQueryService", "1.0");
        long mods = update.updateResource(ITEMS_FILENAME, append);
        assertEquals(mods, 1);
        queryResource(query, ITEMS_FILENAME, "//item[price = 55.50]", 1);
        queryResource(query, ITEMS_FILENAME, "//item[@id = 'i" + i + "']", 1);
        mods = update.updateResource(ITEMS_FILENAME, remove);
        assertEquals(mods, 1);
        queryResource(query, ITEMS_FILENAME, "//item[itemno = " + i + "]", 0);
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) Test(org.junit.Test)

Example 20 with XPathQueryService

use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.

the class ValueIndexTest method pathIndexStringMatchingFunctions.

/*
     * Bugfix
     *
     * These following two tests were put in place to demonstrate bugs in how the index matching functions work,
     * as a precursor to a fix, which was committed 2/3/2010. The issue was that the 2nd parameter
	 * to the string matching functions was incorrectly interpreted as a regex, which causd an exception
	 * to be thrown if the string included characters that have special meaning in a regex, eg. '*' for contains.
	 *
	 * andrzej@chaeron.com
     */
@Test
public void pathIndexStringMatchingFunctions() throws XMLDBException {
    configureCollection(CONFIG_PATH);
    XMLResource resource = (XMLResource) testCollection.createResource("mondial-test.xml", "XMLResource");
    resource.setContent(CITY);
    testCollection.storeResource(resource);
    XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
    queryResource(service, "mondial-test.xml", "//city[ starts-with( name, '^*' ) ]", 0);
    queryResource(service, "mondial-test.xml", "//city[ contains( name, '^*' ) ]", 0);
    queryResource(service, "mondial-test.xml", "//city[ ends-with( name, '^*' ) ]", 0);
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Aggregations

XPathQueryService (org.xmldb.api.modules.XPathQueryService)148 ResourceSet (org.xmldb.api.base.ResourceSet)123 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)92 XMLResource (org.xmldb.api.modules.XMLResource)60 Collection (org.xmldb.api.base.Collection)31 Test (org.junit.Test)22 Resource (org.xmldb.api.base.Resource)18 XMLDBException (org.xmldb.api.base.XMLDBException)14 EXistResource (org.exist.xmldb.EXistResource)10 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)9 XUpdateQueryService (org.xmldb.api.modules.XUpdateQueryService)8 IndexQueryService (org.exist.xmldb.IndexQueryService)6 IOException (java.io.IOException)3 ResourceIterator (org.xmldb.api.base.ResourceIterator)3 BinaryResource (org.xmldb.api.modules.BinaryResource)3 MalformedURLException (java.net.MalformedURLException)2 Path (java.nio.file.Path)2 Node (org.w3c.dom.Node)2 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1