2017-05-03 4 views
0

私のxpathでこのHTMLを次のようにループしています。目指すのは、article要素をループし、その内部に再びarticle要素をループすることです。 私は問題は内側の記事要素に対する私の内側のクエリであると考え、以下のように内側の記事をループするxpath

私はHTMLを持っている:

<div id="content"> 
    <article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome</a></h2> 
      </div> 
     </article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome</a></h2> 
      </div> 
     </article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome</a></h2> 
      </div> 
     </article> 
    </article> 
    <article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome2</a></h2> 
      </div> 
     </article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome2</a></h2> 
      </div> 
     </article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome2</a></h2> 
      </div> 
     </article> 
    </article> 
</div> 

次のように私は私のXPathのコードを持っている:

$articlesxpath = $xpath->query('//*[@id="content"]/article'); 
foreach($articlesxpath as $item){ 
    $items = $item->query('./article'); 
    foreach($items as $ix){ 
    var_dump($ix); 
    } 

}

ご覧のとおり、記事をループして、内側の記事要素の内側に挿入しようとしています。 内部の記事要素から情報を取得することが目的です

私のコードには何が間違っているのか分かりません。狭いXPathクエリへ

+0

そして、何が間違っていますか?私達は絶望すべきですか? –

+0

@u_mulder更新された質問 – jkushner

+0

私は目標を見ましたが、私は__whatが間違っているのを見ません。 –

答えて

0

queryに2つ目の引数を渡す:

$articlesxpath = $xpath->query('//*[@id="content"]/article'); 
foreach($articlesxpath as $item){ 
    // search in $item node 
    $items = $xpath->query('article', $item); 
    foreach($items as $ix) { 
     var_dump($ix->nodeValue); 
    } 
} 

それとも単に:

$articlesxpath = $xpath->query('//*[@id="content"]/article/article'); 
foreach($articlesxpath as $item){ 
    var_dump($item->nodeValue); 
} 

a(およびarticleからない)からのhrefを取得するには、を作成する必要があります適切なクエリ

$articlesxpath = $xpath->query('//*[@id="content"]/article/article/div/h2/a'); 
foreach($articlesxpath as $item){ 
    var_dump($item->getAttribute('href')); 
} 

ここにはmanualがありますので、そのテキストに精通していることをご確認ください。

+0

各記事アンカーからアンカー階層を取得する方法 – jkushner

+0

更新を参照してください。 –

0

あなたはHTMLテンプレートを修正したなら、あなたはこの

XPathクエリのようなDOMDocumentを照会://div[@id="content"]/article/article/div/h2/a

<?php 

$string = '<html><body><div id="content"> 
    <article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome</a></h2> 
      </div> 
     </article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome</a></h2> 
      </div> 
     </article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome</a></h2> 
      </div> 
     </article> 
    </article> 
    <article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome2</a></h2> 
      </div> 
     </article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome2</a></h2> 
      </div> 
     </article> 
     <article> 
      <div> 
      <h2><a href="hrefvalue">Something Awesome2</a></h2> 
      </div> 
     </article> 
    </article> 
</div></body></html>'; 
$obj = new DOMDocument(); 
$obj->loadHTML($string); 
$xpath = new DOMXPath($obj); 
$articlesxpath = $xpath->query('//div[@id="content"]/article/article/div/h2/a'); 
foreach ($articlesxpath as $item) 
{ 
    print_r($item->getAttribute("href")); 
    echo PHP_EOL; 
} 

出力:

hrefvalue 
hrefvalue 
hrefvalue 
hrefvalue 
hrefvalue 
hrefvalue 
関連する問題