More groovifed way of sublist with varying range Student list of 50 students and a teacher list of 3 teachers. 

Section XML as the output where each teacher is assigned to a section, and up to 20 students are assigned in that section.

Need to loop on each teacher element, and for each teacher element, loop on the student name from 1-20, than for 2nd teacher, students 21-40 etc.

The following is working code. However, feeling that there could be better groovified way to get 20 students for a teacher

def students = """&lt;Students&gt;
    &lt;name&gt; st name 1&lt;/name&gt;
    &lt;name&gt; st name 2&lt;/name&gt;
    &lt;name&gt; st name 3&lt;/name&gt;
    &lt;name&gt; st name 4&lt;/name&gt;
    &lt;name&gt; st name 5&lt;/name&gt;
    &lt;name&gt; st name 6&lt;/name&gt;
    &lt;name&gt; st name 7&lt;/name&gt;
    &lt;name&gt; st name 8&lt;/name&gt;
    &lt;name&gt; st name 9&lt;/name&gt;
    &lt;name&gt; st name 10&lt;/name&gt;
    &lt;name&gt; st name 11&lt;/name&gt;
    &lt;name&gt; st name 12&lt;/name&gt;
    &lt;name&gt; st name 13&lt;/name&gt;
    &lt;name&gt; st name 14&lt;/name&gt;
    &lt;name&gt; st name 15&lt;/name&gt;
    &lt;name&gt; st name 16&lt;/name&gt;
    &lt;name&gt; st name 17&lt;/name&gt;
    &lt;name&gt; st name 18&lt;/name&gt;
    &lt;name&gt; st name 19&lt;/name&gt;
    &lt;name&gt; st name 20&lt;/name&gt;
    &lt;name&gt; st name 21&lt;/name&gt;
    &lt;name&gt; st name 22&lt;/name&gt;
    &lt;name&gt; st name 23&lt;/name&gt;
    &lt;name&gt; st name 24&lt;/name&gt;
    &lt;name&gt; st name 25&lt;/name&gt;
    &lt;name&gt; st name 26&lt;/name&gt;
    &lt;name&gt; st name 27&lt;/name&gt;
    &lt;name&gt; st name 28&lt;/name&gt;
    &lt;name&gt; st name 29&lt;/name&gt;
    &lt;name&gt; st name 30&lt;/name&gt;
    &lt;name&gt; st name 31&lt;/name&gt;
    &lt;name&gt; st name 32&lt;/name&gt;
    &lt;name&gt; st name 33&lt;/name&gt;
    &lt;name&gt; st name 34&lt;/name&gt;
    &lt;name&gt; st name 35&lt;/name&gt;
    &lt;name&gt; st name 36&lt;/name&gt;
    &lt;name&gt; st name 37&lt;/name&gt;
    &lt;name&gt; st name 38&lt;/name&gt;
    &lt;name&gt; st name 39&lt;/name&gt;
    &lt;name&gt; st name 40&lt;/name&gt;
    &lt;name&gt; st name 41&lt;/name&gt;
    &lt;name&gt; st name 42&lt;/name&gt;
    &lt;name&gt; st name 43&lt;/name&gt;
    &lt;name&gt; st name 44&lt;/name&gt;
    &lt;name&gt; st name 45&lt;/name&gt;
    &lt;name&gt; st name 46&lt;/name&gt;
    &lt;name&gt; st name 47&lt;/name&gt;
    &lt;name&gt; st name 48&lt;/name&gt;
    &lt;name&gt; st name 49&lt;/name&gt;
    &lt;name&gt; st name 50&lt;/name&gt;
&lt;/Students&gt;"""

def teachers = """&lt;Teachers&gt;
    &lt;name&gt; Teacher name 1&lt;/name&gt;
    &lt;name&gt; Teacher name 2&lt;/name&gt;
    &lt;name&gt; Teacher name 3&lt;/name&gt;
&lt;/Teachers&gt;"""


def getData  = { xml, elementName -&gt;
    def parseXml = new XmlSlurper().parseText(xml)
    parseXml.'**'.findAll { it.name() == elementName }
}

def teacherNodes = getData( teachers, 'name')
def studentNodes = getData( students, 'name')

def sublist (data, start, end) {
    start == 0 ? data.take(end) : data.drop(start).take(end-start)
}

def incrementBy = 20
def startStudents = 0
def endStundents = incrementBy 
def builder = new groovy.xml.StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def xml = builder.bind {
    mkp.xmlDeclaration() 
    Assignments {
        teacherNodes.eachWithIndex { teacherNode, index -&gt;          
            Section {
                id (index+1)
                teacher (teacherNode.text())
                studentList {
                    sublist(studentNodes, startStudents, endStundents).each { studentNode -&gt;
                        name (studentNode.text())
                    }
                }               
            }
            startStudents = endStundents
            endStundents += incrementBy
        }       
    }
}

println groovy.xml.XmlUtil.serialize(xml)
You could start with:

def groups = [:]
teacherNodes.each { teacher -&gt;
    groups[teacher] = studentNodes.take(incrementBy)
    studentNodes    = studentNodes.drop(incrementBy) 
}