$domxpath->query - 按顺序:<h2> -> <div> -> <p>

$domxpath->query - In order: <h2> -> <div> -> <p>

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

$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>

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

你走的路不对。 parent::div 断言但不遍历。 pdiv 元素的后代,而不是兄弟元素。 div 不是 h2 之后的元素,它在另一个路径中,所以它在另一个 div.

之后

你基本上是这样做的:

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