Search in sources :

Example 31 with XQueryService

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

the class XPathQueryTest method strings.

@Test
public void strings() throws XMLDBException {
    final XQueryService service = storeXMLStringAndGetQueryService("strings.xml", strings);
    ResourceSet result = queryResource(service, "strings.xml", "substring(/test/string[1], 1, 5)", 1);
    assertEquals("Hello", result.getResource(0).getContent().toString());
    queryResource(service, "strings.xml", "/test/string[starts-with(string(.), 'Hello')]", 2);
    result = queryResource(service, "strings.xml", "count(/test/item/price)", 1, "Query should return an empty set (wrong document)");
    assertEquals("0", result.getResource(0).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 32 with XQueryService

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

the class XPathQueryTest method namespaces.

@Test
public void namespaces() throws XMLDBException {
    final XQueryService service = storeXMLStringAndGetQueryService("namespaces.xml", namespaces);
    service.setNamespace("t", "http://www.foo.com");
    service.setNamespace("c", "http://www.other.com");
    ResourceSet result = service.queryResource("namespaces.xml", "//t:section");
    assertEquals(1, result.getSize());
    result = service.queryResource("namespaces.xml", "/t:test//c:comment");
    assertEquals(1, result.getSize());
    result = service.queryResource("namespaces.xml", "//c:*");
    assertEquals(1, result.getSize());
    result = service.queryResource("namespaces.xml", "//*:comment");
    assertEquals(1, result.getSize());
    result = service.queryResource("namespaces.xml", "namespace-uri(//t:test)");
    assertEquals(1, result.getSize());
    assertEquals("http://www.foo.com", result.getResource(0).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 33 with XQueryService

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

the class XPathQueryTest method satisfies.

@Test
public void satisfies() throws XMLDBException {
    final XQueryService service = getQueryService();
    ResourceSet result = queryAndAssert(service, "every $foo in (1,2,3) satisfies" + "   let $bar := 'baz'" + "       return false() ", 1, "");
    assertEquals("satisfies + FLWR expression allways false 1", "false", result.getResource(0).getContent().toString());
    result = queryAndAssert(service, "declare function local:foo() { false() };" + "   every $bar in (1,2,3) satisfies" + "   local:foo()", 1, "");
    assertEquals("satisfies + FLWR expression allways false 2", "false", result.getResource(0).getContent().toString());
    String query = "every $x in () satisfies false()";
    result = queryAndAssert(service, query, 1, "");
    assertEquals(query, "true", result.getResource(0).getContent().toString());
    query = "some $x in () satisfies true()";
    result = queryAndAssert(service, query, 1, "");
    assertEquals(query, "false", result.getResource(0).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 34 with XQueryService

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

the class XPathQueryTest method convertToBoolean.

@Test
public void convertToBoolean() throws XMLDBException {
    final XQueryService service = getQueryService();
    ResourceSet result = queryAndAssert(service, "let $doc := <element attribute=''/>" + "return (" + "  <true>{boolean(($doc,2,3))}</true> ," + "  <true>{boolean(($doc/@*,2,3))}</true> ," + "  <true>{boolean(true())}</true> ," + "  <true>{boolean('test')}</true> ," + "  <true>{boolean(number(1))}</true> ," + "  <false>{boolean((0))}</false> ," + "  <false>{boolean(false())}</false> ," + "  <false>{boolean('')}</false> ," + "  <false>{boolean(number(0))}</false> ," + "  <false>{boolean(number('NaN'))}</false>" + ")", 10, "");
    for (int i = 0; i < 10; i++) {
        if (i < 5) {
            assertEquals("true " + (i + 1), "<true>true</true>", result.getResource(i).getContent().toString());
        } else {
            assertEquals("false " + (i + 1), "<false>false</false>", result.getResource(i).getContent().toString());
        }
    }
    boolean exceptionThrown = false;
    String message = "";
    try {
        queryAndAssert(service, "let $doc := <element attribute=''/>" + " return boolean( (1,2,$doc) )", 1, "");
    } catch (XMLDBException e) {
        exceptionThrown = true;
        message = e.getMessage();
    }
    assertTrue("Exception wanted: " + message, exceptionThrown);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 35 with XQueryService

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

the class XPathQueryTest method virtualNodeset_bug_wiki_3.

/**
 * Problem in VirtualNodeSet which return 2 attributes because it
 * computes every level
 * @see http://wiki.exist-db.org/space/XQueryBugs
 */
@Test
public void virtualNodeset_bug_wiki_3() throws XMLDBException {
    final String xQuery = "declare option exist:serialize \"method=xml indent=no\"; " + "let $node := (<c id=\"OK\"><b id=\"cool\"/></c>)" + "/descendant::*/attribute::id return <a>{$node}</a>";
    final XQueryService service = getQueryService();
    final ResourceSet rs = service.query(xQuery);
    assertEquals("VirtualNodesetBUG_wiki_3", 1, rs.getSize());
    assertEquals("VirtualNodesetBUG_wiki_3", "<a id=\"cool\"/>", rs.getResource(0).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

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