I am in need to Group elements and apply a div element for those group. Also kindly note that element levels are optional and they should be grouped accordingly. The levels "prelims","part","chapter","levela","levelb" and "endmatter" should be grouped. Also note that if "levelc" exists then it should also be grouped
Input:
<toc>
<toc.entry level="prelims">Half Titlepage</toc.entry>
<toc.entry level="prelims">Titlepage</toc.entry>
<toc.entry level="prelims">Imprint</toc.entry>
<toc.entry level="part">Part 1. This is part title</toc.entry>
<toc.entry level="chapter">Chapter 1. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="levela">This is level a head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="chapter">Chapter 2. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="levela">This is level a head</toc.entry>
<toc.entry level="chapter">Chapter 3. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="levela">This is level a head</toc.entry>
<toc.entry level="part">Part 2. This is part title</toc.entry>
<toc.entry level="chapter">Chapter 4. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="chapter">Chapter 5. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="chapter">Chapter 6. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="endmatter">References</toc.entry>
<toc.entry level="endmatter">Index</toc.entry>
</toc>
OUTPUT REQUIRED
<toc>
<div class="prelims">
<toc.entry level="prelims">Half Titlepage</toc.entry>
<toc.entry level="prelims">Titlepage</toc.entry>
<toc.entry level="prelims">Imprint</toc.entry>
</div>
<div class="part">
<toc.entry level="part">Part 1. This is part title</toc.entry>
<div class="chapter">
<toc.entry level="chapter">Chapter 1. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<div class="levela">
<toc.entry level="levela">This is level a head</toc.entry>
<div class="levelb">
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
</div>
</div>
</div>
<div class="chapter">
<toc.entry level="chapter">Chapter 2. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<div class="levela">
<toc.entry level="levela">This is level a head</toc.entry>
</div>
</div>
<div class="chapter">
<toc.entry level="chapter">Chapter 3. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<div class="levela">
<toc.entry level="levela">This is level a head</toc.entry>
</div>
</div>
</div>
<div class="part">
<toc.entry level="part">Part 2. This is part title</toc.entry>
<div class="chapter">
<toc.entry level="chapter">Chapter 4. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
</div>
<div class="chapter">
<toc.entry level="chapter">Chapter 5. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
</div>
<div class="chapter">
<toc.entry level="chapter">Chapter 6. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
</div>
</div>
<div class="endmatter">
<toc.entry level="endmatter">References</toc.entry>
<toc.entry level="endmatter">Index</toc.entry>
</div>
</toc>
XSLT TRIED
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:RSUITE="http://www.reallysi.com" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:epub="http://www.idpf.org/2007/ops" epub:prefix="index: http://www.index.com/" exclude-result-prefixes="RSUITE">
<xsl:import href="Entity.xsl"/>
<xsl:output method="xml" indent="no"/>
<xsl:template match="toc">
<toc><xsl:apply-templates select="toc.entry"/></toc>
</xsl:template>
<xsl:template match="toc.entry">
<xsl:choose>
<xsl:when test="@level='prelims'"><div class="prelims"><xsl:for-each select="@level='prelims'"><xsl:apply-templates/></xsl:for-each></div></xsl:when>
<xsl:when test="@level='levela'"><xsl:for-each select="@level='levela'"><div class="levela"><xsl:apply-templates/></div></xsl:for-each></xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>