XPath 查询中的 XPath 查询
XPath query in an XPath query
如何在 XPatch 查询中启动第二个和第三个 XPatch 查询?
例如
键盘 = http://codepad.viper-7.com/ZhMNGw
HTML 代码
<div class="entries">
<h3 class="headline" style="position: relative; cursor: pointer;">
<div>
<a class="selink" href="/tste/?sd=28726585">
<span class="date"> 10:15 </span>
<span class="titel">THE TITLE<span class="subtitel">some subtitle</span>
</span>
</a>
</div>
</h3>
</div>
<div class="entries">
<h3 class="headline" style="position: relative; cursor: pointer;">
<div>
<a class="selink" href="/tste/?sd=287265995">
<span class="date"> 10:16 </span>
<span class="titel">THE TITLE 2<span class="subtitel">some subtitle</span>
</span>
</a>
</div>
</h3>
</div>
PHP
libxml_use_internal_errors(true);
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile('http://domain.com/startpage.php');
$xpath = new DOMXPath($doc);
$query = "//div[@class='entries']"; // <- QUERY ONE
$entries = $xpath->query($query);
$list = array();
$count = 0;
foreach ($entries as $key => $value)
{
$list[$count] = array();
// get the link <- QUERY TWO
$list[$count]['url'] = $xpath->query("//a[@class='selink']");
// get the title but NOT the subtitle <- QUERY THREE
$list[$count]['title'] = $xpath->query("//span[@class='titel']");
$count++;
}
print_r($list);
$xpath->query($expr) 在整个文档上执行循环中的每次调用,因为您没有传递 XPath 查询应该相对评估的文档节点。
使用多态方法 DOMNodeList query(string $expr, DOMNode $node) 您可以相对于给定的 $node 执行子查询。
仅当您使用相对 XPath $expr(不带前导 /)时,此方法才会产生所需的结果。
要从每个 DOMNode/TextNode 中检索字符串,最终使用如下查询:
$list[$count]['url'] = $xpath->query("h3/div/a[@class='selink']/@href", $value)->item(0)->value;
$list[$count]['title'] = $xpath->query("h3/div/a/span[@class='titel']/text()", $value)->item(0)->wholeText;
我编辑了你的 CodePad 代码 here。
此致,
最大值
如何在 XPatch 查询中启动第二个和第三个 XPatch 查询?
例如
键盘 = http://codepad.viper-7.com/ZhMNGw
HTML 代码
<div class="entries">
<h3 class="headline" style="position: relative; cursor: pointer;">
<div>
<a class="selink" href="/tste/?sd=28726585">
<span class="date"> 10:15 </span>
<span class="titel">THE TITLE<span class="subtitel">some subtitle</span>
</span>
</a>
</div>
</h3>
</div>
<div class="entries">
<h3 class="headline" style="position: relative; cursor: pointer;">
<div>
<a class="selink" href="/tste/?sd=287265995">
<span class="date"> 10:16 </span>
<span class="titel">THE TITLE 2<span class="subtitel">some subtitle</span>
</span>
</a>
</div>
</h3>
</div>
PHP
libxml_use_internal_errors(true);
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile('http://domain.com/startpage.php');
$xpath = new DOMXPath($doc);
$query = "//div[@class='entries']"; // <- QUERY ONE
$entries = $xpath->query($query);
$list = array();
$count = 0;
foreach ($entries as $key => $value)
{
$list[$count] = array();
// get the link <- QUERY TWO
$list[$count]['url'] = $xpath->query("//a[@class='selink']");
// get the title but NOT the subtitle <- QUERY THREE
$list[$count]['title'] = $xpath->query("//span[@class='titel']");
$count++;
}
print_r($list);
$xpath->query($expr) 在整个文档上执行循环中的每次调用,因为您没有传递 XPath 查询应该相对评估的文档节点。
使用多态方法 DOMNodeList query(string $expr, DOMNode $node) 您可以相对于给定的 $node 执行子查询。 仅当您使用相对 XPath $expr(不带前导 /)时,此方法才会产生所需的结果。 要从每个 DOMNode/TextNode 中检索字符串,最终使用如下查询:
$list[$count]['url'] = $xpath->query("h3/div/a[@class='selink']/@href", $value)->item(0)->value;
$list[$count]['title'] = $xpath->query("h3/div/a/span[@class='titel']/text()", $value)->item(0)->wholeText;
我编辑了你的 CodePad 代码 here。
此致, 最大值