我有jdom2 XPath的问题:与jdom2 XPath查询结果不明
test.xhtml代码:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>
<title>mypage</title>
</head>
<body>
<div class="in">
<a class="nextpage" href="url.html">
<img src="img/url.gif" alt="to url.html" />
</a>
</div>
</body>
</html>
Java代码:
Document document;
SAXBuilder saxBuilder = new SAXBuilder();
document = saxBuilder.build("test2.html");
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xp = xpfac.compile("//a[@class = 'nextpage']", Filters.element());
for (Element att : xp.evaluate(document)) {
System.out.println("We have target " + att.getAttributeValue("href"));
}
但只是这个我不能得到任何元素。我发现当查询是//*[@class = 'nextpage']
时,它发现它。
We have target url.html
它必须是一些与命名空间或以其他任何标题,因为没有它,它可以产生一些输出。我不知道我做错了什么。
“它必须是具有名称空间的东西” - 正确。我已经链接到的“可能重复”问题是谷歌给我的第一个命令“jdom xpath namespace” –
现在似乎已经解决 - changes:Namespace namespace = Namespace.getNamespace(“my”,“http://www.w3 .ORG/1999/XHTML“);和XPathExpression xp = xpfac.compile(“// my:a [@class ='nextpage']”,Filters.element(),null,namespace); –
d3im
这个问题似乎是脱离主题,因为它现在已经_solved_(见OP的评论)。 – devnull