[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: "Liu Shuai" <shuai@xxxxxxxxxxxxxxxxx>
Date: Tue, 24 Jun 2003 18:50:37 -0400
xsl for each n times
Thank you Jeff, that was indeed very quick response.

Just one question, if foo-bar-temp is a global variable, is the nodeset
initialized
only once or is it generated every time foo-bar-temp is called?

Regards,
LS
-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Jeff Beadle
Sent: Tuesday, June 24, 2003 9:40 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE:  Looping in XSLT(old question, but maybe new problem)


Hey Liu,

Just replace the xsl:variable[@name='foo-bar'] definition with this:

<!-- temp table ... er, excuse me ... I mean, temp node list -->
<xsl:variable name="foo-bar-temp">
<xsl:for-each select="foo/bar">
<xsl:sort select="@id" data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<!--
Now, construct the foo-bar node list from the sorted temp list;
Doing this instead of rolling the for-each directly within the foo-bar will
prevent you from
having to use your parsers node-set/node-list adapter function to turn the
RTF into a node list.
 -->
<xsl:variable name="foo-bar" select="msxsl:node-set($foo-bar-temp)/bar"/>

Sorry, for the delay ... just logged into my mail server.

-Jeff

-----Original Message-----
From: Liu Shuai [mailto:shuai@xxxxxxxxxxxxxxxxx]
Sent: Tuesday, June 24, 2003 12:52 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE:  Looping in XSLT(old question, but maybe new problem)


Jeff,

Thank you for your response.
One concern, though. If I sort foo/bar, will foo/bar[position()] return
nodes based in my sorted order
or original order in the source file? I used following-sibling in a template
I wrote and it return nodes
in the orginal order.

Thanks!
LS


-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Jeff Beadle
Sent: Tuesday, June 24, 2003 12:12 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE:  Looping in XSLT(old question, but maybe new problem)


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


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

 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.