Skip to main content
Question

HTML ancestors


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor

I have some html data that's in the structure 

 

<h2>Status</h2>
<h3>Place</h3>
<p><a name="1">Name</a></p> <p><a name="2">Name<blockquote><p>Description</p></blockquote></a></p> <h3>Place2</h3>
<p><a name="3">Name<blockquote><p>Description</p></blockquote></a></p>

but the Line Feeds are entirely erratic.

 

 

I need to have one feature per name anchor (which is easily enough done with the HTMLExtractor) but I also need to have the corresponding contents of the h2|h3 tags stored as attributes.

 

Normally I would read in the data line by line and use a TestFilter and variables to do so, but since the lines breaks don't match the data structure in any way, I'm not sure as to the best way to proceed.

2 replies

takashi
Evangelist
  • June 13, 2018

Hi @jdh, I think it's hard to accomplish that with CSS Selectors.

A workaround I can think of is, collect all your interested elements with a StringSearcher and save them into a list attribute, explode the list, and then parse them line by line. If your interested elements are <h2>, <h3>, and <a>, this regex matches them, for example.

<h2.+?</h2>|<h3.+?</h3>|<a.+?</a>


jdh
Contributor
Forum|alt.badge.img+28
  • Author
  • Contributor
  • June 13, 2018
takashi wrote:

Hi @jdh, I think it's hard to accomplish that with CSS Selectors.

A workaround I can think of is, collect all your interested elements with a StringSearcher and save them into a list attribute, explode the list, and then parse them line by line. If your interested elements are <h2>, <h3>, and <a>, this regex matches them, for example.

<h2.+?</h2>|<h3.+?</h3>|<a.+?</a>

That's definitely more elegant than solutions I was considering.

 

 

I did need to modify the regex to allow for closing tags split across multiple lines. ( I may have mentioned the Line Feeds were erratic).

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings