Search in sources :

Example 86 with XPathQueryService

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

the class XQueryTest method evalLoosesContext_1740891.

/**
 * @see http://sourceforge.net/support/tracker.php?aid=1740891
 */
@Test
public void evalLoosesContext_1740891() throws XMLDBException {
    String module = "module namespace tst = \"urn:test\"; " + "declare namespace util = \"http://exist-db.org/xquery/util\";" + "declare function tst:bar() as element(Bar)* { " + "let $foo := <Foo><Bar/><Bar/><Bar/></Foo> " + "let $query := \"$foo/Bar\" " + "let $bar := util:eval($query) " + "return $bar };";
    String module_name = "module.xqy";
    Resource doc;
    // Store module
    Collection testCollection = getTestCollection();
    doc = testCollection.createResource(module_name, "BinaryResource");
    doc.setContent(module);
    ((EXistResource) doc).setMimeType("application/xquery");
    testCollection.storeResource(doc);
    String query = "import module namespace tst = \"urn:test\"" + "at \"xmldb:exist:///db/test/module.xqy\"; " + "tst:bar()";
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals(3, result.getSize());
    assertEquals("First", "<Bar/>", result.getResource(0).getContent().toString());
    assertEquals("Second", "<Bar/>", result.getResource(1).getContent().toString());
    assertEquals("Third", "<Bar/>", result.getResource(2).getContent().toString());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 87 with XPathQueryService

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

the class XQueryTest method functionDoc.

@Test
public void functionDoc() throws XMLDBException, IOException, SAXException {
    ResourceSet result;
    String query;
    @SuppressWarnings("unused") boolean exceptionThrown;
    @SuppressWarnings("unused") String message;
    XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
    query = "doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "')";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
    DetailedDiff d = new DetailedDiff(compareXML(numbers, n.toString()));
    assertEquals(0, d.getAllDifferences().size());
    // ignore eXist namespace's attributes
    // assertEquals(1, d.getAllDifferences().size());
    query = "let $v := ()\n" + "return doc($v)";
    result = service.query(query);
    assertEquals("XQuery: " + query, 0, result.getSize());
    query = "doc('" + XmldbURI.ROOT_COLLECTION + "/test/dummy" + NUMBERS_XML + "')";
    try {
        exceptionThrown = false;
        result = service.query(query);
    } catch (XMLDBException e) {
        exceptionThrown = true;
        message = e.getMessage();
    }
    // TODO : to be decided !
    // assertTrue(exceptionThrown);
    assertEquals(0, result.getSize());
    query = "doc-available('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "')";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "true", result.getResource(0).getContent());
    query = "let $v := ()\n" + "return doc-available($v)";
    result = service.query(query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "false", result.getResource(0).getContent());
    query = "doc-available('" + XmldbURI.ROOT_COLLECTION + "/test/dummy" + NUMBERS_XML + "')";
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertEquals("XQuery: " + query, "false", result.getResource(0).getContent());
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) Node(org.w3c.dom.Node) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 88 with XPathQueryService

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

the class XQueryTest method attribute_1691177.

/**
 * @see http://sourceforge.net/tracker/index.php?func=detail&aid=1691177&group_id=17691&atid=117691
 */
@Test
public void attribute_1691177() throws XMLDBException {
    String query = "declare namespace xmldb = \"http://exist-db.org/xquery/xmldb\"; " + "let $uri := xmldb:store(\"/db\", \"insertAttribDoc.xml\", <C/>) " + "let $node := doc($uri)/element() " + "let $attrib := <Value f=\"ATTRIB VALUE\"/>/@* " + "return update insert $attrib into $node  ";
    XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
    ResourceSet result = service.query(query);
    assertEquals("XQuery: " + query, 0, result.getSize());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 89 with XPathQueryService

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

the class ValueIndexByQNameTest method strings.

@Test
@Override
public void strings() throws XMLDBException, URISyntaxException {
    configureCollection(config);
    XPathQueryService service = storeXMLFileAndGetQueryService(ITEMS_FILENAME, ITEMS_FILE);
    // queryResource(service, "items.xml", "//item[name > 'Racing Bicycle']", 4 );
    queryResource(service, "items.xml", "util:qname-index-lookup( xs:QName('name'), 'Racing Bicycle' ) / parent::item", 1);
    queryResource(service, "items.xml", "util:qname-index-lookup( xs:QName('itemno'), 3) / parent::item", 1);
    queryResource(service, "items.xml", "declare namespace xx='http://test.com'; " + "util:qname-index-lookup( xs:QName('xx:test'), 123, false() )", 1);
    // queryResource(service, "items.xml", "//item[name &= 'Racing Bicycle']", 1);
    // queryResource(service, "items.xml", "//item[mixed = 'uneven']", 1);
    queryResource(service, "items.xml", "util:qname-index-lookup( xs:QName('mixed'), 'external' )", 1);
// queryResource(service, "items.xml", "//item[fn:matches(mixed, 'un.*')]", 2);
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Test(org.junit.Test)

Example 90 with XPathQueryService

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

the class RemoveAppendTest method testRemoveAppend.

@Ignore
@Test
public void testRemoveAppend() throws Exception {
    XUpdateQueryService service = (XUpdateQueryService) testCol.getService("XUpdateQueryService", "1.0");
    XPathQueryService query = (XPathQueryService) testCol.getService("XPathQueryService", "1.0");
    for (int i = 1; i < 1000; i++) {
        int which = rand.nextInt(ITEM_COUNT) + 1;
        insert(service, which);
        remove(service, which);
        ResourceSet result = query.query("/test/item[@id='" + which + "']");
        assertEquals(result.getSize(), 1);
        result.getResource(0).getContent();
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet)

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