I am using org.jdom2.xpath
to evaluate XPath
queries on html
documents.
Attempting to retreive a script text from the head
element, I tried this query:
/html/head/script[contains(text(), 'expression1') and contains(text(), 'expression2')]/text()
This query returns a single result in both XPath Helper and Chrome console ($x
queries) but retuns an empty result set using org.jdom2.xpath
.
Trying the simpler (but heavier) query:
//script[contains(text(), 'expression1') and contains(text(), 'expression2')]/text()
produces the same results.
Code sample:
String xpath = "/html/head/script[contains(text(), 'expression1') and contains(text(), 'expression2')]/text()";
List<Text> tokeScriptResults = (List<Text>) xpathFactory.compile(xpath).evaluate(document);
Afterthought: looking at the Document
object, I see that since the script text is very long that jdom2
split it into an array of Text
s instead of one long Text
. Could this be the issue?