I want to select the heading element (whether h1, h2, h3, h4, h5 or h6) that is closest above the form using XPath in PHP.
<h2>Foo</h2>
<h3>Bar</h3>
<form>
<input />
</form>
The example above should return the h3 (Bar) because it is closest to the form.
<h4>Kee</h4>
<form>
<input />
</form>
This example on the other hand should return the h4 (Kee) because it is closest.
This query (from https://stackoverflow.com/a/2216795/4391251) works fine for just the h2 tags. I could modify it for h1, h3, h4, h5, etc. but I want a catch-all query.
$headings = $xpath->query('((//form)[2]/ancestor::*/h2[.!=""])[last()]');
Basically I want something like this
$headings = $xpath->query('((//form)['.$i.']/ancestor::*/[h2 or h3][.!=""])[last()]');
Except for that doesn't return any results, and neither does (based on https://stackoverflow.com/a/7995095/4391251)
$headings = $xpath->query('((//form)['.$i.']/ancestor::*/[self::h2 or self::h3][.!=""])[last()]');
What query will give the desired results?