0
我有以下节点比较XPath列表以找到离其他节点最近的列表?
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[7]/p[1]/#text[1]"
我怎样才能弄清楚,这些最后一个是最接近的一个?
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[4]/div[1]/img[1]"
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[4]/div[3]/a[1]/img[1]"
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[4]/div[3]/a[2]/img[1]"
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[4]/div[5]/img[1]"
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[5]/div[1]/img[1]"
它并不一定是最后一个。
这里是我如何到达那里:
protected string GuessThumbnail(HtmlDocument document)
{
HtmlNode root = document.DocumentNode;
IEnumerable<string> result = new List<string>();
HtmlNode description = root.SelectSingleNode(DescriptionPredictiveXPath);
if (description != null) // in this case, we predict relevant images are the ones closest to the description text node.
{
HtmlNode node = description.ParentNode;
while (node != null)
{
string path = string.Concat(node.XPath, ImageXPath);
node = node.ParentNode;
IEnumerable<HtmlNode> nodes = root.SelectNodesOrEmpty(path);
// find the image tag that's closest to the text node.
if (nodes.Any())
{
var xpaths = nodes.Select(n => n.XPath);
xpaths.ToList();
// return closest
}
}
}
// figure some other way to do it
throw new NotImplementedException();
}
你的意思最接近的是如何接近它是文档结构目标元素内? – JamieSee 2012-03-08 17:56:01
是的,就是这样。我想知道'div [7]'比'div [5]'更接近'div [4]',如果有多个'div [5]',那么检查下一层,等等直到找到最接近的元素。 – bevacqua 2012-03-08 17:59:12
您的代码是否使用CodePlex的Html Agility Pack? – JamieSee 2012-03-08 18:27:57