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

Nested for-each-group and current-group()

Subject: Nested for-each-group and current-group()
From: "Rick Quatro rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 11 May 2020 14:33:48 -0000
 Nested for-each-group and current-group()
Hi All,

 

I have this as my input XML:

 

<?xml version="1.0" encoding="UTF-8"?>

<root>

    <topic>

        <para>topic 1</para>

        <subtopic>

            <para>subtopic 1a</para>

        </subtopic>

        <subtopic>

            <para>subtopic 1b</para>

        </subtopic>

        <subtopic>

            <para>subtopic 1c</para>

        </subtopic>

        <subtopic>

            <para>subtopic 1d</para>

        </subtopic>

    </topic> 

    <topic>

        <para>topic 2</para>

    </topic>

    <topic>

        <para>topic 3</para>

        <subtopic>

            <para>subtopic 3a</para>

        </subtopic>

        <subtopic>

            <para>subtopic 3b</para>

        </subtopic>

        <subtopic>

            <para>subtopic 3c</para>

        </subtopic>

    </topic>

</root>

 

My goal is to convert adjacent topic elements into an <ol> element and
adjacent subtopic elements into a <ul> element.

 

<?xml version="1.0" encoding="UTF-8"?>

<root>

    <ol>

        <li>

            <para>topic 1</para>

            <ul>

                <li>

                    <para>subtopic 1a</para>

                </li>

                <li>

                    <para>subtopic 1b</para>

                </li>

                <li>

                    <para>subtopic 1c</para>

                </li>

                <li>

                    <para>subtopic 1d</para>

                </li>

            </ul>

        </li>

        <li>

            <para>topic 2</para>

        </li>

        <li>

            <para>topic 3</para>

            <ul>

                <li>

                    <para>subtopic 3a</para>

                </li>

                <li>

                    <para>subtopic 3b</para>

                </li>

                <li>

                    <para>subtopic 3c</para>

                </li>

            </ul>

        </li>

    </ol>

</root>

 

I am using nested xsl:for-each-group constructs but I don't get the inner
<ul> elements. I think I am incorrect in my use of current-grouping-key() or
current-group() on the inner group. Thanks in advance.

 

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:xs="http://www.w3.org/2001/XMLSchema"

    exclude-result-prefixes="xs"

    version="2.0">

    

    <xsl:output indent="yes"/>

    

    <xsl:template match="/root">

        <root>

            <xsl:for-each-group select="*" group-adjacent="if(self::topic)
then 1 else 0">

                <xsl:choose>

                    <xsl:when test="current-grouping-key()=1">

                        <ol>

                            <xsl:for-each-group select="current-group()"
group-adjacent="if(self::subtopic) then 1 else 0">

                                <xsl:choose>

                                    <xsl:when
test="current-grouping-key()=1">

                                        <ul>

                                            <xsl:apply-templates
select="current-group()"/>

                                        </ul>

                                    </xsl:when>

                                    <xsl:otherwise>

                                        <xsl:apply-templates
select="current-group()"/>

                                    </xsl:otherwise>

                                </xsl:choose>

                            </xsl:for-each-group>

                        </ol>

                    </xsl:when>

                    <xsl:otherwise>

                        <xsl:apply-templates select="current-group()"/>

                    </xsl:otherwise>

                </xsl:choose>

            </xsl:for-each-group> 

        </root>

    </xsl:template>

    

    <xsl:template match="topic|subtopic">

        <li><xsl:apply-templates/></li>

    </xsl:template>

    

    <!-- Identity transform. -->

    <xsl:template match="@*|node()">

        <xsl:copy copy-namespaces="no">

            <xsl:apply-templates select="@*|node()"/>

        </xsl:copy>

    </xsl:template>

    

</xsl:stylesheet>

 

Rick

 

Rick Quatro

Carmen Publishing Inc.

rick@xxxxxxxxxxxxxxx

585-729-6746

www.frameexpert.com/store/

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.