use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class XPathQueryTest method root.
@Test
public void root() throws XMLDBException {
storeXMLStringAndGetQueryService("nested2.xml", nested2);
final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
String query = "let $doc := <a><b/></a> return root($doc)";
ResourceSet result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
final XMLResource resource = (XMLResource) result.getResource(0);
Node node = resource.getContentAsDOM();
// Oh dear ! Don't tell me that *I* have written this :'( -pb
if (node.getNodeType() == Node.DOCUMENT_NODE) {
node = node.getFirstChild();
}
assertEquals("XPath: " + query, "a", node.getLocalName());
query = "let $c := (<a/>,<b/>,<c/>,<d/>,<e/>) return count($c/root())";
result = service.queryResource("numbers.xml", query);
assertEquals("5", result.getResource(0).getContent().toString());
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class XPathQueryTest method position.
@Test
public void position() throws XMLDBException, IOException, SAXException {
final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
queryResource(service, "numbers.xml", "//item[position() = 3]", 1);
queryResource(service, "numbers.xml", "//item[position() < 3]", 2);
queryResource(service, "numbers.xml", "//item[position() <= 3]", 3);
queryResource(service, "numbers.xml", "//item[position() > 3]", 1);
queryResource(service, "numbers.xml", "//item[position() >= 3]", 2);
queryResource(service, "numbers.xml", "//item[position() eq 3]", 1);
queryResource(service, "numbers.xml", "//item[position() lt 3]", 2);
queryResource(service, "numbers.xml", "//item[position() le 3]", 3);
queryResource(service, "numbers.xml", "//item[position() gt 3]", 1);
queryResource(service, "numbers.xml", "//item[position() ge 3]", 2);
queryResource(service, "numbers.xml", "//item[last() - 1]", 1);
queryResource(service, "numbers.xml", "//item[count(('a','b')) - 1]", 1);
String query = "for $a in (<a/>, <b/>, <c/>) return $a/position()";
ResourceSet result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 3, result.getSize());
XMLResource resource = (XMLResource) result.getResource(0);
assertEquals("XPath: " + query, "1", resource.getContent().toString());
resource = (XMLResource) result.getResource(1);
assertEquals("XPath: " + query, "1", resource.getContent().toString());
resource = (XMLResource) result.getResource(2);
assertEquals("XPath: " + query, "1", resource.getContent().toString());
query = "declare variable $doc { <root>" + "<a>1</a><a>2</a><a>3</a><a>4</a><a>5</a><a>6</a><a>7</a>" + "</root> }; " + "(for $x in $doc/a return $x)[position() mod 3 = 2]";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 2, result.getSize());
query = "declare variable $doc { <root>" + "<a>1</a><a>2</a><a>3</a><a>4</a><a>5</a><a>6</a><a>7</a>" + "</root> }; " + "for $x in $doc/a return $x[position() mod 3 = 2]";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 0, result.getSize());
query = "declare variable $doc { <root>" + "<a>1</a><a>2</a><a>3</a><a>4</a><a>5</a><a>6</a><a>7</a>" + "</root> }; " + "for $x in $doc/a[position() mod 3 = 2] return $x";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 2, 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("XPath: " + query, 1, result.getSize());
resource = (XMLResource) result.getResource(0);
assertEquals("XPath: " + query, "a", resource.getContent().toString());
query = "let $doc := document {<a><b n='1'/><b n='2'/></a>} " + "return $doc//b/(if (@n = '1') then position() else ())";
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
assertEquals("1", result.getResource(0).getContent().toString());
// Try a second time to see if the position is reset
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
assertEquals("1", result.getResource(0).getContent().toString());
query = "let $doc := document {<a><b/></a>} " + "return $doc/a[1] [b[1]]";
service.setProperty(OutputKeys.INDENT, "no");
result = service.queryResource("numbers.xml", query);
assertEquals("XPath: " + query, 1, result.getSize());
assertXMLEqual("<a><b/></a>", result.getResource(0).getContent().toString());
// TODO : make this work ! It currently returns some content
// query = "let $doc := document {<a><b><c>1</c></b><b><c>a</c></b></a>} " +
// "return $doc/a[b[position() = 2]/c[.='1']]";
// result = service.queryResource("numbers.xml", query);
// assertEquals("XPath: " + query, 0, result.getSize());
// TODO: make this work ! It currently returns 1
// query = "let $a := ('a', 'b', 'c') for $b in $a[position()] return <blah>{$b}</blah>";
// result = service.queryResource("numbers.xml", query);
// assertEquals("XPath: " + query, 3, result.getSize());
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class XPathQueryTest method queryResource.
@Test
public void queryResource() throws XMLDBException {
XMLResource doc = (XMLResource) testCollection.createResource("strings.xml", "XMLResource");
doc.setContent(strings);
testCollection.storeResource(doc);
doc = (XMLResource) testCollection.createResource("strings2.xml", "XMLResource");
doc.setContent(strings);
testCollection.storeResource(doc);
final XPathQueryService query = (XPathQueryService) testCollection.getService("XPathQueryService", "1.0");
ResourceSet result = query.queryResource("strings2.xml", "/test/string[. = 'Hello World!']");
assertEquals(1, result.getSize());
result = query.query("/test/string[. = 'Hello World!']");
assertEquals(2, result.getSize());
}
use of org.xmldb.api.modules.XMLResource 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.XMLResource in project exist by eXist-db.
the class OptimizerTest method initDatabase.
@BeforeClass
public static void initDatabase() throws XMLDBException, IOException {
CollectionManagementService service = (CollectionManagementService) server.getRoot().getService("CollectionManagementService", "1.0");
testCollection = service.createCollection("test");
Assert.assertNotNull(testCollection);
IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
idxConf.configureCollection(COLLECTION_CONFIG);
XMLResource resource = (XMLResource) testCollection.createResource("test.xml", "XMLResource");
resource.setContent(XML);
testCollection.storeResource(resource);
for (final String sampleName : SAMPLES.getShakespeareXmlSampleNames()) {
resource = (XMLResource) testCollection.createResource(sampleName, XMLResource.RESOURCE_TYPE);
try (final InputStream is = SAMPLES.getShakespeareSample(sampleName)) {
resource.setContent(InputStreamUtil.readString(is, UTF_8));
}
testCollection.storeResource(resource);
}
}
Aggregations