domXpath - 获取ID后的类

时间:2014-01-28 16:22:57

标签: php domdocument domxpath

我有以下HTML:

<p id="v1"><span class="verseref">1</span>Verse 1</p>
<div class="admonition">
<p class="first admonition-title">Title Here</p>
Foo Bar
</div>
<p id="v2"><span class="verseref">2</span>Verse 2</p>
<div class="admonition">
<p class="first admonition-title">Title Here</p>
Bar Foo
</div>

我需要获取数据'Bar Foo'。我怎样才能做到这一点?班级的“箴言”含糊不清。我需要能够引用p#id 1,然后下一步 div.admonition

这可以用domXpath吗? (使用PHP?)如果是这样,我会运行什么查询来获得它?

1 个答案:

答案 0 :(得分:1)

获得“bar foo”.. 试试这个.. 初始化xpath后..

$idV2 = $xpath->query('//p[@id=v2]')->item(0);
$admonition = $idV2->nextSibling;
$children = $admonition->childNodes;
$BarFoo = $children->item(1)->nodeValue;
echo $BarFoo;