简体   繁体   中英

PHP DOMXpath getting nested values

I'm using DOMDocument and DOMXpath to parse an html page.

The markup is like this:

<dl>
  <dt>
   <a href="">name</a>
  </dt>
  <dd>
   <span class="one">one</span>
   <span class="two">two</span>
  </dd>
</dl>
<dl>
  <dt>
   <a href="">name</a>
  </dt>
  <dd>
   <span class="one">one</span>
   <span class="two">two</span>
  </dd>
</dl>

Originally, I only need to get the href value and was able to use:

  $doc = new \DOMDocument();
  $doc->loadHTML($html);
  $xpath = new \DOMXPath($doc);
  $res = $xpath->query('//dl/dt/a');

Then iterate through the results using ->nodeValue and ->getAttribute('href')

However, now I want to also get the value within the span tag with a class of value of 'two'.

So I updated my query to $xpath->query('//dl') .

The question is, how would I go about getting the href tag and value now and the span value with the class name. Also, any suggestions on how to debug or display paths to query on would be real helpful.

Thank you!

get those values directly with path:

$res = $xpath->query('//a/@href');

and the content within the span-tag:

$res = $xpath->query("//span[@class='two']");

you can test xpath here: http://www.xpathtester.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM