RE: [xsl] Looping in XSLT(old question, but maybe new problem)

Subject: RE: [xsl] Looping in XSLT(old question, but maybe new problem)
From: "Zink, Juergen" <Juergen.Zink@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 24 Jun 2003 09:24:04 +0200
Hey Liu,

if you extend Jeff's solution to "process" the sorted inside the sorted
list.
This will have a bad performance, but keep the sorted order.

<xsl:template match="/">
  <table>
    <xsl:variable name="foo-bar" select="foo/bar"/>
    <xsl:variable name="foo-bar-cnt" select="count($foo-bar)"/>
    <xsl:variable name="N" select="'3'"/>		
    <xsl:for-each select="foo/bar">
    <xsl:sort select="@id" data-type="number"/>
    <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>
<!-- remember the position and process the sorted list again. Awful code,
but works -->
<!-- you get the complexity of o(n*n) -->
<!-- else sort first the xml file and document order will be sorted order
-->
<!-- or switch to XSL 2.0 >
        <xsl:for-each select="//foo/bar">
          <xsl:sort select="@id" data-type="number"/>
          <!-- grab the splice ... all bars that are within the range
[$pos,$pos+$N] -->
          <xsl:if test="position()&gt;=$pos and position()&lt;($pos+$N)">
            <td><xsl:value-of select="someElement"/></td>
          </xsl:if>
          <!-- if within the last row, construct enough empty cells
to"square" the table -->
          <xsl:if test="$pos+$N>position() and $foo-bar-cnt=position() and
$foo-bar-cnt mod $N" >
            <xsl:call-template name="fill">
              <xsl:with-param name="count" select="$N - ($foo-bar-cnt mod
$N)"/>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
        </tr>
      </xsl:if>
    </xsl:for-each>
  </table>
</xsl:template>

<xsl:template name="fill">
  <xsl:param name="count"/>
  <td></td>
  <xsl:if test="$count&gt;1">
    <xsl:call-template name="fill">
      <xsl:with-param name="count" select="$count -1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

Cheers,

Juergen

-----Ursprüngliche Nachricht-----
Von: Liu Shuai [mailto:shuai@xxxxxxxxxxxxxxxxx]
Gesendet am: Dienstag, 24. Juni 2003 06:52
An: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Betreff: 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()&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: [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>&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


*****************************************************************************
Diese E-Mail enthaelt vertrauliche und/oder rechtlich geschuetzte 
Informationen. Wenn Sie nicht der richtige Adressat sind oder diese 
eMail irrtuemlich erhalten haben, informieren Sie bitte sofort den 
Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren 
sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

This e-mail may contain confidential and/or privileged information. 
If you are not the intended recipient (or have received this e-mail 
in error) please notify the sender immediately and destroy this e-mail. 
Any unauthorized copying, disclosure or distribution of the material 
in this e-mail is strictly forbidden.
*****************************************************************************


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


Current Thread