use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class XQueryTest method testFor.
@Test
public void testFor() throws XMLDBException {
ResourceSet result;
String query;
XMLResource resu;
XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
query = "for $f in /*/item return $f";
result = service.queryResource(NUMBERS_XML, query);
assertEquals("XQuery: " + query, 4, result.getSize());
query = "for $f in /*/item order by $f ascending return $f";
result = service.queryResource(NUMBERS_XML, query);
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "3", ((Element) resu.getContentAsDOM()).getAttribute("id"));
query = "for $f in /*/item order by $f descending return $f";
result = service.queryResource(NUMBERS_XML, query);
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "2", ((Element) resu.getContentAsDOM()).getAttribute("id"));
query = "for $f in /*/item order by xs:double($f/price) descending return $f";
result = service.queryResource(NUMBERS_XML, query);
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "4", ((Element) resu.getContentAsDOM()).getAttribute("id"));
query = "for $f in //item where $f/@id = '3' return $f";
result = service.queryResource(NUMBERS_XML, query);
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "3", ((Element) resu.getContentAsDOM()).getAttribute("id"));
// Non null context sequence
query = "/test/item[for $id in ./@id return $id]";
result = service.queryResource(NUMBERS_XML, query);
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, 4, result.getSize());
// Ordered value sequence
query = "let $doc := <doc><value>Z</value><value>Y</value><value>X</value></doc> " + "return " + "let $ordered_values := " + " for $value in $doc/value order by $value ascending " + " return $value " + "for $value in $doc/value " + " return $value[. = $ordered_values[position() = 1]]";
result = service.queryResource(NUMBERS_XML, query);
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "<value>X</value>", resu.getContent());
// Ordered value sequence
query = "for $e in (1) order by $e return $e";
result = service.queryResource(NUMBERS_XML, query);
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "1", resu.getContent());
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class XQueryTest method cdataQuery.
@Test
public void cdataQuery() throws XMLDBException {
ResourceSet result;
String query;
XMLResource resu;
final String xml = "<root><node><![CDATA[world]]></node></root>";
XPathQueryService service = storeXMLStringAndGetQueryService("cdata.xml", xml);
service.setProperty(OutputKeys.INDENT, "no");
query = "//text()";
result = service.queryResource("cdata.xml", query);
assertEquals(1, result.getSize());
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "world", resu.getContent().toString());
query = "//node/text()";
result = service.queryResource("cdata.xml", query);
assertEquals(1, result.getSize());
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "world", resu.getContent().toString());
query = "//node/node()";
result = service.queryResource("cdata.xml", query);
assertEquals(1, result.getSize());
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "world", resu.getContent().toString());
query = "/root[node = 'world']";
result = service.queryResource("cdata.xml", query);
assertEquals(1, result.getSize());
// NOTE - no cdata-section-elements specified for XDM serialization
resu = (XMLResource) result.getResource(0);
assertEquals("XQuery: " + query, "<root><node>world</node></root>", resu.getContent().toString());
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class XQueryTest method largeAttributeKeywordOperator.
@Test
public void largeAttributeKeywordOperator() throws XMLDBException {
ResourceSet result;
String query;
@SuppressWarnings("unused") XMLResource resu;
String large = createXMLContentWithLargeString();
XPathQueryService service = storeXMLStringAndGetQueryService(file_name, xml);
query = "doc('" + file_name + "') / details/metadata[ @docid = '" + large + "' ]";
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 virtualNodesets.
@Test
public void virtualNodesets() throws XMLDBException, IOException, SAXException {
ResourceSet result;
String query;
@SuppressWarnings("unused") XMLResource resu;
@SuppressWarnings("unused") boolean exceptionThrown;
@SuppressWarnings("unused") String message;
XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
service.setProperty(OutputKeys.INDENT, "no");
query = "let $node := (<c id='OK'><b id='cool'/></c>)/descendant::*/attribute::id " + "return <a>{$node}</a>";
result = service.queryResource(NUMBERS_XML, query);
assertEquals("XQuery: " + query, 1, result.getSize());
assertXMLEqual("<a id='cool'/>", ((XMLResource) result.getResource(0)).getContent().toString());
query = "let $node := (<c id='OK'><b id='cool'/></c>)/descendant-or-self::*/child::b " + "return <a>{$node}</a>";
result = service.queryResource(NUMBERS_XML, query);
assertEquals("XQuery: " + query, 1, result.getSize());
assertXMLEqual("<a><b id='cool'/></a>", ((XMLResource) result.getResource(0)).getContent().toString());
query = "let $node := (<c id='OK'><b id='cool'/></c>)/descendant-or-self::*/descendant::b " + "return <a>{$node}</a>";
result = service.queryResource(NUMBERS_XML, query);
assertEquals("XQuery: " + query, 1, result.getSize());
assertXMLEqual("<a><b id='cool'/></a>", ((XMLResource) result.getResource(0)).getContent().toString());
query = "let $doc := <a id='a'><b id='b'/></a> " + "return $doc/*/(<id>{@id}</id>)";
result = service.queryResource(NUMBERS_XML, query);
assertEquals("XQuery: " + query, 1, result.getSize());
assertXMLEqual("<id id='b' />", ((XMLResource) result.getResource(0)).getContent().toString());
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class XQueryTest method functionDoc.
@Test
public void functionDoc() throws XMLDBException, IOException, SAXException {
ResourceSet result;
String query;
@SuppressWarnings("unused") boolean exceptionThrown;
@SuppressWarnings("unused") String message;
XPathQueryService service = storeXMLStringAndGetQueryService(NUMBERS_XML, numbers);
query = "doc('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "')";
result = service.query(query);
assertEquals("XQuery: " + query, 1, result.getSize());
Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
DetailedDiff d = new DetailedDiff(compareXML(numbers, n.toString()));
assertEquals(0, d.getAllDifferences().size());
// ignore eXist namespace's attributes
// assertEquals(1, d.getAllDifferences().size());
query = "let $v := ()\n" + "return doc($v)";
result = service.query(query);
assertEquals("XQuery: " + query, 0, result.getSize());
query = "doc('" + XmldbURI.ROOT_COLLECTION + "/test/dummy" + NUMBERS_XML + "')";
try {
exceptionThrown = false;
result = service.query(query);
} catch (XMLDBException e) {
exceptionThrown = true;
message = e.getMessage();
}
// TODO : to be decided !
// assertTrue(exceptionThrown);
assertEquals(0, result.getSize());
query = "doc-available('" + XmldbURI.ROOT_COLLECTION + "/test/" + NUMBERS_XML + "')";
result = service.query(query);
assertEquals("XQuery: " + query, 1, result.getSize());
assertEquals("XQuery: " + query, "true", result.getResource(0).getContent());
query = "let $v := ()\n" + "return doc-available($v)";
result = service.query(query);
assertEquals("XQuery: " + query, 1, result.getSize());
assertEquals("XQuery: " + query, "false", result.getResource(0).getContent());
query = "doc-available('" + XmldbURI.ROOT_COLLECTION + "/test/dummy" + NUMBERS_XML + "')";
assertEquals("XQuery: " + query, 1, result.getSize());
assertEquals("XQuery: " + query, "false", result.getResource(0).getContent());
}
Aggregations