Search in sources :

Example 26 with XQueryService

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());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 27 with XQueryService

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());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource)

Example 28 with XQueryService

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, "");
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 29 with XQueryService

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);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 30 with XQueryService

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