?
Solved

How to show data in table?

Posted on 2009-12-28
3
Medium Priority
?
255 Views
Last Modified: 2013-11-18
How can I show data in table with four columns?
<table>
<xsl:for-each select="sth">
<tr>

<td><xsl:value-of select="something"/></td>
</tr>
</xsl:for-each>

Open in new window

0
Comment
Question by:static86
  • 3
3 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26136293
I made this source example XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <sth>
        <something>A</something>
    </sth>
    <sth>
        <something>Z</something>
    </sth>
    <sth>
        <something>E</something>
    </sth>
    <sth>
        <something>R</something>
    </sth>
    <sth>
        <something>T</something>
    </sth>
    <sth>
        <something>Y</something>
    </sth>
</root>
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26136294
Try this, the trick is to take each fourth element in the list
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:template match="root">
     <table border="1">
         <xsl:for-each select="sth[position() mod 4 = 1]">
             <tr>
                 <xsl:for-each select="self::sth | following-sibling::sth[position() &lt; 4]">
                     <td>
                         <xsl:value-of select="something"/>
                     </td>
                 </xsl:for-each>
             </tr>
         </xsl:for-each>
     </table>
 </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 26136608
Hey, I added two things
1. The number of columns is a global parameter (works only starting from 2, not 1)
2. I added a recursive template for adding empty cells
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="num-of-cols">4</xsl:param>
 <xsl:template match="root">
     <table border="1">
         <xsl:for-each select="sth[position() mod $num-of-cols = 1]">
             <tr>
                 <xsl:for-each select="self::sth | following-sibling::sth[position() &lt; $num-of-cols]">
                     <td>
                         <xsl:value-of select="something"/>
                     </td>
                 </xsl:for-each>
                 <xsl:if test="count(following-sibling::sth) &lt; $num-of-cols - 1">
                     <xsl:call-template name="add-empty-cells">
                         <xsl:with-param name="count" select="$num-of-cols - count(following-sibling::sth) - 1 "/>
                     </xsl:call-template>
                 </xsl:if>
             </tr>
         </xsl:for-each>
     </table>
 </xsl:template>
    <xsl:template name="add-empty-cells">
        <xsl:param name="count"/>
        <td>-</td>
        <xsl:if test="$count &gt; 1">
            <xsl:call-template name="add-empty-cells">
                <xsl:with-param name="count" select="$count - 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question