Как использовать XPath для извлечения первых трех предложений из абзаца? - PullRequest
1 голос
/ 24 апреля 2019

Мне нужно очистить первые три предложения абзаца, если они существуют, с помощью XPath.

Я уже выделил нужный абзац:

//h3[contains(., 'Synopsis')]/following-sibling::p[1]

Что возвращает простой неформатированный абзац:

What do we do when the world's walls - its family structures, its value-systems, it political forms - crumble? The central character of this novel, 'Moor' Zogoiby, only son of a wealthy, artistic-bohemian Bombay family, finds himself in such a moment of crisis. His mother, a famous painter and an emotional despot, worships beauty, but Moor is ugly, he has a deformed hand. Moor falls in love, with a married woman; when their secret is revealed, both are expelled; a suicide pact is proposed, but only the woman dies. Moor chooses to accept his fate, plunges into a life of depravity in Bombay, then becomes embroiled in a major financial scandal. The novel ends in Spain, in the studio of a painter who was a lover of Moor's mother: in a violent climax Moor has, one more, to decide whether to save the life of his lover by sacrificing his own. 

Мне нужны только первые три предложения, и я хочу быть снисходительным и игнорировать этот первый вопросительный знак, я просто хочу, чтобы все было до первых трех периодов.

1 Ответ

0 голосов
/ 24 апреля 2019
concat(
  substring-before(//h3[contains(., 'Synopsis')]/following-sibling::p[1]/text(), '.'),
  '.',
  substring-before(substring-after(//h3[contains(., 'Synopsis')]/following-sibling::p[1]/text(), '.'), '.'),
  '.',
  substring-before(substring-after(substring-after(//h3[contains(., 'Synopsis')]/following-sibling::p[1]/text(), '.'), '.'), '.'),
  '.'
)

(С XPath интересно делать сумасшедшие вещи, но в реальных сценариях я бы не стал использовать его для подобных задач, если бы не был вынужден делать это из-за полного отсутствия других возможностей.)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...