walk
auto walk(Show show = Show.Element, alias descend = e => true, T)(T root) if (is(T : Node) || is(T == Document))A visit of the descendants of root in tree order, entering and leaving each element: an element gives a step with enter before its children and one with leave after them, the other nodes give only the enter step. show selects the nodes that give steps.
descend(element) decides whether to go into the children of an element; it's called on every element, also those that show hides. If it returns false the element still gives its steps, but its children are skipped. It's called when the walk leaves the enter step of the element, so it sees what the loop did at that step:
int depth;
foreach (s; doc.body.walk!(Show.Element, e => depth < 3))
{
if (s.enter) { depth++; writeln(' '.repeat(depth * 2), s.node.localName); }
else depth--;
}The elements at depth 3 are printed, their children are skipped. On a lazy document the walk parses only as far as it goes. root itself gives no steps.
It's a bidirectional range: retro gives the steps in reverse order, a leave before its enter. Going backwards descend is called after the leave step, the first one met for the element. With a predicate that depends only on the element the two directions give the same steps; with one that depends on what the loop did they can differ.