人生のプチ美学を教えてください!!

下のコメントアウトしている2つのクエリをまとめたいと思っています。今のところ3行目のものを暫定で使っています。

ソースに不定期に<storong>が入ってくるので困っています。

//$entries = $xpath->query("//div[@style='FONT-SIZE: 1.4em']/ol/li/strong");
//$entries = $xpath->query("//div[@style='FONT-SIZE: 1.4em']/strong/ol/li/strong");
$entries = $xpath->query("//li/strong");

是非アドバイスお願いします。

A 回答 (1件)

<?php


$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<hoge>
<div style='FONT-SIZE: 1.4em'>
<ol>
<li><strong>あああ</strong></li>
</ol>
<strong>
<ol>
<li><strong>いいい</strong></li>
</ol>
</strong>
</div>
<ul>
<li><strong>ううう</strong></li>
</ul>
</hoge>


XML;
$dom = new domDocument();
$dom->loadXML($xmlstr);
$xpath = new domXPath($dom);
$xpathstr1 = "//div[@style='FONT-SIZE: 1.4em']/ol/li/strong";
$xpathstr2 = "//div[@style='FONT-SIZE: 1.4em']/strong/ol/li/strong";

// http://www.w3.org/TR/xpath#node-sets
// The | operator computes the union of its operands, which must be node-sets.
$entries = $xpath->query($xpathstr1 . "|" . $xpathstr2 );
for ($i=0; $i < $entries->length; $i++){
$node = $entries->item($i);
print $node->textContent; //うううは表示されない
}
?>
    • good
    • 0
この回答へのお礼

テストしていたらお礼をするのを忘れていました。。すみません。
バッチリ動きました。
コツをつかむ必要がありそうです。Xpath。。
ありがとうございました。

お礼日時:2008/04/06 22:19

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!