Subject: RE: [xsl] Looping in XSLT(old question, but maybe new problem) From: Jeff Beadle <Jbeadle@xxxxxxxx> Date: Tue, 24 Jun 2003 09:39:45 -0400 |
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: [xsl] 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: [xsl] 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()>=$pos and position()<($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 > $foo-bar-cnt - $pos"> <xsl:for-each select="$foo-bar[position() >= 1 and position()<=($N - ($foo-bar-cnt mod $N))]"> <td> </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: [xsl] 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> </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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Looping in XSLT(old quest, Liu Shuai | Thread | RE: [xsl] Looping in XSLT(old quest, Liu Shuai |
RE: [xsl] Different Colors for Alte, Jarno . Elovirta | Date | RE: [xsl] xml (source) file name in, Da Costa Martins, Io |
Month |