Search in sources :

Example 61 with XMLResource

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());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 62 with XMLResource

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());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 63 with XMLResource

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());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 64 with XMLResource

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());
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Example 65 with XMLResource

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());
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) XPathQueryService(org.xmldb.api.modules.XPathQueryService) EXistXPathQueryService(org.exist.xmldb.EXistXPathQueryService) Node(org.w3c.dom.Node) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) XMLResource(org.xmldb.api.modules.XMLResource)

Aggregations

XMLResource (org.xmldb.api.modules.XMLResource)142 Collection (org.xmldb.api.base.Collection)56 ResourceSet (org.xmldb.api.base.ResourceSet)56 XPathQueryService (org.xmldb.api.modules.XPathQueryService)49 Test (org.junit.Test)33 Resource (org.xmldb.api.base.Resource)24 XQueryService (org.xmldb.api.modules.XQueryService)23 Node (org.w3c.dom.Node)22 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)20 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)18 Document (org.w3c.dom.Document)16 XMLDBException (org.xmldb.api.base.XMLDBException)15 EXistResource (org.exist.xmldb.EXistResource)9 EXistXQueryService (org.exist.xmldb.EXistXQueryService)9 BinaryResource (org.xmldb.api.modules.BinaryResource)9 Path (java.nio.file.Path)8 Before (org.junit.Before)8 Element (org.w3c.dom.Element)8 Source (javax.xml.transform.Source)7 SAXSerializer (org.exist.util.serializer.SAXSerializer)7