RE: [xsl] Flat List to HTML Table

Subject: RE: [xsl] Flat List to HTML Table
From: Pieter Reint Siegers Kort <pieter.siegers@xxxxxxxxxxx>
Date: Mon, 25 Oct 2004 19:04:58 -0500
Hi Peter,

Take a look at the following stylesheet - it may well give you some ideas on
how to solve your problem:

***********************************
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="msxsl local xql"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:local="#local-functions"
xmlns:xql="#xql-functions">

<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="/">
	<html>
	<head>
	<title>alternating rows</title>
	</head>
	<body bgcolor="#efc6ff">
		<table border="1">
		<tr><td>
		<table border="1" cellpadding="3" cellspacing="0"
width="600">
			<tr>
				<th align="left" bgcolor="Lightblue"><font
color="white">name</font></th>
				<th align="left" bgcolor="Lightblue"><font
color="white">value</font></th>
				<th align="left" bgcolor="Lightblue"><font
color="white">name</font></th>
				<th align="left" bgcolor="Lightblue"><font
color="white">value</font></th>
			</tr>
			<xsl:apply-templates select="list/item"/>
		</table>
		</td></tr>
		</table>
	</body>
	</html>
</xsl:template>

<xsl:template match="item">
    <xsl:choose>
      <xsl:when test="position() mod 2 = 0">
      </xsl:when>
      <xsl:otherwise>
	  <tr>
		<xsl:attribute name="bgcolor">
        <xsl:choose>
          <xsl:when test="position() mod 2 = 0">#ccfec7</xsl:when>
          <xsl:otherwise>#feffc6</xsl:otherwise>
        </xsl:choose>
        </xsl:attribute>
		<td>
			<xsl:value-of select="@name"/>
		</td>
		<td>
			<xsl:value-of select="."/>
		</td>
		<td>
        <xsl:choose>
          <xsl:when test="following-sibling::item/@name">
			<xsl:value-of
select="following-sibling::item/@name"/>
          </xsl:when>
          <xsl:otherwise>
          &#160;
          </xsl:otherwise>
        </xsl:choose>
		</td>
		<td>
        <xsl:choose>
          <xsl:when test="following-sibling::item/.">
			<xsl:value-of select="following-sibling::item/."/>
          </xsl:when>
          <xsl:otherwise>
          &#160;
          </xsl:otherwise>
        </xsl:choose>
		</td>
	  </tr>
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>
***********************************

HTH,
<prs/> 

-----Original Message-----
From: Peter Charles [mailto:petefc@xxxxxxxxxxxxxxxxx] 
Sent: Monday, October 25, 2004 5:02 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Flat List to HTML Table

Hello,

If I have a list that looks like this:

<list>
    <item name="name1">value1</item>
    <item name="name2">value2/item>
    <item name="name3">value3</item>
    <item name="name4">value4</item>
    <item name="name5">value5</item>
    <item name="name6">value6</item>
    <item name="name7">value7</item>
    <item name="name8">value</item>
    <item name="name9">value9</item>
</list>

And  I want to turn it into a table that looks like this;

<table>
    <tr>
        <td>name1</td>
        <td>value1</td>
        <td>name2</td>
        <td>value2</td>
    </tr>
    <tr>
        <td>name3</td>
        <td>value3</td>
        <td>name4</td>
        <td>value4</td>
    </tr>
    <tr>
        <td>name5</td>
        <td>value5</td>
        <td>name6</td>
        <td>value6</td>
    </tr>
    <tr>
        <td>name7</td>
        <td>value7</td>
        <td>name8</td>
        <td>value8</td>
    </tr>
    <tr>
        <td>name9</td>
        <td>value9</td>
    </tr>
</table>

Can any one suggest a good method for doing so?  I thought about using
template match patterns of:

<xsl:template match="item[position() mod 2=0]">

And
<xsl:template match="item[position() mod 2!=0]">

to let me know if list item is the beginning or ending of a row but that
does not solve the problem of how/when to open and close rows.

Thanks

Peter

Current Thread