[xsl] Sibling recursion?

Subject: [xsl] Sibling recursion?
From: "David Birnbaum djbpitt@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 3 Mar 2022 16:07:14 -0000
Dear XSL-list,

I recently used sibling recursion to manage an XSLT transformation and I
would be grateful for advice about whether there is a more legible and
maintainable alternative. I'm using XSLT 3.0. The task was:

My input is <p> elements with mixed content, some of which is <q> elements
(and other element types). Consecutive <q> elements inside a <p> should be
merged into a single <q>, where consecutive means either immediately
following (with no intervening nodes) or with an intervening
whitespace-only text node. For example:

<p><q>Merge me with the next quote</q><q>I immediately follow the quote I
should be merged with</q> and now some plain text <q>merge me with the next
quote, too</q> <q>but this time there is a whitespace-only text node
between us</q></p>


The desired output is:

<p><q>Merge me with the next quote I immediately follow the quote I should
be merged with</q> and now some plain text <q>merge me with the next quote,
too but this time there is a whitespace-only text node between us</q></p>


There could be other elements inside the mixed content, some inside the
quotes and some not. Quotes do not nest inside other quotes and they have
to be merged with other quotes only inside the same paragraph (that is, not
across paragraph boundaries).

The sibling-recursion approach works as required, but it feels cumbersome
even after I made an intermediate pass to remove whitespace-only text nodes
between <q> elements, so that they would be immediate following sibling
nodes, which freed me from having to distinguish whitespace-only text nodes
from other text nodes when I merged the quotes.

I would be grateful for any insights.

Best,

David

Current Thread