Search in sources :

Example 16 with XQueryService

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

the class XPathQueryTest method pathExpression.

@Test
public void pathExpression() throws XMLDBException {
    final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
    // Invalid path expression left operand (not a node set).
    String message = "";
    try {
        queryAndAssert(service, "('a', 'b', 'c')/position()", -1, null);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue("Exception wanted: " + message, message.indexOf("XPTY0019") > -1);
    // Undefined context sequence
    message = "";
    try {
        queryAndAssert(service, "for $a in (<a/>, <b/>, doh, <c/>) return $a", -1, null);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue("Exception wanted: " + message, message.indexOf("XPDY0002") > -1);
    message = "";
    try {
        // "1 to 2" is resolved as a (1, 2), i.e. a sequence of *integers* which is *not* a singleton
        queryAndAssert(service, "let $a := (1, 2, 3) for $b in $a[1 to 2] return $b", -1, null);
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    // No effective boolean value for such a kind of sequence !
    assertTrue("Exception wanted: " + message, message.indexOf("FORG0006") > -1);
    queryAndAssert(service, "let $a := ('a', 'b', 'c') return $a[2 to 2]", 1, null);
    queryAndAssert(service, "let $a := ('a', 'b', 'c') return $a[(2 to 2)]", 1, null);
    queryAndAssert(service, "let $x := <a min='1' max='10'/> return ($x/@min to $x/@max)", 10, null);
    queryAndAssert(service, "(1,2,3)[xs:decimal(.)]", 3, null);
    queryAndAssert(service, "(1,2,3)[. lt 3]", 2, null);
    queryAndAssert(service, "(0, 1, 2)[if(. eq 1) then 0 else position()]", 2, null);
    queryAndAssert(service, "(1, 2, 3)[if(1) then 1 else last()]", 1, null);
    queryAndAssert(service, "(1, 2, 3)[if(1) then 1 else position()]", 1, null);
    queryAndAssert(service, "()/position()", 0, null);
    queryAndAssert(service, "(0, 1, 2)[if(. eq 1) then 2 else 3]", 2, null);
    queryAndAssert(service, "(0, 1, 2)[remove((1, 'a string'), 2)]", 1, null);
    queryAndAssert(service, "let $page-ix := (1,3) return ($page-ix[1] to $page-ix[2])", 3, null);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 17 with XQueryService

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

the class XPathQueryTest method storeXMLStringAndGetQueryService.

/**
 * stores XML String and get Query Service
 * @param documentName to be stored in the DB
 * @param content to be stored in the DB
 * @return the XQuery Service
 * @throws XMLDBException
 */
private XQueryService storeXMLStringAndGetQueryService(final String documentName, final String content) throws XMLDBException {
    final XMLResource doc = (XMLResource) testCollection.createResource(documentName, "XMLResource");
    doc.setContent(content);
    testCollection.storeResource(doc);
    final XQueryService service = (XQueryService) testCollection.getService("XPathQueryService", "1.0");
    return service;
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource)

Example 18 with XQueryService

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

the class XPathQueryTest method precedingSiblingAxis_memtree.

@Test
public void precedingSiblingAxis_memtree() throws XMLDBException, IOException, SAXException {
    final XQueryService service = getQueryService();
    service.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    service.setProperty(OutputKeys.INDENT, "no");
    ResourceSet rs = service.query("(<a/>, <b/>, <c/>)/preceding-sibling::*");
    assertEquals(0, rs.getSize());
    rs = service.query("let $doc := <doc><div id='1'/><div id='2'><div id='3'/></div><div id='4'/><div id='5'><div id='6'/></div></doc> " + "return $doc/div/preceding-sibling::div");
    assertEquals(3, rs.getSize());
    assertXMLEqual("<div id='1'/>", rs.getResource(0).getContent().toString());
    assertXMLEqual("<div id='2'><div id='3'/></div>", rs.getResource(1).getContent().toString());
    assertXMLEqual("<div id='4'/>", rs.getResource(2).getContent().toString());
    rs = service.query("let $doc := document { <!-- 1 -->,<!-- 2 -->,<test/>,<!-- 3 --> } return $doc/node()[1]/preceding-sibling::node()");
    assertEquals(0, rs.getSize());
    rs = service.query("let $doc := document { <!-- 1 -->,<!-- 2 -->,<test/>,<!-- 3 --> } return $doc/node()[2]/preceding-sibling::node()");
    assertEquals(1, rs.getSize());
    assertEquals("<!-- 1 -->", rs.getResource(0).getContent().toString());
    rs = service.query("let $doc := document { <!-- 1 -->,<!-- 2 -->,<test/>,<!-- 3 --> } return $doc/node()[3]/preceding-sibling::node()");
    assertEquals(2, rs.getSize());
    assertEquals("<!-- 1 -->", rs.getResource(0).getContent().toString());
    assertEquals("<!-- 2 -->", rs.getResource(1).getContent().toString());
    rs = service.query("let $doc := document { <!-- 1 -->,<!-- 2 -->,<test/>,<!-- 3 --> } return $doc/comment()[1]/preceding-sibling::comment()");
    assertEquals(0, rs.getSize());
    rs = service.query("let $doc := document { <!-- 1 -->,<!-- 2 -->,<test/>,<!-- 3 --> } return $doc/comment()[2]/preceding-sibling::comment()[1]");
    assertEquals(1, rs.getSize());
    assertEquals("<!-- 1 -->", rs.getResource(0).getContent().toString());
    rs = service.query("let $doc := document { <!-- 1 -->,<!-- 2 -->,<test/>,<!-- 3 --> } return $doc/comment()[3]/preceding-sibling::comment()[1]");
    assertEquals(1, rs.getSize());
    assertEquals("<!-- 2 -->", rs.getResource(0).getContent().toString());
    rs = service.query("let $doc := document { <!-- 1 -->,<!-- 2 -->,<test/>,<!-- 3 --> } return $doc/comment()[3]/preceding-sibling::comment()[2]");
    assertEquals(1, rs.getSize());
    assertEquals("<!-- 1 -->", rs.getResource(0).getContent().toString());
    rs = service.query("let $elem := <a b='c' bb='cc'/> return $elem/@bb/preceding-sibling::*");
    assertEquals(0, rs.getSize());
    rs = service.query("let $doc := document { <x><y n=\"1\"/><y n=\"2\"/><y n=\"3\"/></x> } return $doc //y[@n eq '2']/preceding-sibling::*:y");
    assertEquals(1, rs.getSize());
    rs = service.query("let $doc := document { <x><y n=\"1\"/><y n=\"2\"/><y n=\"3\"/></x> } return $doc //y[@n eq '2']/preceding-sibling::y");
    assertEquals(1, rs.getSize());
    rs = service.query("let $doc := document { <y><y n=\"1\"/><y n=\"2\"/><y n=\"3\"/></y> } return $doc //y[@n eq '2']/preceding-sibling::*:y");
    assertEquals(1, rs.getSize());
    rs = service.query("let $doc := document { <y><y n=\"1\"/><y n=\"2\"/><y n=\"3\"/></y> } return $doc //y[@n eq '2']/preceding-sibling::y");
    assertEquals(1, rs.getSize());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 19 with XQueryService

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

the class XPathQueryTest method xpointerElementNameHandling.

/**
 * Added by Geoff Shuetrim on 15 July 2006 (geoff@galexy.net).
 * This test has been added following identification of a problem running
 * XPath queries that involved the name of elements with the name 'xpointer'.
 * @throws XMLDBException
 */
@Ignore
@Test
public void xpointerElementNameHandling() throws XMLDBException {
    final XQueryService service = storeXMLStringAndGetQueryService("xpointer.xml", xpointerElementName);
    ResourceSet result = service.queryResource("xpointer.xml", "/test/.[local-name()='xpointer']");
    assertEquals(1, result.getSize());
    result = service.queryResource("xpointer.xml", "/test/xpointer");
    assertEquals(1, result.getSize());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 20 with XQueryService

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

the class XPathQueryTest method except.

@Test
public void except() throws XMLDBException {
    final XQueryService service = getQueryService();
    String query = "()  except ()";
    queryAndAssert(service, query, 0, "");
    query = "()  except  (1)";
    queryAndAssert(service, query, 0, "");
    String message = "";
    try {
        query = "(1)  except  ()";
        queryAndAssert(service, query, 0, "");
    } catch (XMLDBException e) {
        message = e.getMessage();
    }
    assertTrue(message.indexOf("XPTY0004") > -1);
    query = "<a/>  except ()";
    queryAndAssert(service, query, 1, "");
    query = "()  except <a/>";
    queryAndAssert(service, query, 0, "");
    // Not the same nodes
    query = "<a/> except <a/>";
    queryAndAssert(service, query, 1, "");
}
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