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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:58:48+00:00 2026-05-30T06:58:48+00:00

I am creating an application that creates an XML as follows: <?xml version=1.0 encoding=ISO-8859-1?>

  • 0

I am creating an application that creates an XML as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="MyCDCatalog.xsl"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <price>9.90</price>
        <year>1982</year>
    </cd>
    <cd>
        <title>One night only</title>
        <artist>Bee Gees</artist>
        <price>10.90</price>
        <year>1998</year>
    </cd>
    <cd>
        <title>Sylvias Mother</title>
        <artist>Dr.Hook</artist>
        <price>8.10</price>
        <year>1973</year>
    </cd>
    <cd>
        <title>Maggie May</title>
        <artist>Rod Stewart</artist>
        <price>8.50</price>
        <year>1990</year>
    </cd>
</catalog>

I also a XSL file called MyCDCatalog.xsl (below) that transforms the above XML file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="artist" />'s <xsl:value-of select="title" /> (<xsl:value-of select="year" />):   $<xsl:value-of select="price" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Currently, the XSL file returns all the entries in a single column. How do I get my output in 3 or more columns?

I want the output as follows:

[cd1] [cd2] [cd3]
[cd4] [cd5] [cd6]....

where

cd1 = Bob Dylan's Empire Burlesque (1985): $10.90
cd2 = Bonnie Tyler's Hide your heart (1988): $9.90
etc.

Thanks for the help.

  • 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-30T06:58:50+00:00Added an answer on May 30, 2026 at 6:58 am

    Another way to do this is to match the cd in the first, fourth, seventh (etc) position.

    <xsl:apply-templates select="cd[(position() - 1) mod $columns = 0]" mode="first"/>
    

    Where $columns is a parameter containing the number of columns

    You can then match the cd elements in the row by looking at the relevant number of following siblings:

    <xsl:apply-templates select=".|following-sibling::cd[position() &lt; $columns]"/>
    

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html" indent="yes" omit-xml-declaration="yes" />
       <xsl:param name="columns" select="3"/>
    
       <xsl:template match="/catalog">
          <html>
             <body>
                <h2>My CD Collection</h2> 
                <table>
                   <xsl:apply-templates select="cd[(position() - 1) mod $columns = 0]" mode="first"/>
                </table>
             </body>
          </html>
       </xsl:template>
    
       <xsl:template match="cd" mode="first">
          <tr>
             <xsl:apply-templates select=".|following-sibling::cd[position() &lt; $columns]"/>
             <xsl:if test="count(following-sibling::cd) &lt; ($columns - 1)">
                <xsl:call-template name="emptycell">
                   <xsl:with-param name="cells" select="$columns - 1 - count(following-sibling::cd)"/>
                </xsl:call-template>
             </xsl:if>
          </tr>
       </xsl:template>
    
       <xsl:template match="cd">
          <td>
             <xsl:value-of select="concat(title, &apos; &apos;, artist)"/>
          </td>
       </xsl:template>
    
       <xsl:template name="emptycell">
          <xsl:param name="cells"/>
          <td/>
          <xsl:if test="$cells &gt; 1">
             <xsl:call-template name="emptycell">
                <xsl:with-param name="cells" select="$cells - 1"/>
             </xsl:call-template>
          </xsl:if>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output:

    <html>
       <body>
          <h2>My CD Collection</h2>
          <table>
             <tr>
                <td>Empire Burlesque Bob Dylan</td>
                <td>Hide your heart Bonnie Tyler</td>
                <td>Greatest Hits Dolly Parton</td>
             </tr>
             <tr>
                <td>One night only Bee Gees</td>
                <td>Sylvias Mother Dr.Hook</td>
                <td>Maggie May Rod Stewart</td>
             </tr>
          </table>
       </body>
    </html>
    

    (I’m only outputing name and artist here, but I am sure you can see how to change it to show more information)

    Note that there is bit of nasty recursive template to output empty cells should the number of remaining cd elements be less than the number of columns in the row.

    Change the paramater to 4, for example, then the following is output:

    <table>
      <tr>
        <td>Empire Burlesque Bob Dylan</td>
        <td>Hide your heart Bonnie Tyler</td>
        <td>Greatest Hits Dolly Parton</td>
        <td>One night only Bee Gees</td>
      </tr>
      <tr>
        <td>Sylvias Mother Dr.Hook</td>
        <td>Maggie May Rod Stewart</td>
        <td />
        <td />
      </tr>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating application that simply reads data from an XML file and displays
I'm creating an application that is able to generate xml licenses. The application is
I'm creating a little console application that creates a Lucene index from an Sql
I'm creating an Android application that requires me to create a bitmap of the
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.
I am creating an application that uses popups. However, I would like to animate
I am creating an application that will have a list of items that can
I'm creating an application that will store geolocation data for specific transactions. Should I
I am creating an application that basically places a drawable into an imageView onClick.
I am creating an application that is similar to the camera app provided in

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.