[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Feedback on grouping solution

Subject: Re: Feedback on grouping solution
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 26 Oct 2019 19:06:18 -0000
Re:  Feedback on grouping solution
On 26.10.2019 19:03, Rick Quatro rick@xxxxxxxxxxxxxx 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 :)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.