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!

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.