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 8027973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:58:55+00:00 2026-06-04T23:58:55+00:00

I am looking to transform a node, based on the number of elements. i.e.

  • 0

I am looking to transform a node, based on the number of elements. i.e. 5, then close the node and re-open a new one.
see example below

<root>
  <branch>
    <foo>bar</foo>
    <foo>bar2</foo>
    <foo>bar3</foo>
    <foo>bar4</foo>
    <foo>bar5</foo>
    <foo>bar6</foo>
    <foo>bar7</foo>
  </branch>
<root>

Should become

<root>
  <branch>
    <foo>bar</foo>
    <foo>bar2</foo>
    <foo>bar3</foo>
    <foo>bar4</foo>
    <foo>bar5</foo>
   <branch>
   </branch>
    <foo>bar6</foo>
    <foo>bar7</foo>
  </branch>
<root>

Please help on how this can be done in XSLT.

  • 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-06-04T23:58:56+00:00Added an answer on June 4, 2026 at 11:58 pm

    I. This XSLT 1.0 transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/*">
      <root>
       <xsl:apply-templates/>
      </root>
     </xsl:template>
    
     <xsl:template match="foo[position() mod 5 = 1]">
      <branch>
        <xsl:copy-of select=
         ". | following-sibling::*[not(position() > 4)]"/>
      </branch>
     </xsl:template>
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document (corrected to well-formed):

    <root>
        <branch>
            <foo>bar</foo>
            <foo>bar2</foo>
            <foo>bar3</foo>
            <foo>bar4</foo>
            <foo>bar5</foo>
            <foo>bar6</foo>
            <foo>bar7</foo>
        </branch>
    </root>
    

    produces the wanted, correct result:

    <root>
       <branch>
          <foo>bar</foo>
          <foo>bar2</foo>
          <foo>bar3</foo>
          <foo>bar4</foo>
          <foo>bar5</foo>
       </branch>
       <branch>
          <foo>bar6</foo>
          <foo>bar7</foo>
       </branch>
    </root>
    

    Explanation:

    This is a case of “positional grouping”, where every starting element of a group is the first of a 5-tuple (so its position satisfies: position() mod 5 = 1.


    II. XSLT 2.0 Solution:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/*">
      <root>
        <xsl:for-each-group select="*/*"
             group-adjacent="(position() -1) idiv 5">
              <branch>
                <xsl:sequence select="current-group()"/>
              </branch>
        </xsl:for-each-group>
      </root>
     </xsl:template>
    </xsl:stylesheet>
    

    when this XSLT 2.0 transformation is applied on the same XML document (above), the same wanted, correct result is produced.

    Explanation:

    Proper use of the <xsl:for-each-group> XSLT 2.0 instruction with the group-adjacent attribute and the current-group() function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to transform a input xml given below <profile name=default> <color id=forecolor
I am looking for a mechanism to transform dataobjects into HTML. The elements in
I am looking to make an XSLT that will transform the input XML below:
I am looking for some best practices/patterns to transform objects from one to another.
It is easy to transform a number into a alphanumeric value based on radix
I'm looking to implement multi-user operational transform just for plain-text based changes on the
I am looking for some help with XSLT to transform one xml file into
Possible Duplicate: Fast fourier transform in c# I am looking for an example of
I'm looking to take the result of a SQL Server stored procedure and transform
Looking at the example here - http://jsfiddle.net/uqYeQ/3/ The first row behaves as expected, returning

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.