Find sibling of parent element with floki

I have a html like this

<h4>
	<a name="myname"> Text" </a>
</h4>
<table>
	# table content
</table>

How can I retrieve the table tag using floki, can anyone help me build the selector for this? I spent a lot of time without success trying query this table.

The attribute name is unique so a[name=“myname”] will return just one tag

My last try result in some variations of this selector: a[name=“myname”] +|~|> h4 table

1 Like

Floki uses a CSS selector to walk through the HTML nodes, but it can also parse the HTML and return a nested list of tuples representing the entire tree of nodes (using parse_document or parse_fragment). You can then walk through those nodes in your own Elixir code to find what you’re looking for.

2 Likes

The :has() pseudoclass does exactly what you’re describing - so in an environment that supports the Selectors Level 4 draft, you could use h4:has(a[name='myname']) + table.

HOWEVER

That’s not supported yet in Floki. Trying it produces a Pseudo-class "has" is not implemented. Ignoring. warning message.

1 Like

:sob: :sob: :sob:

Ok, I will try traverse the nodes like @eahanson suggests, but looks like that is not so easy of to do.