Search in sources :

Example 41 with XQueryService

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

the class XPathQueryTest method cdataMemtreeDom.

@Test
public void cdataMemtreeDom() throws XMLDBException, IOException {
    final String docName = "cdata.xml";
    final Path tempFile = tempFolder.newFile().toPath();
    Files.write(tempFile, Arrays.asList(cdata_xml));
    final XQueryService service = getQueryService();
    String query = "doc(\"" + tempFile.toUri().toString() + "\")";
    ResourceSet result = queryAndAssert(service, query, 1, null);
    final String expected = "<elem1>" + cdata_content.replace("<", "&lt;").replace(">", "&gt;") + "</elem1>";
    assertEquals(expected, result.getResource(0).getContent().toString());
    query = "declare namespace output = \"http://www.w3.org/2010/xslt-xquery-serialization\";\n" + "declare option output:cdata-section-elements \"elem1\";\n" + "doc(\"" + tempFile.toUri().toString() + "\")\n";
    result = queryAndAssert(service, query, 1, null);
    assertEquals(cdata_xml, result.getResource(0).getContent().toString());
    query = "fn:serialize(doc(\"" + tempFile.toUri().toString() + "\"),\n" + "<output:serialization-parameters xmlns:output = \"http://www.w3.org/2010/xslt-xquery-serialization\">\n" + "    <output:method value=\"xml\"/>\n" + "    <output:cdata-section-elements value=\"elem1\"/>\n" + "</output:serialization-parameters>)";
    result = queryAndAssert(service, query, 1, null);
    assertEquals(cdata_xml, result.getResource(0).getContent().toString());
    query = "fn:serialize(doc(\"" + tempFile.toUri().toString() + "\"),\n" + "map {\n" + "    \"method\": \"xml\",\n" + "    \"cdata-section-elements\": xs:QName(\"elem1\")\n" + "})";
    result = queryAndAssert(service, query, 1, null);
    assertEquals(cdata_xml, result.getResource(0).getContent().toString());
}
Also used : Path(java.nio.file.Path) EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 42 with XQueryService

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

the class XPathQueryTest method logicalOr.

@Test
public void logicalOr() throws XMLDBException, IOException, SAXException {
    final XQueryService service = storeXMLStringAndGetQueryService("strings.xml", strings);
    ResourceSet result = queryResource(service, "strings.xml", "<test>{() or ()}</test>", 1);
    Resource r = result.getResource(0);
    assertXMLEqual("<test>false</test>", r.getContent().toString());
    result = queryResource(service, "strings.xml", "() or ()", 1);
    r = result.getResource(0);
    assertEquals("false", r.getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource)

Example 43 with XQueryService

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

the class XPathQueryTest method substring.

@Test
public void substring() throws XMLDBException {
    final XQueryService service = getQueryService();
    // Test cases by MIKA
    final String validQuery = "substring(\"MK-1234\", 4,1)";
    ResourceSet result = queryAndAssert(service, validQuery, 1, validQuery);
    assertEquals("1", result.getResource(0).getContent().toString());
    String invalidQuery = "substring(\"MK-1234\", 4,4)";
    result = queryAndAssert(service, invalidQuery, 1, invalidQuery);
    assertEquals("1234", result.getResource(0).getContent().toString());
    // Test case by Toar
    final String toarQuery = "let $num := \"2003.123\" \n return substring($num, 1, 7)";
    result = queryAndAssert(service, toarQuery, 1, toarQuery);
    assertEquals("2003.12", result.getResource(0).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService)

Example 44 with XQueryService

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

the class XPathQueryTest method followingSiblingAxis_persistent.

@Test
public void followingSiblingAxis_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[following-sibling::*[1]/s = 'B']", 1);
    assertXMLEqual("<a> <s>Z</s> <n>2</n> </a>", result.getResource(0).getContent().toString());
    result = queryResource(service, "siblings.xml", "//a[following-sibling::a[1]/s = 'B']", 1);
    assertXMLEqual("<a> <s>Z</s> <n>2</n> </a>", result.getResource(0).getContent().toString());
    result = queryResource(service, "siblings.xml", "//a[following-sibling::*[2]/s = 'B']", 1);
    assertXMLEqual("<a> <s>A</s> <n>1</n> </a>", result.getResource(0).getContent().toString());
    result = queryResource(service, "siblings.xml", "//a[following-sibling::a[2]/s = 'B']", 1);
    assertXMLEqual("<a> <s>A</s> <n>1</n> </a>", result.getResource(0).getContent().toString());
    result = queryResource(service, "siblings.xml", "/test/following-sibling::node()", 1);
    assertEquals("<!-- 3 -->", result.getResource(0).getContent().toString());
    result = queryResource(service, "siblings.xml", "/node()[1]/following-sibling::node()", 3);
    assertEquals("<!-- 2 -->", result.getResource(0).getContent().toString());
    final Node testElem = ((XMLResource) result.getResource(1)).getContentAsDOM();
    assertTrue(testElem instanceof Element);
    assertEquals("test", testElem.getNodeName());
    assertEquals("<!-- 3 -->", result.getResource(2).getContent().toString());
    result = queryResource(service, "siblings.xml", "/comment()[1]/following-sibling::comment()[1]", 1);
    assertEquals("<!-- 2 -->", result.getResource(0).getContent().toString());
    result = queryResource(service, "siblings.xml", "/comment()[1]/following-sibling::comment()[2]", 1);
    assertEquals("<!-- 3 -->", 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/@b/following-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']/following-sibling::*:y", 1);
    queryResource(service, "siblings_named1.xml", "//y[@n eq '2']/following-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']/following-sibling::*:y", 1);
    queryResource(service, "siblings_named2.xml", "//y[@n eq '2']/following-sibling::y", 1);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XMLResource(org.xmldb.api.modules.XMLResource)

Example 45 with XQueryService

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

the class XPathQueryTest method quotes.

@Test
public void quotes() throws XMLDBException {
    final XQueryService service = storeXMLStringAndGetQueryService("quotes.xml", quotes);
    queryResource(service, "quotes.xml", "/test[title = '&quot;Hello&quot;']", 1);
    service.declareVariable("content", "&quot;Hello&quot;");
    queryResource(service, "quotes.xml", "/test[title = $content]", 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