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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:52:42+00:00 2026-05-21T03:52:42+00:00

I am looking at a similar problem to what was covered here Transforming List

  • 0

I am looking at a similar problem to what was covered here

Transforming List into a 2-D Table

but with a slight wrinkle. My XML is not in any particular order and I would like to sort it for display. For example my XML is

<items>  
  <item>A</item>  
  <item>C</item>  
  <item>E</item>  
  <item>B</item>  
  <item>D</item> 
  <!-- ... any number of item nodes ... -->
<item>  

and I want my output to be (where I am ignoring the non-named nodes for illustrative purposes)

<table>
    <tr>
        <td>A</td>
        <td>C</td>
        <td>E</td>
    </tr>
    <tr>
        <td>B</td>
        <td>D</td>
        <td />
    </tr>
</table>

The XSL I am basing this off is from the above link (I need to use XSL 1.0):

<xsl:template match="/*">
    <table>
        <xsl:call-template name="make-columns">
            <xsl:with-param name="nodelist" select="item"/>
        </xsl:call-template>
    </table>
</xsl:template>

<xsl:template name="make-columns">
    <xsl:param name="nodelist"/>
    <xsl:param name="columns-number" select="3"/>

    <tr>
        <xsl:apply-templates select="$nodelist[
                        not(position() > $columns-number)
                        ]"/>
        <xsl:if test="count($nodelist) &lt; $columns-number">
            <xsl:call-template name="empty-cells">
                <xsl:with-param name="finish" 
                                select="$columns-number - count($nodelist)"/>
            </xsl:call-template>
        </xsl:if>
    </tr>

    <!-- If some nodes are left, recursively call current
    template, passing only nodes that are left -->
    <xsl:if test="count($nodelist) > $columns-number">
        <xsl:call-template name="make-columns">
            <xsl:with-param name="nodelist" select="$nodelist[
                                    position() > $columns-number
                                    ]"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template match="item">
    <td>
        <xsl:apply-templates/>
    </td>
</xsl:template>

<xsl:template name="empty-cells">
    <xsl:param name="finish"/>
    <td/>
    <xsl:if test="not($finish = 1)">
        <xsl:call-template name="empty-cells">
            <xsl:with-param name="finish" select="$finish - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

I have tried inserting commands within the various apply-templates but that doesn’t work.

ideas?

Jeff

Update from comments

I want to output a multicolum table
with 3 columns where the entries are
in alphabetical order vertically

  • 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-21T03:52:42+00:00Added an answer on May 21, 2026 at 3:52 am

    This transformation:

    <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:param name="pNumCols" select="3"/>
    
     <xsl:variable name="vNumRows" select=
          "ceiling(count(/*/*) div $pNumCols)"/>
    
     <xsl:variable name="vrtfSorted">
       <xsl:for-each select="/*/*">
        <xsl:sort/>
        <xsl:copy-of select="."/>
       </xsl:for-each>
     </xsl:variable>
    
     <xsl:variable name="vSorted"
          select="ext:node-set($vrtfSorted)/*"/>
    
     <xsl:template match="/">
      <table>
       <xsl:apply-templates select=
         "$vSorted[not(position() > $vNumRows)]"/>
      </table>
     </xsl:template>
    
     <xsl:template match="item">
      <tr>
       <xsl:apply-templates select=
       "(.|following-sibling::*[position() mod $vNumRows =0])/text()"/>
      </tr>
     </xsl:template>
    
     <xsl:template match="text()">
      <td><xsl:value-of select="."/></td>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <items>
        <item>A</item>
        <item>C</item>
        <item>E</item>
        <item>B</item>
        <item>D</item>
    </items>
    

    produces the wanted, correct result:

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

Sidebar

Related Questions

I tried looking for a similar problem but I could not find a similar
I tried looking into similar problems, but the solutions offered there do not seem
I've tried looking at similar problems, but could not easily find one that helped
I'm looking for something similar to the table creation form from phpmyadmin, but it
Although it is looking similar to my previous post but here purpose is different.
I've tried looking around and there are similar problems, but mine is way more
I've been looking at similar questions but they are more on how to get
Looking for something similar to xerces for parsing an xml file in ruby. I
I am looking for something similar to array_chunk() but using an empty value as
I'm having a problem with a site created in Drupal 6 but not compatable

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.