Apache Camel Xpath 2.0 with Saxon does not look to work in RouteBuilder / Predicates

筅森魡賤 提交于 2019-12-04 17:21:50

In Saxon 9.6 we removed the services file that registered Saxon as an implementation of the JAXP XPathFactory interface. It still implements that interface, it just doesn't have a services file in the JAR file manifest that says so. There's a long history behind this, but there are basically two main reasons: (a) incompatibility between JDK releases making it impossible to produce a services file that works from JDK 5 to JDK 8 inclusive, and (b) merely putting Saxon on the classpath was causing applications to break if they were not written or tested to work with XPath 2.0.

I guess there's a workaround by adding the services file to the SAXON Jar file manifest yourself (you can copy it from the 9.5 release).

Finally I found a working solution for the camel route:

As the .saxon() was without effect on my route, I decided to look another way and I found there is a specific option to override the Xpath factory like this:

.factory(new net.sf.saxon.xpath.XPathFactoryImpl())

After that I had to force the conversion of the result of the Xpath into a String with .resultType(String.class).

The full predicate will looks like this:

Predicate isNotSamePhoneBrand = PredicateBuilder.isNotEqualTo(
        xpath("tokenize(/myns:Phone/@code, ':')[3]").namespaces(ourNS)
        .factory(new net.sf.saxon.xpath.XPathFactoryImpl()).resultType(String.class),
        header("PhoneBrand"));

and now the tokenize function (xpath 2.0) is very well recognized.

I found that I have to do the same thing with the instantiation of the factory implementation when I use it in a camel Processor like this:

net.sf.saxon.xpath.XPathFactoryImpl factory = new net.sf.saxon.xpath.XPathFactoryImpl();
XPath xpath = factory.newXPath();

NodeList channels = (NodeList) xpath.evaluate(
    "//*:Channels/*:Channel/text()",
    body, XPathConstants.NODESET);

This is not dynamic but I want to force the usage of Saxon everywhere then this solution fit our needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!