tags:

views:

39

answers:

2

Let's say I have a list of 500 names in XML tags. I want to display this list in a table using XSLT. In the XSLT table I want to have a maximum of three rows; I don't care how many columns I have - I plan to put the table into a scrollable div. If my list of names grows to 1,000 I still want only three rows, but the amount of columns can grow to whatever size it needs to.

How can I do this in XSLT? As far as XSL tables go, I'm familiar with the xsl:for-each, but that's about it.

The format is something like this:

< node1 > < node2 > < NAME >data< /NAME > < NAME >data< /NAME > ... < /node2 > < /node1 >

A: 

If you are talking about transforming XML into a HTML table using XSLT I'd recommend taking a look at this article. He provides some templates that will allow you to do what you are talking about, but it will probably take a little tweaking.

Abe Miessler
+1  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pnumRows" select="3"/>

 <xsl:variable name="vnumCells" select="count(/*/*/NAME)"/>
 <xsl:variable name="vcellsPerRow"
       select="ceiling($vnumCells div $pnumRows)"/>


 <xsl:template match="node2">
  <table>
    <xsl:apply-templates select="NAME[position() mod $vcellsPerRow = 1]"/>
  </table>
 </xsl:template>

 <xsl:template match="NAME">
  <tr>
    <xsl:apply-templates mode="copy" select=
    ". | following-sibling::*[not(position() >= $vcellsPerRow)]"/>
  </tr>
 </xsl:template>

 <xsl:template match="NAME" mode="copy">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>

when performed on any XML document with the specified format, such as the following:

<node1>
    <node2>
        <NAME>name1</NAME>
        <NAME>name2</NAME>
        <NAME>name3</NAME>
        <NAME>name4</NAME>
        <NAME>name5</NAME>
        <NAME>name6</NAME>
        <NAME>name7</NAME>
        <NAME>name8</NAME>
        <NAME>name9</NAME>
        <NAME>name10</NAME>
    </node2>
</node1>

produces the wanted result: a table with three rows, where the first two rows have the same number of cells and the last row may be shorter:

    <table>
   <tr>
      <td>name1</td>
      <td>name2</td>
      <td>name3</td>
      <td>name4</td>
   </tr>
   <tr>
      <td>name5</td>
      <td>name6</td>
      <td>name7</td>
      <td>name8</td>
   </tr>
   <tr>
      <td>name9</td>
      <td>name10</td>
   </tr>
</table>
Dimitre Novatchev