1

Есть html со следующей структурой

<div class="text">
  <h1>Headerh1</h1>
    Text1 <br/> after header1 
  <h3>Headerh3.1</h3> 
    Text2 <br/> after header3.1 
  <h3>Headerh3.2</h3>
    Text3 <br/> after header3.2 
  <h3>Headerh3.3</h3>
    Text4 <br/> after header3.3 
 </div>

Как получить текст Text1 after header1?

div[@class='text']/text()[count(preceding-sibling::h1)=1] - возвращает текст после всех тегов.

Найден вариант //div[@class='text']/h3[1]/preceding-sibling::text(), хотелось бы более гибкое решение.

2 ответа 2

1

Если структура постоянная и, как вы отметили, <br/> может встречаться сколько угодно раз, попробуйте получать текст после h1 и до h3, как результат комбинации двух запросов

//div[@class='text']/h1/following-sibling::text()[1]
//div[@class='text']/h3/preceding-sibling::text()[1]

Или объединить (вдруг текст будет между <br>)

//div[@class='text']/h1/following-sibling::text()[not(preceding-sibling::h3)]
2
  • Вот не знаю как их подружить
    – dMazay
    5 авг 2017 в 18:56
  • @dMazay подружено
    – while1pass
    5 авг 2017 в 20:01
0

Первая строка "Text1"

//div[@class='text']/h1/following-sibling::text()[1]

Вторая строка "after header1"

//div[@class='text']/h1/following-sibling::text()[2]

После этого сделайте конкатенацию строк.

Либо, что-то подобное:

//text()[preceding::*[contains(text(),'Headerh1')] and following::*[contains(text(),'Headerh3.1')] and not(contains(., 'Term:'))]
1
  • <br/> может встречаться сколько угодно раз
    – dMazay
    5 авг 2017 в 17:08

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.