use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class UpdateReplaceTest method replaceFirstChildWhereParentHasAttribute.
@Test
public void replaceFirstChildWhereParentHasAttribute() throws XMLDBException {
final String testDocName = "replaceFirstChildWhereParentHasAttribute.xml";
final String testDoc = "<Test><Content Foo=\"bar\"><A/><A/></Content></Test>";
final String updateQuery = "let $content := doc('/db/test/" + testDocName + "')/Test/Content\n" + " let $legacy := $content/A[1]\n" + " return\n" + " update replace $legacy with <AA/>,\n" + " doc('/db/test/" + testDocName + "')/Test";
final XQueryService xqueryService = storeXMLStringAndGetQueryService(testDocName, testDoc);
final ResourceSet result = xqueryService.query(updateQuery);
assertNotNull(result);
assertEquals(1, result.getSize());
final Resource res1 = result.getResource(0);
assertNotNull(res1);
assertEquals(XMLResource.RESOURCE_TYPE, res1.getResourceType());
final Document doc = ((XMLResource) res1).getContentAsDOM().getOwnerDocument();
final Source actual = Input.fromDocument(doc).build();
final Source expected = Input.fromString("<Test><Content Foo='bar'><AA/><A/></Content></Test>").build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class XQueryTest method largeAttributeContains.
@Test
public void largeAttributeContains() throws XMLDBException {
ResourceSet result;
String query;
@SuppressWarnings("unused") XMLResource resu;
@SuppressWarnings("unused") String large = createXMLContentWithLargeString();
XPathQueryService service = storeXMLStringAndGetQueryService(file_name, xml);
query = "doc('" + file_name + "') / details/metadata[ contains(@docid, 'aa') ]";
result = service.queryResource(file_name, query);
assertEquals("XQuery: " + query, nbElem, result.getSize());
}
use of org.xmldb.api.modules.XMLResource 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.XMLResource 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.XMLResource 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());
}
Aggregations