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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:27:00+00:00 2026-05-19T16:27:00+00:00

Let’s say I have this XML node: <items> <item>…<item> <item>…<item> <item>…<item> <item>…<item> <item>…<item> …

  • 0

Let’s say I have this XML node:

<items>
    <item>...<item>
    <item>...<item>
    <item>...<item>
    <item>...<item>
    <item>...<item>
    ...
</items>

where there are N item nodes.

Now I would like to transform it into an HTML table with 4 columns. (e.g. if N=12, there are 3 complete rows, and if N=27, there are 7 rows, the last having 3 cells)

How could I go about doing this?

My gut call is to do it this way, where {{something}} is what I don’t know how to implement:

<xsl:template match="items">
   <table>
      <xsl:call-template name="partition-items">
         <xsl:with-param name="skip" select="0" />
      </xsl:call-template>
   </table>
</xsl:template> 

<xsl:template name="partition-items">
    <xsl:param name="skip" />
    {{ if # of items in current node > $skip,
          output a row, 
          and call partition-items($skip+4)
    }}
<xsl:template />

The pieces I don’t know how to implement, are

  • how to make a predicate for testing the # of item elements in the current node
  • how to get the Nth item element in the current node

Update from comments

How to pad the last row with empty
<td /> elements so that each row
contains exactly the wanted cells?

  • 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-19T16:27:00+00:00Added an answer on May 19, 2026 at 4:27 pm

    That’s my working solution.

    As you didn’t provide a desired output, this particular one may be uncomplete for your needs.

    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" indent="yes"/>
    
        <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="4"/>
    
            <tr>
                <xsl:apply-templates select="$nodelist[
                                not(position() > $columns-number)
                                ]"/>
            </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:stylesheet>
    

    Test input:

    <items>
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
        <item>7</item>
        <item>8</item>
        <item>9</item>
        <item>10</item>
        <item>11</item>
        <item>12</item>
        <item>13</item>
        <item>14</item>
        <item>15</item>
        <item>16</item>
        <item>17</item>
        <item>18</item>
        <item>19</item>
        <item>20</item>
        <item>21</item>
        <item>22</item>
        <item>23</item>
        <item>24</item>
        <item>25</item>
        <item>26</item>
        <item>27</item>
    </items>
    

    Output:

    <table>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <td>5</td>
            <td>6</td>
            <td>7</td>
            <td>8</td>
        </tr>
        <tr>
            <td>9</td>
            <td>10</td>
            <td>11</td>
            <td>12</td>
        </tr>
        <tr>
            <td>13</td>
            <td>14</td>
            <td>15</td>
            <td>16</td>
        </tr>
        <tr>
            <td>17</td>
            <td>18</td>
            <td>19</td>
            <td>20</td>
        </tr>
        <tr>
            <td>21</td>
            <td>22</td>
            <td>23</td>
            <td>24</td>
        </tr>
        <tr>
            <td>25</td>
            <td>26</td>
            <td>27</td>
        </tr>
    </table>
    

    Do note: you can pass columns number dynamically.

    Additional requirements and edit.

    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:my="http://localhost"
        exclude-result-prefixes="my">
        <xsl:output method="html" indent="yes"/>
    
        <my:layout>
            <td/><td/><td/><td/>
            <td/><td/><td/><td/>
            <td/><td/><td/><td/>
            <td/><td/><td/><td/>
        </my:layout>
    
        <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="4"/>
    
            <tr>
                <xsl:apply-templates select="$nodelist[
                                not(position() > $columns-number)
                                ]"/>
                <xsl:if test="count($nodelist) &lt; $columns-number">
                    <xsl:copy-of select="document('')/*/my:layout/td[
                        position() &lt;= $columns-number - count($nodelist)
                        ]"/>
                </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:stylesheet>
    

    It can be applied to the previous sample or to this concise XML:

    <items>
        <item>1</item>
    </items>
    

    Result will be:

    <table>
        <tr>
            <td>1</td>
            <td xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://localhost"></td>
            <td xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://localhost"></td>
            <td xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://localhost"></td>
        </tr>
    </table>
    

    Do note:

    1. Hardcoded data to add elements, when there are less item elements than number of columns is.
    2. Extra hardcoded elements, if number of columns will ever change.

    If there won’t ever be less elements than number of columns, you can just apply to the item elements with the same predicate and a different mode.

    And last edit. With a counted loop.

    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" indent="yes"/>
    
        <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="4"/>
    
            <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>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say you have an XML like this: <?xml version=1.0 encoding=utf-8?> <Class HashCode=307960707> <Person>
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's consider the following XML document: <items> <item>item1</item> <item>item2</item> </items> Now, let's remove all
Let's say that I have classes like this: public class Parent { public int
Let me explain best with an example. Say you have node class that can
Let's say I have some text as follows: do this, do that, then this,
Let's say I create an object like this: Person: NSString *name; NSString *phone; NSString
Let's say I have an abstract parent class called shape, and that there are
Let me frame it this way.. Say I have an application server running on

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.