c# - Compare XPath list to find the closest to another node? -
i have following node
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[7]/p[1]/#text[1]"
how can figure out last 1 of these closest one?
"/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]"
it won't last one.
here's how got there:
protected string guessthumbnail(htmldocument document) { htmlnode root = document.documentnode; ienumerable<string> result = new list<string>(); htmlnode description = root.selectsinglenode(descriptionpredictivexpath); if (description != null) // in case, predict relevant images ones closest 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 image tag that's closest text node. if (nodes.any()) { var xpaths = nodes.select(n => n.xpath); xpaths.tolist(); // return closest } } } // figure other way throw new notimplementedexception(); }
consider assigning "position in whole tree in depth-first order" each node. way comparing 2 nodes simple.
if can attach arbitrary data nodes - add directly. otherwise have dictionary of nodes position map.
note depending on how many times need comparison approach may slow you, should easy implement , measure it meats requirements.
Comments
Post a Comment