Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:45:42+00:00 2026-05-29T09:45:42+00:00

I try to understand the grouping functions in XSLT 2.0. My source document is

  • 0

I try to understand the grouping functions in XSLT 2.0. My source document is

<root>
  <entry level="a" name="aaa"/>
  <entry level="a" name="bbb"/>
  <entry level="b" name="ccc"/>
  <entry level="c" name="ddd"/>
  <entry level="a" name="eee"/>
  <entry level="a" name="fff"/>
  <entry level="b" name="ggg"/>
</root>

and the result should be something like

<section name="aaa"/>
<section name="bbb">
  <section name="ccc">
    <section name="ddd" />
  </section>
</section>
<section name="eee"/>
<section name="fff">
  <section name="ggg" />
</section>

That is: if there is a following entry with a deeper level (b is deeper than a,…) the next section should be child of the current, if it is the same level, it should be the next sibling.

I’ve tried with xsl:group-by select="entry" group-by="@level" which gives me a sensible grouping, but I don’t know how to open the section to go down, if there is a down.

There is another similar question which states that “In XSLT 2.0 it would be rather easy with the new grouping functions.” – it might be easy but I don’t get it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:45:43+00:00Added an answer on May 29, 2026 at 9:45 am

    Here is an example:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:mf="http://example.com/mf"
      exclude-result-prefixes="xs mf"
      version="2.0">
    
      <xsl:output indent="yes"/>
    
      <xsl:function name="mf:group" as="element(section)*">
        <xsl:param name="entries" as="element(entry)*"/>
        <xsl:param name="level" as="xs:string"/>
        <xsl:for-each-group select="$entries" group-starting-with="entry[@level = $level]">
          <section name="{@name}">
            <xsl:sequence select="mf:group(current-group() except ., codepoints-to-string(string-to-codepoints($level)[1] + 1))"/>
          </section>
        </xsl:for-each-group>
      </xsl:function>
    
      <xsl:template match="root">
        <xsl:copy>
          <xsl:sequence select="mf:group(entry, 'a')"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Should work with any levels from ‘a’ to ‘z’.

    Saxon 9.4, when running above against

    <root>
      <entry level="a" name="aaa"/>
      <entry level="a" name="bbb"/>
      <entry level="b" name="ccc"/>
      <entry level="c" name="ddd"/>
      <entry level="a" name="eee"/>
      <entry level="a" name="fff"/>
      <entry level="b" name="ggg"/>
    </root>
    

    outputs

    <root>
       <section name="aaa"/>
       <section name="bbb">
          <section name="ccc">
             <section name="ddd"/>
          </section>
       </section>
       <section name="eee"/>
       <section name="fff">
          <section name="ggg"/>
       </section>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to win32 API programming and I try to understand source code of
I try to understand code: <bean id=topBean class=com.topBean > <property name=helpbean ref=helpBean /> <property
I'm looking through the MooTools source to try and understand its .implement() and .extend()
I am interested in delving into Erlang's C source code and try to understand
I try to understand the advantage of Properties Triggers over Data Triggers in WPF.
I try to understand the memory management in ObjectiveC and still some things are
I used mmap(just try to understand how mmap works) to allocate 96k anonymous memory,
I was using .Net Reflector on an Internal App to try and understand what
I try to compare Mnesia with more traditional databases. As I understand it tables
I can fix the below exception with a try-catch loop but I cannot understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.