Re: [xsl] Grouping to Separate Out Block Level Elements from Mixed Content

Subject: Re: [xsl] Grouping to Separate Out Block Level Elements from Mixed Content
From: "Imsieke, Gerrit, le-tex gerrit.imsieke@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 5 Feb 2022 15:30:04 -0000
Hi Don,

I haven't looked at your grouping approach in detail. The spec [1] contains a similar example that employs group-adjacent instead of group-starting-with/group-ending-with. Maybe you can adapt this approach to your situation?

Gerrit

[1] https://www.w3.org/TR/xslt-30/#d7e42443

On 05.02.2022 16:20, Don Smith dsmith_lockesmith@xxxxxxxxx wrote:
The sample XML file has two paragraphs ("p" element) that contain mixed content. However, some of the child elements of "p" are block level elements. I need to separate these block level elements out from the mixed content and place each component of the paragraph content in their own consecutive blocks, as appropriate. So, in the first example input paragraph (see attached for complete input file):
<p>In Boolean algebra, the algebraic normal form <i>(ANF)</i> is a way of writing logical formulas in
B B one of three subforms: <list list-type="simple">
B B B <list-item>
B B B <p>The entire formula is purely true or false</p>
B B B </list-item>
B B B <list-item>
B B B <p>One or more variables are ANDed together into a term</p>
B B B </list-item>
B B B <list-item>
B B B <p>The previous subform with a purely true term</p>
B B B </list-item>
B B </list>Formulas written in ANF are also known as Zhegalkin polynomials and Positive Polarity (or
B B Parity) ReedbMuller expressions (PPRM).</p>


Should on output be:

<p>In Boolean algebra, the algebraic normal form <i>(ANF)</i> is a way of writing logical formulas in
B B one of three subforms:<p>
B <list list-type="simple">
B B B <list-item>
B B B <p>The entire formula is purely true or false</p>
B B B </list-item>
B B B <list-item>
B B B <p>One or more variables are ANDed together into a term</p>
B B B </list-item>
B B B <list-item>
B B B <p>The previous subform with a purely true term</p>
B B B </list-item>
B B </list>
<p>Formulas written in ANF are also known as Zhegalkin polynomials and Positive Polarity (or
B B Parity) ReedbMuller expressions (PPRM).</p>


You'll note in the second example input paragraph a more complex scenario where there are two nested block elements. I think a recursive solution is needed for that but so far I've had no success in achieving it.

The XSLT I've written so far (attached) accomplishes the first needed part by separating the block elements from the mixed content that comes *before* the block using for-each-group and group-starting-with. But attempts to achieve the same result on mixed content that *follows* the block using for-each-group and group-ending-with have not succeeded. I think it's close but am just missing a crucial detail. And, as noted already, the solution does need to account for the possibility of nested blocks.

Embedded XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
B B xmlns:xs="http://www.w3.org/2001/XMLSchema";
B B xmlns:xlink="http://www.w3.org/1999/xlink";
B B xmlns:mml="http://www.w3.org/1998/Math/MathML";
B B exclude-result-prefixes="xs"
B B version="2.0">
B B <xsl:template match="p">
B B B B <xsl:for-each-group select="node()" group-starting-with="list | disp-formula | mml:math">
B B B B B B <xsl:variable name="group1" as="element()">
B B B B B B B B <GROUP1>
B B B B B B B B B B <xsl:apply-templates select="current-group()"></xsl:apply-templates>
B B B B B B B B </GROUP1>
B B B B B B </xsl:variable>
B B B B B B <xsl:choose>
B B B B B B B B <xsl:when test="$group1/list |
B B B B B B B B B B $group1/disp-formula |
B B B B B B B B B B $group1/mml:math">
B B B B B B B B B B <xsl:call-template name="end_group">
B B B B B B B B B B B B <xsl:with-param name="input" select="$group1"/>
B B B B B B B B B B </xsl:call-template>
B B B B B B B B </xsl:when>
B B B B B B B B <xsl:otherwise>
B B B B B B B B B B <xsl:copy-of select="$group1"/>
B B B B B B B B </xsl:otherwise>
B B B B B B </xsl:choose>
B B B B </xsl:for-each-group>
B B </xsl:template>
B B <xsl:template name="end_group">
B B B B <xsl:param name="input" >default</xsl:param>
B B B B <xsl:variable name="group2" as="element()" >
B B B B B B <xsl:for-each-group select="$input" group-ending-with="list | disp-formula | mml:math">
B B B B B B B B <PGROUP2>
B B B B B B B B B B <xsl:apply-templates select="current-group()"></xsl:apply-templates>
B B B B B B B B </PGROUP2>
B B B B B B </xsl:for-each-group>
B B B B </xsl:variable>
B B B B <xsl:copy-of select="$group2"/>
B B </xsl:template>


 B  B  <xsl:template match="/">
 B  B  B  B  <output>
 B  B  B  B  B  B  <xsl:apply-templates/>
 B  B  B  B  </output>
 B  B  </xsl:template>
 B  B  <xsl:template match="*">
 B  B  B  B  <xsl:variable name="name" as="xs:string" select="name()"/>
 B  B  B  B  <xsl:element name="{$name}">
 B  B  B  B  B  B  <xsl:apply-templates/>
 B  B  B  B  </xsl:element>
 B  B  </xsl:template>
</xsl:stylesheet>

Thanks in advance,

Don

Current Thread