Jump to content

how i can getting xpath address of a string in html code?


mikhak
Go to solution Solved by hansford,

Recommended Posts

Hi

 

i want know how i can getting a xpath address
of 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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution



$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 by hansford
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.