0

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!

4

1 回答 1

2

使用路径直接获取这些值:

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

以及跨度标签中的内容:

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

你可以在这里测试 xpath:http ://www.xpathtester.com

于 2013-03-05T22:54:15.987 回答