[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Looping in XSLT(old question, but maybe new proble

Subject: RE: Looping in XSLT(old question, but maybe new problem)
From: Jeff Beadle <Jbeadle@xxxxxxxx>
Date: Tue, 24 Jun 2003 00:11:53 -0400
RE:  Looping in XSLT(old question
Hey Liu,

Fun one!  I had one of those rare moments where I actually had a bit of time
to work on one of the problems/questions raised on this list.

Also, please be advised ... other members on this list--the experts--will
most likely provide direction and/or solutions well beyond what I'm
providing, so you may want to wait a bit to see what they contribute.

But hey, this works!

<xsl:param name="N" select="5"/>
<xsl:template match="/">
  <table>
    <xsl:variable name="foo-bar" select="foo/bar"/>
    <xsl:variable name="foo-bar-cnt" select="count($foo-bar)"/>		
    <xsl:for-each select="$foo-bar">
      <xsl:variable name="pos" select="position()"/>
      <!-- if at the Nth column, then create a new row -->
      <xsl:if test="not(($pos - 1) mod $N)">
        <tr>
          <!-- grab the splice ... all bars that are within the range
[$pos,$pos+$N] -->
          <xsl:for-each select="$foo-bar[position()&gt;=$pos and
position()&lt;($pos + $N)]">
            <td><xsl:value-of select="someElement"/></td>
          </xsl:for-each>
          <!-- if within the last row, construct enough empty cells to
"square" the table -->
          <xsl:if test="$foo-bar-cnt mod $N &gt; $foo-bar-cnt - $pos">
            <xsl:for-each select="$foo-bar[position() &gt;= 1 and
position()&lt;=($N - ($foo-bar-cnt mod $N))]">
              <td>&#160;</td>
            </xsl:for-each>
          </xsl:if>
        </tr>
      </xsl:if>
    </xsl:for-each>
  </table>
</xsl:template>

Anyhow, it was fun.

-Jeff

-----Original Message-----
From: Liu Shuai [mailto:shuai@xxxxxxxxxxxxxxxxx]
Sent: Monday, June 23, 2003 8:32 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  Looping in XSLT(old question, but maybe new problem)


Hi, all

I am trying to write a template that will generate a table based on a xml
file and right now I don't have any
clue how to do it.

If I have a source file looks like this

<foo>
	<bar id='1'>
		<someElement>a</someElement>
	</bar>
	<bar id='2'>
		<someElement>b</someElement>
	</bar>
	<bar id='3'>
		<someElement>c</someElement>
	</bar>
	<bar id='4'>
		<someElement>d</someElement>
	</bar>
	<bar id='5'>
		<someElement>e</someElement>
	</bar>
	...
</foo>

Can I write a style sheet that will transform the source file above to a
html table like this?

<table>
	<tr>
		<td>a</td>
		<td>b</td>
		<td>c</td>
	</tr>
	<tr>
		<td>d</td>
		<td>e</td>
		<td>&nbsp;</td>
	</tr>
	...
</table>

Basicly, I want to generate N columns per row but I don't know how many
"bar"s I have in the source file.

Any hint or suggestion will be greatly appreciated.

Shuai


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.