$ domxpath-&gt;查询 - 按顺序:<h2> - &gt; <div> - &gt; <P>

时间:2018-03-06 20:29:38

标签: php dom domdocument domparser

如何访问标记div之后的</h2>内的段落内容?

$domxpath->query('//h2[*[contains(text(), "Território e Ambiente")]]/parent::div[@class="cabecalho"]/following-sibling::p[position() = 1]');

html摘录:

    <div _ngcontent-3a8d-21="" class="cabecalho" id="cont">
                <div _ngcontent-3a8d-21="" class="cabecalho__celula-esquerda">
                    <h2 _ngcontent-3a8d-21="" class="cabecalho__titulo">Território e Ambiente</h2>
                </div>

                <!--template bindings={}-->
                <div _ngcontent-3a8d-21="" class="cabecalho__celula-direita cabecalho__celula-direita--fader">

                    <p _ngcontent-3a8d-21="" class="cabecalho__descricao">

                        content paragraph

                    </p>

                    <div _ngcontent-3a8d-21="" class="mobile-exclusive cabecalho__descricao__showhide">
                        <i _ngcontent-3a8d-21="" aria-hidden="true" class="fa fa-chevron-down cabecalho__descricao__bt--show"></i>
                        <i _ngcontent-3a8d-21="" aria-hidden="true" class="fa fa-chevron-up cabecalho__descricao__bt--hide"></i>
                    </div>
                </div>
            </div>

我尝试了上述查询和类似的尝试,但它没有返回段落

1 个答案:

答案 0 :(得分:1)

你没有穿越正确的道路。 parent::div断言但不断言。 pdiv元素的后代,而不是兄弟。 div不是h2之后的元素,它位于另一条路径中,因此它是另一条div

你基本上是这样做的:

$domxpath->query('
    //h2[contains(., "Território e Ambiente")]/../following-sibling::div/p
');