【问题标题】:Xpath getting multiple p tags in one nodeValueXpath在一个nodeValue中获取多个p标签
【发布时间】:2013-09-03 20:49:12
【问题描述】:

我想在一个nodeValue 中检索div class="post" 中的所有p 标签。这是我的 HTML:

<div class="blogEntry expecting featured featured-post-today%e2%80%99s-top-story first">
<h2><a href="/2013/09/03/rachel-zoe-pregnant-expecting-second-child/" rel="bookmark" title="Permanent Link to Rachel Zoe Expecting Second&nbsp;Child">Rachel Zoe Expecting Second&nbsp;Child</a></h2>
<div class="post">
    <p>Sometext</p>
    <p>someothertext</p>
</div>
<div class="blogEntry expecting featured featured-post-today%e2%80%99s-top-story first">
<h2><a href="someurl" rel="bookmark" title="Permanent Link to Rachel Zoe Expecting Second&nbsp;Child">sometitle</a></h2>
<div class="post">
    <p>Sometext</p>
    <p>someothertext</p>
</div>
</div>

这是我的 xpath:

$finder->query('//div[contains(@class,"blogEntry")]//div[@class="post"]//p');

【问题讨论】:

    标签: php xpath


    【解决方案1】:

    很丑但是希望是你要找的那个-

    //div[contains(@class,"blogEntry")]/div[@class="post"]/concat(p[1]," ",p[2])
    

    //div[contains(@class,"blogEntry")]/div[@class="post"]/string-join((p[1],p[2])," ")
    

    输出

    Sometext someothertext
    Sometext someothertext
    

    【讨论】:

    • @jkushner 然后我会这样想,在 OP 的帖子中没有观察到它。我根据帖子给出了解决方案。
    【解决方案2】:

    应该是:

    $finder->query('//div[contains(@class,"blogEntry")]/div[@class="post"]/p');
    

    【讨论】:

    • “仍在分离”是什么意思?
    • 它仍然将Sometextsomeothertext 分隔成单独的nodeValues。我希望 sometextsomeothertext 在一个值中
    • Xpath 只是从文档中返回节点。由于它们是单独的节点,它们将作为多个节点返回。你不能“加入”他们。如果您需要在获取节点后加入它们的文本值,请使用 PHP 字符串操作,或者 xslt
    • ... 必须喃喃自语 contains(concat(" ", normalize-space(@class), " "), " blogEntry ") 以避免与blogEntryContainer等类发生冲突 ....
    • @Wrikken 太好了! :) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多