Выражение XPath для содержимого внутри узла, пока не встретится узел со строкой - PullRequest
2 голосов
/ 06 июня 2019

Я ищу выражение XPath для получения содержимого статьи без раздела ссылок. Я хочу, чтобы в разделе статьи было все, пока не появится тег <p>, внутри которого есть "Ссылки".

//root/main/article/following-sibling::p[.="References"]
<root>
    <main>
        <article>
            <p>
               The stunning increase in homelessness announced in Los Angeles 
               this week — up 16% over last year citywide — was an almost  an 
               incomprehensible conundrum given the nation's booming economy 
               and the hundreds of millions of dollars that city, county and 
               state officials have directed toward the problem.
            </p>
            <p>
                "We cannot let a set of difficult numbers discourage us 
                or weaken our resolve" Garcetti said.
            </p>
            <p>
                References: Maeve Reston, CNN
            </p>
        </article>
    </main>
</root>

Результат, который я ищу, будет следующим.

<p>
    The stunning increase in homelessness announced in Los Angeles
    this week — up 16% over last year citywide — was an almost  an
    incomprehensible conundrum given the nation's booming economy
    and the hundreds of millions of dollars that city, county and
    state officials have directed toward the problem.
</p>
<p>
    "We cannot let a set of difficult numbers discourage us
    or weaken our resolve" Garcetti said.
</p>

1 Ответ

1 голос
/ 06 июня 2019

Этот XPath,

/root/main/article/p[starts-with(normalize-space(),'References')]
                  /preceding-sibling::p

выберет абзацы, предшествующие абзацу со ссылками.

Вы можете добавить /text(), если хотите, чтобы только текстовые узлы были потомками этих p элементов.

...