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("<", "<").replace(">", ">") + "</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());
}
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());
}
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());
}
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);
}
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 = '"Hello"']", 1);
service.declareVariable("content", ""Hello"");
queryResource(service, "quotes.xml", "/test[title = $content]", 1);
}
Aggregations