Search in sources :

Example 86 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class XQueryTest method attributeAxis.

@Test
public void attributeAxis() throws XMLDBException {
    ResourceSet result;
    String query;
    XMLResource resu;
    @SuppressWarnings("unused") String large = createXMLContentWithLargeString();
    XPathQueryService service = storeXMLStringAndGetQueryService(file_name, xml);
    query = "let $node := (<c id=\"OK\">b</c>)/descendant-or-self::*/attribute::id " + "return <a>{$node}</a>";
    result = service.query(query);
    resu = (XMLResource) result.getResource(0);
    assertEquals("XQuery: " + query, "OK", ((Element) resu.getContentAsDOM()).getAttribute("id"));
}
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 87 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class XQueryTest method whereClause.

@Test
public void whereClause() 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 $a := element node1 { " + "attribute id {'id'}, " + "element node1 {'1'}, " + "element node2 {'2'} " + "} " + "for $x in $a " + "where $x/@id eq 'id' " + "return $x";
    result = service.queryResource(NUMBERS_XML, query);
    assertEquals("XQuery: " + query, 1, result.getSize());
    assertXMLEqual("<node1 id='id'><node1>1</node1><node2>2</node2></node1>", ((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 88 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class XPathQueryTest method attributes.

/**
 * test simple queries involving attributes
 */
@Test
public void attributes() throws XMLDBException {
    final String testDocument = "numbers.xml";
    final XQueryService service = storeXMLStringAndGetQueryService(testDocument, numbers);
    String query = "/test/item[ @id='1' ]";
    ResourceSet result = service.queryResource(testDocument, query);
    assertEquals("XPath: " + query, 1, result.getSize());
    XMLResource resource = (XMLResource) result.getResource(0);
    Node node = resource.getContentAsDOM();
    if (node.getNodeType() == Node.DOCUMENT_NODE)
        node = node.getFirstChild();
    assertEquals("XPath: " + query, "item", node.getNodeName());
    query = "/test/item [ @type='alphanum' ]";
    result = service.queryResource(testDocument, query);
    assertEquals("XPath: " + query, 1, result.getSize());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) Node(org.w3c.dom.Node) XMLResource(org.xmldb.api.modules.XMLResource)

Example 89 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class XPathQueryTest method dates.

@Test
public void dates() throws XMLDBException {
    final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
    String query = "xs:untypedAtomic(\"--12-05:00\") cast as xs:gMonth";
    ResourceSet result = service.queryResource("numbers.xml", query);
    XMLResource resource = (XMLResource) result.getResource(0);
    assertEquals("XPath: " + query, "--12-05:00", resource.getContent().toString());
    query = "(xs:dateTime(\"0001-01-01T01:01:01Z\") + xs:yearMonthDuration(\"-P20Y07M\"))";
    result = service.queryResource("numbers.xml", query);
    resource = (XMLResource) result.getResource(0);
    assertEquals("XPath: " + query, "-0021-06-01T01:01:01Z", resource.getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) XMLResource(org.xmldb.api.modules.XMLResource)

Example 90 with XMLResource

use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.

the class XPathQueryTest method last.

@Test
public void last() throws XMLDBException {
    final XQueryService service = storeXMLStringAndGetQueryService("numbers.xml", numbers);
    final String query = "<a><b>test1</b><b>test2</b></a>/b/last()";
    final ResourceSet result = service.queryResource("numbers.xml", query);
    assertEquals("XPath: " + query, 2, result.getSize());
    XMLResource resource = (XMLResource) result.getResource(0);
    assertEquals("XPath: " + query, "2", resource.getContent().toString());
    resource = (XMLResource) result.getResource(1);
    assertEquals("XPath: " + query, "2", resource.getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XQueryService(org.xmldb.api.modules.XQueryService) 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