xsl-list
[Top] [All Lists]

Re: [xsl] Feedback on grouping solution

2019-10-26 14:06:24
On 26.10.2019 19:03, Rick Quatro rick(_at_)rickquatro(_dot_)com wrote:

I need to process the <step> child elements so that the <figure>
elements are always on the "right" (even-numbered position) in the
output. Immediate children of the <procedure> do not factor into the
odd/even sequence.

The first child of each group of adjacent <step> elements starts a new
odd/even series. To ensure that the each <figure> is in an even-numbered
position, I want to insert a <spacer> element where it is required.

Here is my stylesheet. My basic question is: is there a better or more
efficient way to do this?

I think with XSLT 3 it is possible to use xsl:iterate on the child
elements of the adjacent steps found by grouping:

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="procedure">
        <xsl:copy>
            <xsl:for-each-group select="*"
group-adjacent="boolean(self::step)">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <xsl:iterate select="current-group()/*">
                            <xsl:param name="position-in-output"
select="1"/>
                            <xsl:apply-templates select=".">
                                <xsl:with-param
name="position-in-output" select="$position-in-output"/>
                            </xsl:apply-templates>
                            <xsl:next-iteration>
                                <xsl:with-param name="position-in-output"
                                    select="if (self::figure and
$position-in-output mod 2 = 1)
                                            then $position-in-output + 2
                                            else $position-in-output + 1"/>
                            </xsl:next-iteration>
                        </xsl:iterate>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="step/figure">
        <xsl:param name="position-in-output"/>
        <xsl:if test="$position-in-output mod 2 = 1">
            <spacer/>
        </xsl:if>
        <xsl:next-match/>
    </xsl:template>



Now waiting for Dimitre posting a less "fancy" but equally compact XSLT
1 solution :)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>