Php Xpath - 如何在子节点中获取两个信息 [英] Php Xpath - How to get two information in a child node

查看:25
本文介绍了Php Xpath - 如何在子节点中获取两个信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在xpath中获取以下信息?

文本 01 - link_1.com

文本 02 - link_2.com

$page = '<div class="新闻"><div class="内容"><div><span class="title">文本 01</span><span class="link">link_1.com</span>

<div class="内容"><div><span class="title">文本 02</span><span class="link">link_2.com</span>

';@$this->dom->loadHTML($page);$xpath = new DOMXPath($this->dom);//执行步骤#1$childElements = $xpath->query("///*[@class='content']");$lista = '';foreach ($childElements 作为 $child) {//执行步骤#2$textChildren = $xpath->query("///*[@class='title']", $child);foreach ($textChildren as $n) {echo $n->nodeValue.'
';}$linkChildren = $xpath->query("///*[@class='link']", $child);foreach ($linkChildren as $n) {echo $n->nodeValue.'
';}}

我的结果回来了

文本 01

文字 02

link_1.com

link_2.com

文本 01

文字 02

link_1.com

link_2.com

解决方案

在第二个和第三个 xpath 中将 // 替换为 descendant::,因为 // 告诉 xpath 在 xml 中的任何地方搜索此元素,而不是在特定节点(根据需要),并且 $child 不是单独的 XML.descendat:: 表示任意子节点

@$this->dom->loadHTML($page);$xpath = new DOMXPath($this->dom);//执行步骤#1$childElements = $xpath->query("///*[@class='content']");$lista = '';foreach ($childElements 作为 $child) {//执行步骤#2$textChildren = $xpath->query("descendant::*[@class='title']", $child);foreach ($textChildren as $n) {echo $n->nodeValue.'
';}$linkChildren = $xpath->query("descendant::*[@class='link']", $child);foreach ($linkChildren as $n) {echo $n->nodeValue.'
';}}

How can I get the following information in xpath?

Text 01 - link_1.com

Text 02 - link_2.com

$page = '
<div class="news">
<div class="content">
    <div>
    <span class="title">Text 01</span>
    <span class="link">link_1.com</span>
    </div>
</div>
<div class="content">
    <div>
    <span class="title">Text 02</span>
    <span class="link">link_2.com</span>
    </div>
</div>
</div>';
@$this->dom->loadHTML($page);
$xpath = new DOMXPath($this->dom);

// perform step #1
$childElements = $xpath->query("//*[@class='content']");
$lista = '';
foreach ($childElements as $child) {
    // perform step #2
    $textChildren = $xpath->query("//*[@class='title']", $child);
    foreach ($textChildren as $n) {
        echo $n->nodeValue.'<br>';
    }

    $linkChildren = $xpath->query("//*[@class='link']", $child);
    foreach ($linkChildren as $n) {
        echo $n->nodeValue.'<br>';
    }
}

My result is returning

Text 01

Text 02

link_1.com

link_2.com

Text 01

Text 02

link_1.com

link_2.com

解决方案

Replace // by descendant:: in second and third xpath, because // tells xpath to search this element evrywhere in xml and not in specific node (as you need), and $child is NOT separate XML. descendat:: means any child node

@$this->dom->loadHTML($page);
$xpath = new DOMXPath($this->dom);

// perform step #1
$childElements = $xpath->query("//*[@class='content']");
$lista = '';
foreach ($childElements as $child) {
    // perform step #2
    $textChildren = $xpath->query("descendant::*[@class='title']", $child);
    foreach ($textChildren as $n) {
        echo $n->nodeValue.'<br>';
    }

    $linkChildren = $xpath->query("descendant::*[@class='link']", $child);
    foreach ($linkChildren as $n) {
        echo $n->nodeValue.'<br>';
    }
}

这篇关于Php Xpath - 如何在子节点中获取两个信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆