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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:11+00:00 2026-05-27T14:52:11+00:00

I have few titles which I am getting by having for loop around some

  • 0

I have few titles which I am getting by having for loop around some xml(It can be n no. of titles). I want to display them horizontally in 3 columns but vertically in alphabetical order:

If I have 3 titles, I am representing them with just alphabet(I can get the count of no. of titles.

A              B              C                    -----count(3)

4 titles:
A              C              D                    -----count(4)
B    

5 Titles: 
A              C              E                    -----count(5)
B              D   

7 Titles:
A              D              F                     -----count(7)
B              E              G
C

I am using xsl 1.0 and right now I have it like

<div class="navigation">
<ul>
<xsl:foreach select="/Custom/Alphabet/titles">

   <li>
     <xsl:value-of select="." />
   </li>
</xsl:foreach>
</ul>
</div>
  • 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-27T14:52:12+00:00Added an answer on May 27, 2026 at 2:52 pm

    Excellent question!

    I. Here is an XSLT 2.0 solution (65 lines, can be converted to XSLT 1.0 almost mechanically):

    <xsl:stylesheet version="2.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:xs="http://www.w3.org/2001/XMLSchema"
         xmlns:my="my:my" exclude-result-prefixes="xs my">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
         <xsl:variable name="vItems" select="/*/*/string(.)"
          as="item()+"/>
    
    
         <xsl:template match="/">
          <xsl:sequence select="my:fill($vItems, 3)"/>
         </xsl:template>
    
         <xsl:function name="my:fill" as="element()+">
          <xsl:param name="pItems" as="item()*"/>
          <xsl:param name="pK" as="xs:integer"/>
    
          <xsl:variable name="pN" select="count($pItems)"/>
    
          <xsl:choose>
           <xsl:when test="$pN le $pK">
            <xsl:sequence select="my:fillRow($pItems)"/>
           </xsl:when>
           <xsl:otherwise>
            <xsl:variable name="vColSize"
             select="ceiling($pN div $pK)"/>
    
            <xsl:variable name="vCol-1" select=
             "$pItems[position() le $vColSize]"/>
    
            <xsl:variable name="vSubTable"
             select="my:fill($pItems[position() gt $vColSize],
                             $pK -1
                            )
             "/>
    
             <xsl:sequence select="my:merge($vCol-1, $vSubTable)"/>
           </xsl:otherwise>
          </xsl:choose>
         </xsl:function>
    
         <xsl:function name="my:fillRow" as="element()">
          <xsl:param name="pItems" as="item()*"/>
    
          <row>
           <xsl:for-each select="$pItems">
            <cell><xsl:sequence select="."/></cell>
           </xsl:for-each>
          </row>
         </xsl:function>
    
         <xsl:function name="my:merge" as="element()*">
          <xsl:param name="pCol" as="item()*"/>
          <xsl:param name="pTable" as="element()*"/>
    
          <xsl:for-each select="$pCol">
           <xsl:variable name="vrowPos" select="position()"/>
           <row>
            <cell><xsl:sequence select="."/></cell>
            <xsl:sequence select="$pTable[position() eq $vrowPos]/cell"/>
           </row>
          </xsl:for-each>
         </xsl:function>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided (most complex) 7-items case:

    <titles>
     <t>A</t>
     <t>B</t>
     <t>C</t>
     <t>D</t>
     <t>E</t>
     <t>F</t>
     <t>G</t>
    </titles>
    

    the wanted, correct result is produced:

    <row>
       <cell>A</cell>
       <cell>D</cell>
       <cell>F</cell>
    </row>
    <row>
       <cell>B</cell>
       <cell>E</cell>
       <cell>G</cell>
    </row>
    <row>
       <cell>C</cell>
    </row>
    

    I have verified that the expected, correct result is produced for every N = 1 to 7.

    Explanation:

    We are building the required table recursively on the number of items in the input sequence (pN):

    1. The base of the recursion is for any $pN not greater than $pK (the required number of columns). In this basic case the table has a single row.

    2. In the general case $pN > $pK ; then we build the leftmost column $vCol-1 and, recursively, a smaller table with the rest of the items and new number of required columns: $pK -1.

    3. In case 2. above, we finally merge the column and the sub-table to produce the resulting table.


    II. Equivalent XSLT 2.0 solution, writenn in a “more XSLT 2.0 style” (60 lines):

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:my="my:my" exclude-result-prefixes="xs my">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:variable name="vItems" select="/*/*/string(.)"
      as="item()+"/>
    
    
     <xsl:template match="/">
      <xsl:sequence select="my:fill($vItems, 3)"/>
     </xsl:template>
    
     <xsl:function name="my:fill" as="element()+">
      <xsl:param name="pItems" as="item()*"/>
      <xsl:param name="pK" as="xs:integer"/>
    
      <xsl:sequence select=
       "for $vN in count($pItems)
         return
           if($vN le $pK)
             then my:fillRow($pItems)
             else
               (for $vColSize in xs:integer(ceiling($vN div $pK))
                 return
                   my:merge((for $i in 1 to $vColSize
                               return $pItems[$i]),
                                 my:fill((for $i in $vColSize+1 to $vN
                                           return $pItems[$i]),
                                          $pK -1
                                         )
                            )
                )
       "/>
     </xsl:function>
    
     <xsl:function name="my:fillRow" as="element()">
      <xsl:param name="pItems" as="item()*"/>
    
      <row>
       <xsl:for-each select="$pItems">
        <cell><xsl:sequence select="."/></cell>
       </xsl:for-each>
      </row>
     </xsl:function>
    
     <xsl:function name="my:merge" as="element()*">
      <xsl:param name="pCol" as="item()*"/>
      <xsl:param name="pTable" as="element()*"/>
    
      <xsl:for-each select="$pCol">
       <xsl:variable name="vrowPos" select="position()"/>
       <row>
        <cell><xsl:sequence select="."/></cell>
        <xsl:sequence select="$pTable[position() eq $vrowPos]/cell"/>
       </row>
      </xsl:for-each>
     </xsl:function>
    </xsl:stylesheet>
    

    III. XSLT 1.0 solution (75 lines)

    This is the first XSLT 2.0 solution (above), translated almost mechanically to XSLT 1.0:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
         <xsl:variable name="vItems" select="/*/*"/>
    
         <xsl:template match="/">
          <xsl:call-template name="fill">
           <xsl:with-param name="pItems" select="$vItems"/>
           <xsl:with-param name="pK" select="3"/>
          </xsl:call-template>
         </xsl:template>
    
         <xsl:template name="fill">
          <xsl:param name="pItems"/>
          <xsl:param name="pK"/>
    
          <xsl:variable name="vN" select="count($pItems)"/>
    
          <xsl:choose>
           <xsl:when test="not($vN > $pK)">
            <row>
             <xsl:call-template name="fillRow">
              <xsl:with-param name="pItems" select="$pItems"/>
             </xsl:call-template>
            </row>
           </xsl:when>
           <xsl:otherwise>
            <xsl:variable name="vColSize"
             select="ceiling($vN div $pK)"/>
    
            <xsl:variable name="vCol-1" select=
             "$pItems[not(position() > $vColSize)]"/>
    
            <xsl:variable name="vrtfSubtable">
             <xsl:call-template name="fill">
              <xsl:with-param name="pItems" select=
               "$pItems[position() > $vColSize]"/>
              <xsl:with-param name="pK" select="$pK -1"/>
             </xsl:call-template>
            </xsl:variable>
    
            <xsl:variable name="vSubTable" select=
            "ext:node-set($vrtfSubtable)/*"/>
    
             <xsl:call-template name="merge">
              <xsl:with-param name="pCol" select="$vCol-1"/>
              <xsl:with-param name="pTable" select="$vSubTable"/>
             </xsl:call-template>
           </xsl:otherwise>
          </xsl:choose>
         </xsl:template>
    
         <xsl:template name="fillRow">
          <xsl:param name="pItems"/>
    
           <xsl:for-each select="$pItems">
            <cell><xsl:value-of select="."/></cell>
           </xsl:for-each>
         </xsl:template>
    
         <xsl:template name="merge">
          <xsl:param name="pCol"/>
          <xsl:param name="pTable"/>
    
          <xsl:for-each select="$pCol">
           <xsl:variable name="vrowPos" select="position()"/>
           <row>
            <cell><xsl:value-of select="."/></cell>
            <xsl:copy-of select="$pTable[position() = $vrowPos]/cell"/>
           </row>
          </xsl:for-each>
         </xsl:template>
    </xsl:stylesheet>
    

    IV. Finally, a pure, generative (non-recursive) XSLT 1.0 solution:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:param name="pK" select="3"/>
    
     <xsl:variable name="vItems" select="/*/*"/>
    
     <xsl:template match="/">
      <xsl:call-template name="genTable"/>
     </xsl:template>
    
     <xsl:template name="genTable">
      <xsl:param name="pItems" select="$vItems"/>
      <xsl:param name="pK" select="$pK"/>
    
      <xsl:variable name="vN" select=
       "count($vItems)"/>
      <xsl:variable name="vnumRows"
         select="ceiling($vN div $pK)"/>
    
      <table>
       <xsl:for-each select=
         "$pItems[not(position() > $vnumRows)]">
         <xsl:call-template name="genRow">
          <xsl:with-param name="pRowInd" select="position()"/>
          <xsl:with-param name="pItems" select="$vItems"/>
          <xsl:with-param name="pK" select="$pK"/>
         </xsl:call-template>
       </xsl:for-each>
      </table>
     </xsl:template>
    
     <xsl:template name="genRow">
      <xsl:param name="pRowInd" select="position()"/>
      <xsl:param name="pItems" select="$vItems"/>
      <xsl:param name="pK" select="$pK"/>
    
      <xsl:variable name="vN" select=
       "count($vItems)"/>
      <xsl:variable name="vFullCols" select=
       "$vN mod $pK"/>
      <xsl:variable name="vFullColSize" select=
       "ceiling($vN div $pK)"/>
    
       <tr>
         <td><xsl:value-of select="$pItems[number($pRowInd)]"/></td>
    
       <xsl:for-each select=
        "$pItems[position() > 1
               and
                 not(position() > $pK)
                ]">
        <xsl:variable name="vX" select="position()+1"/>
    
        <xsl:variable name="vMinFullColsAndX" select=
        "($vX > $vFullCols) * $vFullCols
         +
          not($vX > $vFullCols) * $vX
        "/>
    
        <xsl:variable name="vAmmt1" select=
        "$vMinFullColsAndX * $vFullColSize
        "/>
    
        <xsl:variable name="vAmmt2" select=
        "($vX -1 - $vMinFullColsAndX) * ($vFullColSize -1)
        "/>
    
        <xsl:variable name="vValue" select=
        "$vAmmt1 + $vAmmt2"/>
    
        <xsl:if test="not(($pRowInd -1) * $pK +$vX > $vN)">
         <td><xsl:value-of select=
         "$pItems[position()=$pRowInd+$vValue]"/>
         </td>
        </xsl:if>
       </xsl:for-each>
      </tr>
    
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the same XML document (above), the wanted, correct result is produced:

    <table>
       <tr>
          <td>A</td>
          <td>D</td>
          <td>F</td>
       </tr>
       <tr>
          <td>B</td>
          <td>E</td>
          <td>G</td>
       </tr>
       <tr>
          <td>C</td>
       </tr>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few HTML pages in which I use <sup></sup> tags. I want
I have few different applications among which I'd like to share a C# enum.
I have few nested DIVs at page. I want to add event only for
I have few Question for which I am not able to get a proper
Explanation: i have few objects and im declaring them inside $(document).ready(). WHY? because in
I have a Note model, which can contain have either an image link attachment
I am developing a small application in android in which i have few image
I had a home page which have a few tables that refresh itself every
We have a form which has a few separate submit buttons which do different
I have few sets of code to display right side of my webpage. I

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.