1

I need to strip some values and also some raw HTML from an HTML document. I thought of using XPath, but I cannot get my queries to work.

Here is what I want to achieve:

<div class="unit-id">
   <div class="title">
      some title-1
   </div>

   <div class="another-class">
      another class
   </div>
   <p>segwegw1<p>
   <p>segwegw1<p>
   <p>segwegw1<p>
   <p>segwegw1<p>
   <ul>
     <li>jfjfj</li>
     <li>jfjfj</li>
     <li>jfjfj</li>
   </ul>
</div>


<div class="unit-id">
   <div class="title">
      some title-2
   </div>
   <div class="another-class">
      some other class
   </div>
   <p>segwegw2<p>
   <p>segwegw2<p>
   <p>segwegw2<p>
   <p>segwegw2<p>
</div>


<div class="unit-id">
   <div class="title">
      some title-3
   </div>
   <div class="some-other-class">
      some other data
   </div>
   <p>segwegw3<p>
   <p>segwegw3<p>
   <p>segwegw3<p>
   <p>segwegw3<p>
</div>

So I'd like the query to iterate through each div with a unit-id class and return the value of the divs with a class of title and the rest of the HTML, excluding any more divs so just the p tags and ul stuff for that particular unit-id classed div, and then the next iteration.

Is that possible? Could you provide me with an example of how to write this query? Is there a better way to do it?

4
  • 1
    What have you attempted so far, stackoverflow is not about writing your code but more fixing the problem you have, you have no code to show.
    – Kivylius
    May 13, 2013 at 17:11
  • I have created the query to return a collection of elements that are divs with the class unit-id (query("//div[@class='unit-id']"), however I then need to return all the following non div elements until the next div with a class of 'unit-id'. This I'm struggling with. Is there a better method than using an xpath query?
    – daktau
    May 13, 2013 at 18:30
  • @Jessica - Some of the best questions ever asked in StackOverflow were asked without showing any unsuccessful code. Check out this link: meta.stackexchange.com/questions/122986/…
    – Expedito
    May 13, 2013 at 19:35
  • If you want XPath answers, you should at least fix your HTML to be somewhat valid XML (eg., closing the <p/> tags instead of repeating the opening one). Also, please provide the expected result, your question is somewhat unclear and answering it would include lots of guessing.
    – Jens Erat
    May 13, 2013 at 21:43

1 Answer 1

3

This code does something like what you're looking for:

function get_content($data){
    $doc = new DOMDocument();
    //load HTML string into document object
    if ( ! @$doc->loadHTML($data)){
        return FALSE;
    }
    //create XPath object using the document object as the parameter
    $xpath = new DOMXPath($doc);
    $query = "//div[@class='unit-id']";
    //XPath queries return a NodeList
    $res = $xpath->query($query);
    $out = array();
    foreach ($res as $key => $node){
        //subquery
        $sub = $xpath->query('.//div[@class="title"]', $node);
        $out[$key]['title'] = trim($sub->item(0)->nodeValue);
        foreach ($node->getElementsByTagName('p') as $key2 => $value){
            $out[$key]['par'][$key2] = $value->nodeValue;
        }
        foreach ($node->getElementsByTagName('li') as $key2 => $value){
            $out[$key]['list'][$key2] = $value->nodeValue;
        }
    }
    return $out;
}

Please note that you have errors in your HTML. You're closing paragraph tags should have the trailing slash </p>.

Here's the output:

array
  0 => 
    array
      'title' => string 'some title-1' (length=12)
      'par' => 
        array
          0 => string 'segwegw1' (length=8)
          1 => string 'segwegw1' (length=8)
          2 => string 'segwegw1' (length=8)
          3 => string 'segwegw1' (length=8)
      'list' => 
        array
          0 => string 'jfjfj' (length=5)
          1 => string 'jfjfj' (length=5)
          2 => string 'jfjfj' (length=5)
  1 => 
    array
      'title' => string 'some title-2' (length=12)
      'par' => 
        array
          0 => string 'segwegw2' (length=8)
          1 => string 'segwegw2' (length=8)
          2 => string 'segwegw2' (length=8)
          3 => string 'segwegw2' (length=8)
1
  • Great. That really helps me out. It was doing a sub query on the initial query that was confusing me. Cheers!
    – daktau
    May 15, 2013 at 9:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.