mikhak Posted September 23, 2015 Share Posted September 23, 2015 Hi i want know how i can getting a xpath addressof a string in html code. for example i want finding xpath of YES in this html code : <div><div> <div><div> <div> <p>NO</p> <div> <p>a text</p> </div> </div> </div> <div> <div> <p>YES</p> <div> <p>b text</p> </div> </div> </div></div> </div></div> i used the http://php.net/manual/en/domnode.getnodepath.php but thats only work with tag name and not working with a word or text searching in a string. i want getting like this result finally : /html/body/div/div/div/div[2]/div/p how i can doing like that ? and is it possible doint that with php simple_html_dom parser ? Thanks. Quote Link to comment Share on other sites More sharing options...
requinix Posted September 23, 2015 Share Posted September 23, 2015 Find the node using whatever you want, then call getNodePath(). Or are you trying to violate causality by using the path to the node to get to the node in the first place? Quote Link to comment Share on other sites More sharing options...
mikhak Posted September 23, 2015 Author Share Posted September 23, 2015 Find the node using whatever you want, then call getNodePath(). Or are you trying to violate causality by using the path to the node to get to the node in the first place? Find the node using whatever you want, then call getNodePath() how can do this ? Quote Link to comment Share on other sites More sharing options...
requinix Posted September 23, 2015 Share Posted September 23, 2015 Is it the only in the document whose value is "YES"? Then //p[.="YES"]Which is, conveniently, an XPath "address" to the node. Quote Link to comment Share on other sites More sharing options...
mikhak Posted September 23, 2015 Author Share Posted September 23, 2015 Is it the only <p> in the document whose value is "YES"? Then //p[.="YES"]Which is, conveniently, an XPath "address" to the node. No i dont know 'yes' word in p tag mybe span tag or h1 or h2 or another tag i want having xpath according of only word not tag and word. this is //p[.=YES] tag and word together. thank you for help. Quote Link to comment Share on other sites More sharing options...
requinix Posted September 23, 2015 Share Posted September 23, 2015 Okay, so can you at least say the entire element's value is "YES"? //*[.="YES"]You should also consider actually learning XPath. Quote Link to comment Share on other sites More sharing options...
Solution hansford Posted September 23, 2015 Solution Share Posted September 23, 2015 (edited) $html ="<div><div> <div><div> <div> <span>YES</span> <div> <p>b text</p> </div> </div> <div> <p>NO</p> <div> <p>a text</p> </div> </div> </div> <div> <div> <p>YES</p> <div> <p>b text</p> </div> </div> </div></div> </div></div>"; $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); foreach($xpath->query('//*[.="YES"]') as $node) { echo $node->getNodePath() . '<br />'; } Edited September 23, 2015 by hansford Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.