M
M
mortify2016-05-10 21:26:48
HTML
mortify, 2016-05-10 21:26:48

Is it possible to get such output using xpath?

Hello! It is necessary to obtain from information of this type:

<div>
  %p lorem
  %p lorem
  %pre 
                 <span>нужная</span> информация с 
                            отступами и тегами span    <span>и </span>
                         прочими, чуть ли не на каждом символе.
  %div.someclass НЕНУЖНАЯ ИНФОРМАЦИЯ
</div>

At the exit you need
%p lorem
%p lorem
%pre 
  информация
        с сохраненными отступами
      но без ненужных тегов

How to implement this using xpath + nokogiri? Or just xpath, preferably without resorting to nokogiri's .at_css and .css methods. The main catch is that I don't understand how to require all the elements inside the parent div with html tags, but the elements inside the pre without html tags.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Fov, 2017-09-08
@Roman-Fov

The main catch is that I don't understand how to require all the elements inside the parent div with html tags, but the elements inside the pre without html tags.

<div>
  <p>lorem<span>1</span></p>
  <p>lorem<span>2</span></p>
  <pre>
    test <span>t</span><span>e</span><span>x</span><span>t</span>
  </pre>
</div>

<p> tagged with:
Will produce:
'<p>lorem<span>1</span></p>'
'<p>lorem<span>2</span></p>'

Text from <pre> without tags:
Will produce:
'test '
't'
'e'
'x'
't'
''

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question