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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations