Here is my sample xml

<STUDENT_INFOs>
<STUDENT_INFO>
<STUDENT_COUNT>1</STUDENT_COUNT>
<STDNT_ID>xxxxxxxxx</STDNT_ID>
<STDNT_NAME>someone1</STDNT_NAME>
<STREET_ADDRESS>address1</STREET_ADDRESS>
</STUDENT_INFO>
<STUDENT_INFO>
<STUDENT_COUNT>2</STUDENT_COUNT>
<STDNT_ID>yyyyyyyyy</STDNT_ID>
<STDNT_NAME>someone2</STDNT_NAME>
<STREET_ADDRESS>address2</STREET_ADDRESS>
</STUDENT_INFO>
:
:
upto 50 student info

</STUDENT_INFOs>

I would like to display this using xslt such that I have 5 student names in the first row and the next 5 in the 2nd row and so on.
How do I do that? My <xsl:for-each> is giving me 1 student per row. Hope my question is clear. Thanks for the help.

Recommended Answers

All 5 Replies

Are you trying to display in an HTML table? A table that has 5 columns of students names? What version of xslt are you using? 1.0 or 2.0?

The best way to tell us what you want is to give us the sample input and sample output that you're looking for.

Yes, I am looking to display using an html table something like

student1 student2 student3 student4 student5
      student6 student7 student8 student9 student10

and so on upto 50 students.
I do not know the version of xslt that I am using. How do I find that out?

If you don't know you're likely using 1.0 But it's right at the top of your transformation that you've written.

Quick and dirty! This solution takes your input document as defined and produces an HTML table of students names, with each row containing 5 names.

If you really want to make a robust solution for grouping you should read about muenchian grouping in XSLT 1.0. Once you mature to XSLT 2.0, this kind of solution is much simpler.
http://www.jenitennison.com/xslt/grouping/muenchian.html

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:variable name="setNumberofStudents" select="count(/STUDENT_INFOs/STUDENT_INFO)"/>

    <xsl:template match="/">
        <html>
            <body>
                <table>
                    <xsl:call-template name="getStudentRows">
                        <xsl:with-param name="currentStudent" select="1"/>
                        <xsl:with-param name="maxStudents" select="$setNumberofStudents"/>
                    </xsl:call-template>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="getStudentRows">
        <xsl:param name="currentStudent"/>
        <xsl:param name="maxStudents"/>
        <xsl:choose>
            <xsl:when test="$currentStudent &gt; $maxStudents">
            </xsl:when>
            <xsl:otherwise>
                <tr>
                    <xsl:apply-templates select="STUDENT_INFOs/STUDENT_INFO[position() &gt;= $currentStudent and position() &lt; ($currentStudent + 5)]/STDNT_NAME" />
                </tr>
                <xsl:call-template name="getStudentRows" >
                    <xsl:with-param name="currentStudent" select="($currentStudent + 5)" />
                    <xsl:with-param name="maxStudents" select="$maxStudents" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="STDNT_NAME" >
        <td>
            <xsl:value-of select="." />
        </td>
    </xsl:template>
</xsl:stylesheet>

Output:

<html>
  <body>
    <table>
      <tr>
        <td>someone1</td>
        <td>someone2</td>
        <td>someone3</td>
        <td>someone4</td>
        <td>someone5</td>
      </tr>
      <tr>
        <td>someone6</td>
        <td>someone7</td>
        <td>someone8</td>
        <td>someone9</td>
        <td>someone10</td>
      </tr>
      <tr>
        <td>someone11</td>
      </tr>
    </table>
  </body>
</html>

Beautiful. I will try this approach. Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.