use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method starAxisConstraints.
@Test
public void starAxisConstraints() throws XMLDBException {
final XQueryService service = storeXMLStringAndGetQueryService("namespaces.xml", namespaces);
service.setNamespace("t", "http://www.foo.com");
String query = "// t:title/text() [ . != 'aaaa' ]";
ResourceSet result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
query = "/t:test/*:section[contains(., 'comment')]";
result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
query = "/t:test/t:*[contains(., 'comment')]";
result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
query = "/t:test/t:section[contains(., 'comment')]";
result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
query = "/t:test/t:section/*[contains(., 'comment')]";
result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
query = "/ * / * [ t:title ]";
result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
query = "/ t:test / t:section [ t:title ]";
result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
query = "/ t:test / t:section";
result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method position.
@Test
public void position() throws XMLDBException, IOException, SAXException {
final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
queryResource(service, "numbers.xml", "//item[position() = 3]", 1);
queryResource(service, "numbers.xml", "//item[position() < 3]", 2);
queryResource(service, "numbers.xml", "//item[position() <= 3]", 3);
queryResource(service, "numbers.xml", "//item[position() > 3]", 1);
queryResource(service, "numbers.xml", "//item[position() >= 3]", 2);
queryResource(service, "numbers.xml", "//item[position() eq 3]", 1);
queryResource(service, "numbers.xml", "//item[position() lt 3]", 2);
queryResource(service, "numbers.xml", "//item[position() le 3]", 3);
queryResource(service, "numbers.xml", "//item[position() gt 3]", 1);
queryResource(service, "numbers.xml", "//item[position() ge 3]", 2);
queryResource(service, "numbers.xml", "//item[last() - 1]", 1);
queryResource(service, "numbers.xml", "//item[count(('a','b')) - 1]", 1);
String query = "for $a in (<a/>, <b/>, <c/>) return $a/position()";
ResourceSet result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 3, result.getSize());
XMLResource resource = (XMLResource) result.getResource(0);
assertEquals("XPath: " + query, "1", resource.getContent().toString());
resource = (XMLResource) result.getResource(1);
assertEquals("XPath: " + query, "1", resource.getContent().toString());
resource = (XMLResource) result.getResource(2);
assertEquals("XPath: " + query, "1", resource.getContent().toString());
query = "declare variable $doc { <root>" + "<a>1</a><a>2</a><a>3</a><a>4</a><a>5</a><a>6</a><a>7</a>" + "</root> }; " + "(for $x in $doc/a return $x)[position() mod 3 = 2]";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 2, result.getSize());
query = "declare variable $doc { <root>" + "<a>1</a><a>2</a><a>3</a><a>4</a><a>5</a><a>6</a><a>7</a>" + "</root> }; " + "for $x in $doc/a return $x[position() mod 3 = 2]";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 0, result.getSize());
query = "declare variable $doc { <root>" + "<a>1</a><a>2</a><a>3</a><a>4</a><a>5</a><a>6</a><a>7</a>" + "</root> }; " + "for $x in $doc/a[position() mod 3 = 2] return $x";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 2, result.getSize());
query = "let $test := <test><a> a </a><a>a</a></test>" + "return distinct-values($test/a/normalize-space(.))";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
resource = (XMLResource) result.getResource(0);
assertEquals("XPath: " + query, "a", resource.getContent().toString());
query = "let $doc := document {<a><b n='1'/><b n='2'/></a>} " + "return $doc//b/(if (@n = '1') then position() else ())";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
assertEquals("1", result.getResource(0).getContent().toString());
// Try a second time to see if the position is reset
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
assertEquals("1", result.getResource(0).getContent().toString());
query = "let $doc := document {<a><b/></a>} " + "return $doc/a[1] [b[1]]";
service.setProperty(OutputKeys.INDENT, "no");
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
assertXMLEqual("<a><b/></a>", result.getResource(0).getContent().toString());
// TODO : make this work ! It currently returns some content
// query = "let $doc := document {<a><b><c>1</c></b><b><c>a</c></b></a>} " +
// "return $doc/a[b[position() = 2]/c[.='1']]";
// result = service.queryResource("numbers.xml", query);
// assertEquals("XPath: " + query, 0, result.getSize());
// TODO: make this work ! It currently returns 1
// query = "let $a := ('a', 'b', 'c') for $b in $a[position()] return <blah>{$b}</blah>";
// result = service.queryResource("numbers.xml", query);
// assertEquals("XPath: " + query, 3, result.getSize());
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method union.
@Test
public void union() throws XMLDBException {
final XQueryService service = getQueryService();
String query = "() union ()";
queryAndAssert(service, query, 0, "");
String message = "";
try {
query = "() union (1)";
queryAndAssert(service, query, 0, "");
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("XPTY0004") > -1);
message = "";
try {
query = "(1) union ()";
queryAndAssert(service, query, 0, "");
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("XPTY0004") > -1);
query = "<a/> union ()";
queryAndAssert(service, query, 1, "");
query = "() union <a/>";
queryAndAssert(service, query, 1, "");
// Not the same nodes
query = "<a/> union <a/>";
queryAndAssert(service, query, 2, "");
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method precedingSiblingAxis_persistent.
@Test
public void precedingSiblingAxis_persistent() throws XMLDBException, IOException, SAXException {
XQueryService service = storeXMLStringAndGetQueryService("siblings.xml", siblings);
service.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
service.setProperty(OutputKeys.INDENT, "no");
ResourceSet result = queryResource(service, "siblings.xml", "//a[preceding-sibling::*[1]/s = 'B']", 1);
assertXMLEqual("<a> <s>Z</s> <n>4</n> </a>", result.getResource(0).getContent().toString());
result = queryResource(service, "siblings.xml", "//a[preceding-sibling::a[1]/s = 'B']", 1);
assertXMLEqual("<a> <s>Z</s> <n>4</n> </a>", result.getResource(0).getContent().toString());
result = queryResource(service, "siblings.xml", "//a[preceding-sibling::*[2]/s = 'B']", 1);
assertXMLEqual("<a> <s>C</s> <n>5</n> </a>", result.getResource(0).getContent().toString());
result = queryResource(service, "siblings.xml", "//a[preceding-sibling::a[2]/s = 'B']", 1);
assertXMLEqual("<a> <s>C</s> <n>5</n> </a>", result.getResource(0).getContent().toString());
result = queryResource(service, "siblings.xml", "/test/preceding-sibling::node()", 2);
assertEquals("<!-- 1 -->", result.getResource(0).getContent().toString());
assertEquals("<!-- 2 -->", result.getResource(1).getContent().toString());
queryResource(service, "siblings.xml", "/node()[1]/preceding-sibling::node()", 0);
result = queryResource(service, "siblings.xml", "/node()[2]/preceding-sibling::node()", 1);
assertEquals("<!-- 1 -->", result.getResource(0).getContent().toString());
result = queryResource(service, "siblings.xml", "/node()[3]/preceding-sibling::node()", 2);
assertEquals("<!-- 1 -->", result.getResource(0).getContent().toString());
assertEquals("<!-- 2 -->", result.getResource(1).getContent().toString());
queryResource(service, "siblings.xml", "/comment()[1]/preceding-sibling::comment()", 0);
result = queryResource(service, "siblings.xml", "/comment()[2]/preceding-sibling::comment()[1]", 1);
assertEquals("<!-- 1 -->", result.getResource(0).getContent().toString());
result = queryResource(service, "siblings.xml", "/comment()[3]/preceding-sibling::comment()[1]", 1);
assertEquals("<!-- 2 -->", result.getResource(0).getContent().toString());
result = queryResource(service, "siblings.xml", "/comment()[3]/preceding-sibling::comment()[2]", 1);
assertEquals("<!-- 1 -->", result.getResource(0).getContent().toString());
service = storeXMLStringAndGetQueryService("siblings_attr.xml", siblings_attr);
service.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
service.setProperty(OutputKeys.INDENT, "no");
queryResource(service, "siblings_attr.xml", "/a/@bb/preceding-sibling::*", 0);
service = storeXMLStringAndGetQueryService("siblings_named1.xml", siblings_named1);
service.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
service.setProperty(OutputKeys.INDENT, "no");
queryResource(service, "siblings_named1.xml", "//y[@n eq '2']/preceding-sibling::*:y", 1);
queryResource(service, "siblings_named1.xml", "//y[@n eq '2']/preceding-sibling::y", 1);
service = storeXMLStringAndGetQueryService("siblings_named2.xml", siblings_named2);
service.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
service.setProperty(OutputKeys.INDENT, "no");
queryResource(service, "siblings_named2.xml", "//y[@n eq '2']/preceding-sibling::*:y", 1);
queryResource(service, "siblings_named2.xml", "//y[@n eq '2']/preceding-sibling::y", 1);
}
use of org.xmldb.api.modules.XQueryService in project exist by eXist-db.
the class XPathQueryTest method starAxisConstraints2.
@Test
public void starAxisConstraints2() throws XMLDBException {
final XQueryService service = storeXMLStringAndGetQueryService("namespaces.xml", namespaces);
service.setNamespace("t", "http://www.foo.com");
String query = "/ * [ ./ * / t:title ]";
ResourceSet result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
query = "/ * [ * / t:title ]";
result = service.queryResource("namespaces.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
}
Aggregations