use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.
the class XQueryTest method typedVariables.
@Test
public void typedVariables() throws XMLDBException {
ResourceSet result;
String query;
boolean exceptionThrown;
@SuppressWarnings("unused") String message;
XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
query = "let $v as element()* := ( <assign/> , <assign/> )\n" + "let $w := <r>{ $v }</r>\n" + "let $x as element()* := $w/assign\n" + "return $x";
result = service.query(query);
assertEquals("XQuery: " + query, 2, result.getSize());
assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
assertEquals("XQuery: " + query, "assign", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
query = "let $v as node()* := ()\n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "let $v as item()* := ()\n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "let $v as empty-sequence() := ()\n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "let $v as item() := ()\n" + "return $v";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue("XQuery: " + query, exceptionThrown);
query = "let $v as item()* := ( <a/> , 1 )\n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 2, result.getSize());
assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
assertEquals("XQuery: " + query, "1", ((XMLResource) result.getResource(1)).getContent());
query = "let $v as node()* := ( <a/> , 1 )\n" + "return $v";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue(exceptionThrown);
query = "let $v as item()* := ( <a/> , 1 )\n" + "let $w as element()* := $v\n" + "return $w";
try {
exceptionThrown = false;
result = service.query(query);
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue(exceptionThrown);
query = "declare variable $v as element()* {( <assign/> , <assign/> ) };\n" + "declare variable $w { <r>{ $v }</r> };\n" + "declare variable $x as element()* { $w/assign };\n" + "$x";
result = service.query(query);
assertEquals("XQuery: " + query, 2, result.getSize());
assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
assertEquals("XQuery: " + query, "assign", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
query = "declare variable $v as node()* { () };\n" + "$v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "declare variable $v as item()* { () };\n" + "$v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "declare variable $v as empty-sequence() { () };\n" + "$v";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "declare variable $v as item() { () };\n" + "$v";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue("XQuery: " + query, exceptionThrown);
query = "declare variable $v as item()* { ( <a/> , 1 ) }; \n" + "$v";
result = service.query(query);
assertEquals("XQuery: " + query, 2, result.getSize());
assertEquals("XQuery: " + query, Node.ELEMENT_NODE, ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeType());
assertEquals("XQuery: " + query, "a", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
assertEquals("XQuery: " + query, "1", ((XMLResource) result.getResource(1)).getContent());
query = "declare variable $v as node()* { ( <a/> , 1 ) };\n" + "$v";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue(exceptionThrown);
query = "declare variable $v as item()* { ( <a/> , 1 ) };\n" + "declare variable $w as element()* { $v };\n" + "$w";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
assertTrue(exceptionThrown);
query = "let $v as document-node() := doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "') \n" + "return $v";
result = service.query(query);
assertEquals("XQuery: " + query, 1, result.getSize());
// TODO : no way to test the node type ?
// assertEquals( "XQuery: " + query, Node.DOCUMENT_NODE, ((XMLResource)result.getResource(0)));
assertEquals("XQuery: " + query, "test", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
}
use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.
the class XQueryTest method constructedNode1.
@Test
public void constructedNode1() throws XMLDBException {
String q1 = "let $a := <A/> for $b in $a//B/string() return \"Oops!\"";
XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
ResourceSet result = service.query(q1);
assertEquals(0, result.getSize());
}
use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.
the class XQueryTest method namespace.
@Test
public void namespace() throws XMLDBException, IOException, SAXException {
Resource doc;
ResourceSet result;
String query;
@SuppressWarnings("unused") XMLResource resu;
@SuppressWarnings("unused") boolean exceptionThrown;
String message;
Collection testCollection = getTestCollection();
doc = testCollection.createResource(MODULE1_NAME, "BinaryResource");
doc.setContent(module1);
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
doc = testCollection.createResource(MODULE2_NAME, "BinaryResource");
doc.setContent(module2);
((EXistResource) doc).setMimeType("application/xquery");
testCollection.storeResource(doc);
doc = testCollection.createResource(NAMESPACED_NAME, "XMLResource");
doc.setContent(namespacedDocument);
((EXistResource) doc).setMimeType("application/xml");
testCollection.storeResource(doc);
XPathQueryService service = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "(:: redefine existing prefix ::)\n" + "declare namespace blah=\"bla\";\n" + "$blah:param";
try {
message = "";
result = service.query(query);
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("XQST0033") > -1);
query = "xquery version \"1.0\";\n" + "import module namespace blah=\"blah\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "(:: redefine existing prefix with same getUri ::)\n" + "declare namespace blah=\"blah\";\n" + "declare variable $blah:param {\"value-2\"};\n" + "$blah:param";
try {
message = "";
result = service.query(query);
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("XQST0033") > -1);
query = "xquery version \"1.0\";\n" + "import module namespace foo=\"ho\" at \"" + URI + "/test/" + MODULE1_NAME + "\";\n" + "$foo:bar";
try {
message = "";
result = service.query(query);
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("does not match namespace URI") > -1);
query = "xquery version \"1.0\";\n" + "import module namespace foo=\"ho\" at \"" + URI + "/test/" + MODULE2_NAME + "\";\n" + "$bar";
try {
message = "";
result = service.query(query);
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("No namespace defined for prefix") > -1);
query = "xquery version \"1.0\";\n" + "import module namespace foo=\"blah\" at \"" + URI + "/test/" + MODULE2_NAME + "\";\n" + "$bar";
try {
message = "";
result = service.query(query);
} catch (XMLDBException e) {
message = e.getMessage();
}
assertTrue(message.indexOf("No namespace defined for prefix") > -1);
query = "declare namespace x = \"http://www.foo.com\"; \n" + "let $a := doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NAMESPACED_NAME + "') \n" + "return $a//x:edition";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "declare namespace x = \"http://www.foo.com\"; \n" + "declare namespace y = \"http://exist.sourceforge.net/dc-ext\"; \n" + "let $a := doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NAMESPACED_NAME + "') \n" + "return $a//y:edition";
result = service.query(query);
assertEquals("XQuery: " + query, 1, result.getSize());
assertEquals("XQuery: " + query, "<x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>", ((XMLResource) result.getResource(0)).getContent());
query = "<result xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>{//rdf:Description}</result>";
result = service.query(query);
assertEquals(query, "<result xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" + " <rdf:Description id=\"3\">\n" + " <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title</dc:title>\n" + " <dc:creator xmlns:dc=\"http://purl.org/dc/elements/1.1/\">creator</dc:creator>\n" + " <x:place xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:place>\n" + " <x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>\n" + " </rdf:Description>\n" + "</result>", ((XMLResource) result.getResource(0)).getContent());
query = "<result xmlns='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>{//Description}</result>";
result = service.query(query);
assertEquals("XQuery: " + query, "<result xmlns=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" + " <rdf:Description xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" id=\"3\">\n" + " <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title</dc:title>\n" + " <dc:creator xmlns:dc=\"http://purl.org/dc/elements/1.1/\">creator</dc:creator>\n" + " <x:place xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:place>\n" + " <x:edition xmlns:x=\"http://exist.sourceforge.net/dc-ext\">place</x:edition>\n" + " </rdf:Description>\n" + "</result>", ((XMLResource) result.getResource(0)).getContent());
// Interesting one : let's see with XQuery gurus :-)
// declare namespace fn="";
// fn:current-time()
/*
If the URILiteral part of a namespace declaration is a zero-length string,
any existing namespace binding for the given prefix is removed from
the statically known namespaces. This feature provides a way
to remove predeclared namespace prefixes such as local.
*/
query = "declare option exist:serialize 'indent=no';" + "for $x in <parent4 xmlns=\"http://www.example.com/parent4\"><child4/></parent4> " + "return <new>{$x//*:child4}</new>";
result = service.query(query);
assertXMLEqual("<new><child4 xmlns='http://www.example.com/parent4'/></new>", ((XMLResource) result.getResource(0)).getContent().toString());
}
use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.
the class XQueryTest method let.
@Test
public void let() throws XMLDBException, IOException, SAXException {
ResourceSet result;
String query;
@SuppressWarnings("unused") XMLResource resu;
XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
// Non null context sequence
query = "/test/item[let $id := ./@id return $id]";
result = service.queryResource(NUMBERS_XML, query);
assertEquals("XQuery: " + query, 4, result.getSize());
query = "/test/item[let $id := ./@id return not(/test/set[@id=$id])]";
result = service.queryResource(NUMBERS_XML, query);
assertEquals("XQuery: " + query, 4, 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("XQuery: " + query, 1, result.getSize());
// Ordered value sequence
query = "let $unordset := (for $val in reverse(1 to 100) return " + "<value>{$val}</value>)" + "let $ordset := (for $newval in $unordset " + "where $newval mod 2 eq 1 " + "order by $newval " + "return $newval/text()) " + "return $ordset/ancestor::node()";
result = service.queryResource(NUMBERS_XML, query);
assertEquals("XQuery: " + query, 50, result.getSize());
// WARNING : the return order CHANGES !!!!!!!!!!!!!!!!!!
assertXMLEqual("<value>99</value>", ((XMLResource) result.getResource(0)).getContent().toString());
assertXMLEqual("<value>1</value>", ((XMLResource) result.getResource(49)).getContent().toString());
}
use of org.xmldb.api.modules.XPathQueryService in project exist by eXist-db.
the class XQueryTest method wrongAttributeCardinalityCount_1805609.
/**
* Regression
*
* @see http://sourceforge.net/support/tracker.php?aid=1805609
*/
@Test
public void wrongAttributeCardinalityCount_1805609() throws XMLDBException {
// OK
String query = "element {\"a\"} { <element b=\"\" c=\"\" />/attribute()[namespace-uri(.) != " + "\"http://www.asml.com/metainformation\"]}";
XPathQueryService service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
ResourceSet result = service.query(query);
assertEquals(1, result.getSize());
assertEquals(query, "<a b=\"\" c=\"\"/>", result.getResource(0).getContent().toString());
// NOK
query = "element {\"a\"} { <element b=\"\" c=\"\"/>" + "/attribute()[namespace-uri(.) != \"http://www.asml.com/metainformation\"]}";
service = (XPathQueryService) getTestCollection().getService("XPathQueryService", "1.0");
result = service.query(query);
assertEquals(1, result.getSize());
assertEquals(query, "<a b=\"\" c=\"\"/>", result.getResource(0).getContent().toString());
}
Aggregations