如果p标记后面跟着任何其他标记,Xpath将不返回文本


xpath not returning text if p tag is followed by any other tag

我想获得<p><h3>标签之间的所有文本为以下HTML

<div class="bodyText">
  <p>
    <div class="articleBox articleSmallHorizontal channel-32333770 articleBoxBordered alignRight">
  <div class="one">
  <a  href="url" class="img"><img src="url" alt="bar" class="img" width="80" height="60" /></a>
  </div>
  <div class="two">
    <h4 class="preTitle">QIEZ-Lieblinge</h4>
    <h3 class="title"><a  href="url"  title="ABC"  onclick="cmsTracking.trackClickOut({element:this,  channel : 32333770, channelname : 'top_listen',  content : 14832081,  callTemplate : '_htmltagging.Text',  action : 'click',  mouseevent : event});">
        Prominente Gastronomen      </a></h3>
    <span class="postTitle"></span>
    <span class="district"><a href="http://www.qiez.de/berlin/top-listen" title="TOP-LISTEN in Berlin">Berlin</a></span>  </div>
  <div class="clear"></div>
</div>
I want this TEXT</p>
<h3>I want this TEXT</h3>
<p>I want this TEXT</p>
<p>
    <div class="inlineImage alignLeft">
  <div class="medium">
    <img src="http://images03.qiez.de/Restaurant+%C3%96_QIEZ.jpg/280x210/0/167.231.886/167.231.798" width="280" height="210" alt="Schöne Lage: das Restaurant Ø. (c)QIEZ"/>
    <span class="caption">
      Schöne Lage: das Restaurant Ø. (c)QIEZ    </span>
  </div>
</div>I want this TEXT</p>
<p>I want this TEXT</p>
<p>I want this TEXT<br /> </p>
<blockquote><img src="url" alt="" width="68" height="68" />
    "Eigentlich nur drei Worte: Ich komme wieder."<span class="author">Tina Gerstung</span></blockquote>
  <div class="clear"></div>
</div>

i want all " i want this TEXT"。我使用了xpath查询

//div[contains(@class,'bodyText')]/*[local-name()='p' or local-name()='h3']

但是它不给我文本如果<p>标签后面跟着任何其他标签

看起来您的p元素中包含div元素,这是无效的,并且会弄乱事情。如果您在循环中使用var_dump,您可以看到它实际上拾取了节点,但nodeValue为空。

对html的一个快速而肮脏的修复是将p元素中包含的第一个div包装到span中。

<span><div class="articleBox articleSmallHorizontal channel-32333770 articleBoxBordered alignRight">...</div></span>
一个更好的解决办法是把div元素放在段落之外。

如果你使用脏的工作方式,你需要改变你的查询,像这样:

$xpath->query("//div[contains(@class,'bodyText')]/*[local-name()='p' or local-name()='h3']/text()");

如果你不能控制源html。你可以复制一份html,并删除不合适的div:

$nodes = $xpath->query("//div[contains(@class,'articleBox')]");
$node = $nodes->item(0);
$node->parentNode->removeChild($node);

使用simple_html_dom可能更容易。也许你可以试试这个:

include('simple_html_dom.php');
$dom = new simple_html_dom();
$dom->load($html);
foreach($dom->find("div[class=bodyText]") as $parent) {
    foreach($parent->children() as $child) {
        if ($child->tag == 'p' || $child->tag == 'h3') {
            // remove the inner text of divs contained within a p element
            foreach($dom->find('div') as $e) 
                $e->innertext = '';
            echo $child->plaintext . '<br>';
        }
    }
}

这是混合内容。根据定义元素位置的因素,可以使用许多因素。在这个cse中,可能只选择所有的文本节点就足够了:

//div[contains(@class, 'bodyText')]/(p | h3)/text()

如果你的处理器不允许在路径位置中使用联合操作符,那么你可以像以前一样使用你的语法,或者在我看来更简单一点:

//div[contains(@class, 'bodyText')]/*[local-name() = ('p', 'h3')]/text()