can not make xpath with functions work with JDOM2

情到浓时终转凉″ 提交于 2019-12-12 01:42:37

问题


I used JDOM1 before to parse xmls with xpath, and tired with the non-generic style, so I decide to try JDOM2, OK, everything works perfect for me ( the generic, XPathFactory, XPathExpression). then I try a xpath statement with contains function :

XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
    "./p[contains(.,'time:')]/text()", Filters.textOnly());
String time = timeXpath.evaluateFirst(div).getTextTrim();

then I got exeptions:

java.lang.IllegalStateException: Unable to evaluate expression. See cause
    at org.jdom2.xpath.jaxen.JaxenCompiled.evaluateRawFirst(JaxenCompiled.java:200)
    at org.jdom2.xpath.util.AbstractXPathCompiled.evaluateFirst(AbstractXPathCompiled.java:327)
    at peace.org.tm.spider.star.DamaiStarSpider.syncStarTracks(DamaiStarSpider.java:123)
    at peace.org.tm.spider.star.DamaiStarSpider.main(DamaiStarSpider.java:156)
Caused by: org.jaxen.UnresolvableException: Function :contains
    at org.jaxen.SimpleFunctionContext.getFunction(SimpleFunctionContext.java:142)
    at org.jaxen.ContextSupport.getFunction(ContextSupport.java:189)
    at org.jaxen.Context.getFunction(Context.java:153)
    at org.jaxen.expr.DefaultFunctionCallExpr.evaluate(DefaultFunctionCallExpr.java:183)
    at org.jaxen.expr.DefaultPredicate.evaluate(DefaultPredicate.java:106)
    at org.jaxen.expr.PredicateSet.evaluatePredicates(PredicateSet.java:188)
    at org.jaxen.expr.DefaultLocationPath.evaluate(DefaultLocationPath.java:218)

then I tried:

XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
    "./p[fn:contains(.,'time:')]/text()", Filters.textOnly());
String time = timeXpath.evaluateFirst(div).getTextTrim();

xpath compile failed:

java.lang.IllegalArgumentException: Unable to compile './p[fn:contains(.,'time:')]/text()'. See Cause.
    at org.jdom2.xpath.jaxen.JaxenCompiled.<init>(JaxenCompiled.java:152)
    at org.jdom2.xpath.jaxen.JaxenXPathFactory.compile(JaxenXPathFactory.java:82)
    at org.jdom2.xpath.XPathFactory.compile(XPathFactory.java:282)
    at peace.org.tm.spider.star.DamaiStarSpider.syncStarTracks(DamaiStarSpider.java:91)
    at peace.org.tm.spider.star.DamaiStarSpider.main(DamaiStarSpider.java:156)
Caused by: org.jaxen.XPathSyntaxException: Unexpected '('
    at org.jaxen.BaseXPath.<init>(BaseXPath.java:136)
    at org.jaxen.BaseXPath.<init>(BaseXPath.java:157)
    at org.jdom2.xpath.jaxen.JaxenCompiled.<init>(JaxenCompiled.java:150)
    ... 4 more

I already google the stack trace for about 2 hours, nothing useful founded, I think maybe I made a very stupid mistake, is anyone can figure it out for me? thanks!


回答1:


I can't reproduce the exceptions you are getting.... I am using JDOM 2.0.5 with Jaxen 1.1.6.

I have created the following:

public static void main(String[] args) {

    Element root = new Element ("root");
    Element p = new Element("p");
    p.addContent("  Return this time: Boo! ");
    root.addContent(p);

    XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
            "./p[contains(.,'time:')]/text()", Filters.textOnly());
    XPathDiagnostic<Text> xpd = timeXpath.diagnose(root, true);
    System.out.println(xpd);
    System.out.println(timeXpath.evaluateFirst(root).getTextTrim());

}

and it produces:

[XPathDiagnostic: './p[contains(.,'time:')]/text()' evaluated (first) against org.jdom2.Element produced  raw=1 discarded=0 returned=1]
Return this time: Boo!

I believe you may have out-of-date Jaxen class libraries?



来源:https://stackoverflow.com/questions/19630295/can-not-make-xpath-with-functions-work-with-jdom2

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