Skip to content

How to select values between two nodes in DOM Crawler and PHP

When a page places content between two marker elements — for example, paragraphs between an h1 and an h2 — you can collect that content with a single XPath expression passed to filterXPath.

OUTPUT FORMATSHTMLJSONMarkdown

This guide shows the expression, explains each axis, and gives you a checklist for deciding when this approach fits and when it will return nothing.

The approach: one XPath expression, one filterXPath call

Symfony's DomCrawler exposes filterXPath, which accepts an XPath expression and returns a node list. To gather the elements that sit between two anchors, you describe the second anchor as a following sibling of the first, then walk back through its preceding siblings and keep only those that also come after the first anchor. The result is the set of nodes strictly between the two anchors — the anchors themselves are not returned.

Worked example with the supplied HTML

The sample document below contains an h1, two paragraphs, an h2, and a third paragraph inside a div. The XPath expression selects the h2 as a following sibling of the h1, then keeps the h2's preceding siblings that themselves have an h1 as a preceding sibling. In this document that matches Paragraph 1 and Paragraph 2 only. Paragraph 3 comes after the h2 and is excluded; the h1 and h2 are excluded because they are the anchors.

Reading the XPath expression step by step

The expression is built from three parts, each of which you can adjust independently:

  • //h1 — start at the h1 element.
  • /following-sibling::h2 — move to the h2 that follows it as a sibling.
  • /preceding-sibling::*[preceding-sibling::h1] — from that h2, take every preceding sibling that also has an h1 before it. The asterisk matches any tag, so the filter is not limited to paragraphs.

Decision checklist before you rely on this expression

  • Are the two anchors true siblings under the same parent? If not, following-sibling and preceding-sibling will not connect them.
  • Do you want the anchor elements included? This expression excludes them; add them back explicitly if needed.
  • Could the document contain more than one h1/h2 pair? The expression will match the between-nodes for each pair, so scope it with a parent predicate if you only want one section.
  • Do you need only text, or also attributes and structure? textContent returns text only; iterate the nodes if you need more.
  • Is the content wrapped in a container you could target directly? A direct container selector is usually simpler and less fragile than sibling-axis XPath.

Why the result can be empty — two different causes

An empty node list from this expression usually means one of two things. First, the anchors may not share a parent, so the sibling axes cannot reach from one to the other. Second, the second anchor may appear before the first in document order, in which case following-sibling finds nothing. Check the parent structure and the order of the anchors before changing the expression; the XPath itself is not the problem in either case.

Frequently asked questions

Does filterXPath return the anchor nodes too?

No. The expression in this guide returns only the nodes strictly between the two anchors; the h1 and h2 are used to locate the range but are not part of the result.

Can I use this with tags other than h1 and h2?

Yes. Replace //h1 and following-sibling::h2 with the anchors you need. The preceding-sibling::*[preceding-sibling::h1] filter uses an asterisk, so it matches any tag between the anchors, not just paragraphs.

What if there are multiple h1/h2 pairs on the page?

The expression will match the between-nodes for each pair. To target one section, add a predicate that scopes the starting h1 — for example, by id or class — before the sibling axes.