Search in sources :

Example 96 with XQueryService

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

the class XPathQueryTest method predicates2.

@Test
public void predicates2() throws XMLDBException, IOException, SAXException {
    final String numbers = "<test>" + "<item id='1' type='alphanum'><price>5.6</price><stock>22</stock></item>" + "<item id='2'><price>7.4</price><stock>43</stock></item>" + "<item id='3'><price>18.4</price><stock>5</stock></item>" + "<item id='4'><price>65.54</price><stock>16</stock></item>" + "</test>";
    final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
    service.setProperty(OutputKeys.INDENT, "no");
    String query = "let $t := <test>" + "<a> <s>A</s> 1 </a>" + "<a> <s>Z</s> 2 </a>" + "<a> <s>B</s> 3 </a>" + "<a> <s>Z</s> 4 </a>" + "<a> <s>C</s> 5 </a>" + "<a> <s>Z</s> 6 </a>" + "</test>" + "return $t//a[s='Z' and preceding-sibling::*[1]/s='B']";
    ResourceSet result = queryResource(service, "numbers.xml", query, 1);
    assertXMLEqual("<a><s>Z</s> 4 </a>", result.getResource(0).getContent().toString());
    query = "let $t := <test>" + "<a> <s>A</s> 1 </a>" + "<a> <s>Z</s> 2 </a>" + "<a> <s>B</s> 3 </a>" + "<a> <s>Z</s> 4 </a>" + "<a> <s>C</s> 5 </a>" + "<a> <s>Z</s> 6 </a>" + "</test>" + "return $t//a[s='Z' and ./preceding-sibling::*[1]/s='B']";
    result = queryResource(service, "numbers.xml", query, 1);
    assertXMLEqual("<a><s>Z</s> 4 </a>", result.getResource(0).getContent().toString());
    query = "let $doc := <doc><rec n='1'><a>first</a><b>second</b></rec>" + "<rec n='2'><a>first</a><b>third</b></rec></doc> " + "return $doc//rec[fn:not(b = 'second') and (./a = 'first')]";
    result = queryResource(service, "numbers.xml", query, 1);
    assertXMLEqual("<rec n=\"2\"><a>first</a><b>third</b></rec>", result.getResource(0).getContent().toString());
    query = "let $doc := <doc><a b='c' d='e'/></doc> " + "return $doc/a[$doc/a/@b or $doc/a/@d]";
    result = queryResource(service, "numbers.xml", query, 1);
    assertXMLEqual("<a b=\"c\" d=\"e\"/>", result.getResource(0).getContent().toString());
    query = "let $x := <a><b><x/><x/></b><b><x/></b></a>" + "return $x//b[count(x) = 2]";
    result = queryResource(service, "numbers.xml", query, 1);
    assertXMLEqual("<b><x/><x/></b>", result.getResource(0).getContent().toString());
    // Boolean evaluation for "." (atomic sequence)
    query = "(1,2,3)[xs:decimal(.)]";
    result = queryResource(service, "numbers.xml", query, 3);
    query = "(1,2,3)[number()]";
    result = queryResource(service, "numbers.xml", query, 3);
    query = " 	let $c := (<a/>,<b/>), $i := 1 return $c[$i]";
    result = queryResource(service, "numbers.xml", query, 1);
    assertXMLEqual("<a/>", result.getResource(0).getContent().toString());
    query = "(1,2,3)[position() = last()]";
    result = queryResource(service, "numbers.xml", query, 1);
    assertEquals("3", result.getResource(0).getContent().toString());
    query = "(1,2,3)[max(.)]";
    result = queryResource(service, "numbers.xml", query, 3);
    query = "(1,2,3)[max(.[. gt 1])]";
    result = queryResource(service, "numbers.xml", query, 2);
    assertEquals("2", result.getResource(0).getContent().toString());
    assertEquals("3", result.getResource(1).getContent().toString());
    query = "(1,2,3)[.]";
    result = queryResource(service, "numbers.xml", query, 3);
    query = "declare function local:f ($n) { " + "$n " + "}; " + " " + "declare function local:g( $n ) { " + "('OK','Fine','Wrong') [local:f($n) + 1 ] " + "} ; " + " " + "declare function local:h( $n ) { " + "('OK','Fine','Wrong') [local:f($n) ] " + "} ; " + " " + "declare function local:j( $n ) { " + "let $m := local:f($n) " + "return " + "('OK','Fine','Wrong') [$m + 1 ] " + "} ; " + " " + "declare function local:k ( $n ) { " + "('OK','Fine','Wrong') [ $n + 1 ] " + "} ; " + " " + "local:f(1),local:g(1), local:h(1), local:j(1), local:k(1) ";
    result = queryResource(service, "numbers.xml", query, 5);
    assertEquals("1", result.getResource(0).getContent().toString());
    assertEquals("Fine", result.getResource(1).getContent().toString());
    assertEquals("OK", result.getResource(2).getContent().toString());
    assertEquals("Fine", result.getResource(3).getContent().toString());
    assertEquals("Fine", result.getResource(4).getContent().toString());
    // The collection doesn't exist : let's see how the query behaves with empty sequences
    query = "let $checkDate := xs:date(adjust-date-to-timezone(current-date(), ())) " + "let $collection := if (xmldb:collection-available(\"/db/lease\")) then collection(\"/db/lease\") else () " + "for $x in " + "$collection//Lease/Events/Type/Event[(When/Date<=$checkDate or " + "When/EstimateDate<=$checkDate) and not(Status='Complete')] " + "return $x";
    result = queryResource(service, "numbers.xml", query, 0);
    query = "let $res := <test><element name='A'/><element name='B'/></test> " + "return " + "for $name in ('A', 'B') return " + "$res/element[@name=$name][1]";
    result = queryResource(service, "numbers.xml", query, 2);
    assertXMLEqual("<element name='A'/>", result.getResource(0).getContent().toString());
    assertXMLEqual("<element name='B'/>", result.getResource(1).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 97 with XQueryService

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

the class XPathQueryTest method predicates.

@Test
public void predicates() throws XMLDBException, IOException, SAXException {
    final String numbers = "<test>" + "<item id='1' type='alphanum'><price>5.6</price><stock>22</stock></item>" + "<item id='2'><price>7.4</price><stock>43</stock></item>" + "<item id='3'><price>18.4</price><stock>5</stock></item>" + "<item id='4'><price>65.54</price><stock>16</stock></item>" + "</test>";
    final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
    service.setProperty(OutputKeys.INDENT, "no");
    ResourceSet result = queryResource(service, "numbers.xml", "/test/item[2]/price/text()", 1);
    assertEquals("7.4", result.getResource(0).getContent().toString());
    result = queryResource(service, "numbers.xml", "/test/item[5]", 0);
    result = queryResource(service, "numbers.xml", "/test/item[@id='4'][1]/price[1]/text()", 1);
    assertEquals("65.54", result.getResource(0).getContent().toString());
    result = queryResource(service, "numbers.xml", "for $i in //item return " + "<item>{$i/price, $i/stock}</item>", 4);
    assertXMLEqual("<item><price>5.6</price><stock>22</stock></item>", result.getResource(0).getContent().toString());
    assertXMLEqual("<item><price>65.54</price><stock>16</stock></item>", result.getResource(3).getContent().toString());
    // test positional predicates
    result = queryResource(service, "numbers.xml", "/test/node()[2]", 1);
    assertXMLEqual("<item id='2'><price>7.4</price><stock>43</stock></item>", result.getResource(0).getContent().toString());
    result = queryResource(service, "numbers.xml", "/test/element()[2]", 1);
    assertXMLEqual("<item id='2'><price>7.4</price><stock>43</stock></item>", result.getResource(0).getContent().toString());
    // positional predicate on sequence of atomic values
    result = queryResource(service, "numbers.xml", "('test', 'pass')[2]", 1);
    assertEquals("pass", result.getResource(0).getContent().toString());
    result = queryResource(service, "numbers.xml", "let $credentials := ('test', 'pass') let $user := $credentials[1] return $user", 1);
    assertEquals("test", result.getResource(0).getContent().toString());
    result = queryResource(service, "numbers.xml", "let $credentials := ('test', 'pass') let $user := $credentials[2] return $user", 1);
    assertEquals("pass", result.getResource(0).getContent().toString());
    result = queryResource(service, "numbers.xml", "let $els := <els><el>text1</el><el>text2</el></els> return $els/el[xs:string(.) eq 'text1'] ", 1);
    assertEquals("<el>text1</el>", result.getResource(0).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 98 with XQueryService

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

the class XPathQueryTest method predicate_bug_wiki_1.

/**
 * In Predicate.java, the contextSet and the outerSequence.toNodeSet()
 * documents are different so that no match can occur.
 *
 * @see http://wiki.exist-db.org/space/XQueryBugs
 */
@Test
public void predicate_bug_wiki_1() throws XMLDBException {
    final String xQuery = "let $dum := <dummy><el>1</el><el>2</el></dummy> return $dum/el[2]";
    final XQueryService service = getQueryService();
    final ResourceSet rs = service.query(xQuery);
    assertEquals("Predicate bug wiki_1", 1, rs.getSize());
    assertEquals("Predicate bug wiki_1", "<el>2</el>", rs.getResource(0).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 99 with XQueryService

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

the class XPathQueryTest method idRefs_memtree.

@Ignore("Not yet supported in eXist")
@Test
public void idRefs_memtree() throws XMLDBException {
    final XQueryService service = getQueryService();
    ResourceSet result = service.query("document {" + ids_content + "}/idref('id2')");
    assertEquals(1, result.getSize());
    result = service.query("document {" + ids_content + "}/idref('id1')");
    assertEquals(2, result.getSize());
    result = service.query("document {" + ids_content + "}/idref(('id2', 'id1'))");
    assertEquals(3, result.getSize());
    result = service.query("let $doc := document {" + ids_content + "} return <results>{$doc/idref('id2')}</results>");
    assertEquals(1, result.getSize());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 100 with XQueryService

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

the class XPathQueryTest method predicates_bug1537355.

/**
 * @see http://sourceforge.net/tracker/index.php?func=detail&aid=1537355&group_id=17691&atid=117691
 */
@Test
public void predicates_bug1537355() throws XMLDBException {
    final String xQuery = "let $one := 1 return (1, 2, 3)[$one + 1]";
    final XQueryService service = getQueryService();
    final ResourceSet rs = service.query(xQuery);
    assertEquals("SFBUG 1537355 nr of results", 1, rs.getSize());
    assertEquals("SFBUG 1537355 result", "2", 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