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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:32:38+00:00 2026-06-12T18:32:38+00:00

My input XML document is a simple list of items. The number of items

  • 0

My input XML document is a simple list of items. The number of items is arbitrary:

<items>
  <item name="item1"/>
  <item name="item2"/>
  <item name="item3"/>
  ...
  <item name="itemX"/>
</items>

Now, I want to split this list into HTML tables. The number of rows and columns are given as parameter values:

<xsl:param name="rows"/>
<xsl:param name="cols"/>

If we let rows be 3, and cols be 2, the resulting HTML should look like:

<table>
  <tr>
    <td>item1</td>
    <td>item2</td>
  </tr>
  <tr>
    <td>item3</td>
    <td>item4</td>
  </tr>
  <tr>
    <td>item5</td>
    <td>item6</td>
  </tr>
</table>

<table>
  <tr>
    <td>item7</td>
    <td>item8</td>
  </tr>
  <tr>
    <td>item9</td>
    <td>item10</td>
  </tr>
  <tr>
    <td>item11</td>
    <td>item12</td>
  </tr>
</table>
...

The number of <table>s created is thus ceil(number_of_items / rows / cols)

I have a basic idea how to solve this, but I can’t seem to get the last tweaks right. The following stylesheet produces something close to what I want, but item 4, 7, 10 and 13 are duplicated.
Does anyone have a better idea about how to do this?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="cols" select="2"/>
<xsl:param name="rows" select="3"/>

<xsl:template match="/*">
  <html>
    <head/>
    <body>
      <xsl:apply-templates select="*[position() mod ($cols * $rows) = 1]" mode="table"/>
     </body>
   </html>
</xsl:template>

<xsl:template match="*" mode="table">
    <table border="1" id="{@name}">
        <xsl:apply-templates select="." mode="row"/>
        <xsl:apply-templates select="following-sibling::*[position() &gt; 1 and position() mod $rows = 0]" mode="row"/>
     </table>
</xsl:template>

<xsl:template match="*" mode="row">
    <tr id="{@name}">
       <xsl:apply-templates select="." mode="cell"/>
       <xsl:apply-templates select="following-sibling::*[position() &lt; $cols]" mode="cell"/>
    </tr>
</xsl:template>

<xsl:template match="*" mode="cell">
    <td>
        <xsl:apply-templates select="."/>
    </td>
</xsl:template>

<xsl:template match="item">
    <xsl:value-of select="@name"/>
</xsl:template>

</xsl:stylesheet>
  • 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-06-12T18:32:40+00:00Added an answer on June 12, 2026 at 6:32 pm

    You can try to add +1 to $cols like this:

    <xsl:template match="*" mode="row">
    <tr id="{@name}">
       <xsl:apply-templates select="." mode="cell"/>
       <xsl:apply-templates select="following-sibling::*[position() &lt; ($cols +1)]" mode="cell"/>
    </tr>
    </xsl:template>
    

    Try this for the table template (you have to limit the amount of items per table):

    <xsl:template match="*" mode="table">
    <xsl:variable name="mypos" select="position()"/>
    <table border="1" id="{@name}" test="{$mypos}">
        <xsl:apply-templates select="." mode="row"/>
        <xsl:apply-templates select="following-sibling::*[position() &gt; 1 and position() mod  $rows = 0 and position() &lt; $mypos * ($cols * $rows)]" mode="row"/>
     </table>
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My sample input XML is: <root> <a> <b>item</b> <b>item1</b> <b>item2</b> <b>item3</b> <b>item4</b> </a> </root>
I have the following input XML: <root age=1> <description>some text</description> <section> <item name=a> <uuid>1</uuid>
How to copy xml document skipping some top level nodes. For example: Input: <root>
I am looking to transform a input xml given below <profile name=default> <color id=forecolor
I'm a beginner at XSL FO. I have a very simple XML document :
I am passing a XML document as a input to a stored procedure in
Here is something so simple <p:commandLink value=Tom onclick=document.getElementById('tom').focus()/><br/> <input id=tom/> When u click on
My input XML consists of the following, <root> <entry> <type>U</type> <value>111</value> </entry> <entry> <type>X</type>
The input XML tag must be validated for a pattern which is like this:
i have input xml as <content> <date> <day>14</day> <month>06</month> <year>2012</year> </date> </content> want it

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.