Search in sources :

Example 6 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXPath method testB1.

@Test
public void testB1() throws Exception {
    final XPathSelector selector = new XPathEvaluator.Builder("//b[1]", mHolder.getSession()).build().call();
    final StringBuilder strBuilder = new StringBuilder();
    for (final XdmItem item : selector) {
        strBuilder.append(item.toString());
    }
    assertXMLEqual("expected pieces to be similar", "<b xmlns:p=\"ns\">foo<c xmlns:p=\"ns\"/></b>", strBuilder.toString());
}
Also used : XPathSelector(net.sf.saxon.s9api.XPathSelector) XdmItem(net.sf.saxon.s9api.XdmItem) Test(org.junit.Test)

Example 7 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXPath method testB2Text.

@Test
public void testB2Text() throws Exception {
    final XPathSelector selector = new XPathEvaluator.Builder("//b[2]/text()", mHolder.getSession()).build().call();
    final StringBuilder strBuilder = new StringBuilder();
    for (final XdmItem item : selector) {
        strBuilder.append(item.toString());
    }
    assertEquals("bar", strBuilder.toString());
}
Also used : XPathSelector(net.sf.saxon.s9api.XPathSelector) XdmItem(net.sf.saxon.s9api.XdmItem) Test(org.junit.Test)

Example 8 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXPath method testB1String.

@Test
public void testB1String() throws Exception {
    final XPathSelector selector = new XPathEvaluator.Builder("//b[1]/text()", mHolder.getSession()).build().call();
    final StringBuilder strBuilder = new StringBuilder();
    for (final XdmItem item : selector) {
        strBuilder.append(item.toString());
    }
    assertEquals("foo", strBuilder.toString());
}
Also used : XPathSelector(net.sf.saxon.s9api.XPathSelector) XdmItem(net.sf.saxon.s9api.XdmItem) Test(org.junit.Test)

Example 9 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project jmeter by apache.

the class XPathUtilTest method testBug63033.

@Test
public void testBug63033() throws SaxonApiException {
    Processor p = new Processor(false);
    XPathCompiler c = p.newXPathCompiler();
    c.declareNamespace("age", "http://www.w3.org/2003/01/geo/wgs84_pos#");
    String xPathQuery = "//Employees/Employee[1]/age:ag";
    XPathExecutable e = c.compile(xPathQuery);
    XPathSelector selector = e.load();
    selector.setContextItem(p.newDocumentBuilder().build(new StreamSource(new StringReader(xmlDoc))));
    XdmValue nodes = selector.evaluate();
    XdmItem item = nodes.itemAt(0);
    assertEquals("<age:ag xmlns:age=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">29</age:ag>", item.toString());
}
Also used : XdmValue(net.sf.saxon.s9api.XdmValue) Processor(net.sf.saxon.s9api.Processor) XPathCompiler(net.sf.saxon.s9api.XPathCompiler) XPathSelector(net.sf.saxon.s9api.XPathSelector) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) XPathExecutable(net.sf.saxon.s9api.XPathExecutable) XdmItem(net.sf.saxon.s9api.XdmItem) Test(org.junit.jupiter.api.Test)

Example 10 with XPathSelector

use of net.sf.saxon.s9api.XPathSelector in project jmeter by apache.

the class XPathUtil method putValuesForXPathInListUsingSaxon.

public static void putValuesForXPathInListUsingSaxon(String xmlFile, String xPathQuery, List<String> matchStrings, boolean fragment, int matchNumber, String namespaces) throws SaxonApiException, FactoryConfigurationError {
    // generating the cache key
    final ImmutablePair<String, String> key = ImmutablePair.of(xPathQuery, namespaces);
    // check the cache
    XPathExecutable xPathExecutable;
    if (StringUtils.isNotEmpty(xPathQuery)) {
        xPathExecutable = XPATH_CACHE.get(key);
    } else {
        log.warn("Error : {}", JMeterUtils.getResString("xpath2_extractor_empty_query"));
        return;
    }
    try (StringReader reader = new StringReader(xmlFile)) {
        // We could instantiate it once but might trigger issues in the future
        // Sharing of a DocumentBuilder across multiple threads is not recommended.
        // However, in the current implementation sharing a DocumentBuilder (once initialized)
        // will only cause problems if a SchemaValidator is used.
        net.sf.saxon.s9api.DocumentBuilder builder = PROCESSOR.newDocumentBuilder();
        XdmNode xdmNode = builder.build(new SAXSource(new InputSource(reader)));
        if (xPathExecutable != null) {
            XPathSelector selector = null;
            try {
                selector = xPathExecutable.load();
                selector.setContextItem(xdmNode);
                XdmValue nodes = selector.evaluate();
                int length = nodes.size();
                int indexToMatch = matchNumber;
                // In case we need to extract everything
                if (matchNumber < 0) {
                    for (XdmItem item : nodes) {
                        if (fragment) {
                            matchStrings.add(item.toString());
                        } else {
                            matchStrings.add(item.getStringValue());
                        }
                    }
                } else {
                    if (indexToMatch <= length) {
                        if (matchNumber == 0 && length > 0) {
                            indexToMatch = JMeterUtils.getRandomInt(length) + 1;
                        }
                        XdmItem item = nodes.itemAt(indexToMatch - 1);
                        matchStrings.add(fragment ? item.toString() : item.getStringValue());
                    } else {
                        if (log.isWarnEnabled()) {
                            log.warn("Error : {}{}", JMeterUtils.getResString("xpath2_extractor_match_number_failure"), indexToMatch);
                        }
                    }
                }
            } finally {
                if (selector != null) {
                    try {
                        selector.getUnderlyingXPathContext().setContextItem(null);
                    } catch (Exception e) {
                    // NOSONAR Ignored on purpose
                    // NOOP
                    }
                }
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) XdmNode(net.sf.saxon.s9api.XdmNode) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XdmValue(net.sf.saxon.s9api.XdmValue) SAXSource(javax.xml.transform.sax.SAXSource) XPathSelector(net.sf.saxon.s9api.XPathSelector) StringReader(java.io.StringReader) XPathExecutable(net.sf.saxon.s9api.XPathExecutable) XdmItem(net.sf.saxon.s9api.XdmItem)

Aggregations

XPathSelector (net.sf.saxon.s9api.XPathSelector)10 XdmItem (net.sf.saxon.s9api.XdmItem)9 Test (org.junit.Test)6 StringReader (java.io.StringReader)3 XPathExecutable (net.sf.saxon.s9api.XPathExecutable)3 XdmValue (net.sf.saxon.s9api.XdmValue)3 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 TransformerException (javax.xml.transform.TransformerException)2 SAXSource (javax.xml.transform.sax.SAXSource)2 Processor (net.sf.saxon.s9api.Processor)2 SaxonApiException (net.sf.saxon.s9api.SaxonApiException)2 XPathCompiler (net.sf.saxon.s9api.XPathCompiler)2 XdmNode (net.sf.saxon.s9api.XdmNode)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 StreamSource (javax.xml.transform.stream.StreamSource)1 Configuration (net.sf.saxon.Configuration)1