Click to See Complete Forum and Search --> : How to output table in 2 column in xsl


musicsnoopy67
September 5th, 2003, 08:26 PM
I have an xml file below which I want to use xsl to put certain information in a
table. I only want to extract particular ID's and put them in the table. The problem I am having is how to know when to put in a <tr> table return to get the table into 2 columns using xslt.

I would like the output to look like below in a table with 2 columns for only the ID's or data I want to pick out:


<tr><td>A</td><td>D</td></tr>
<tr><td>I</td><td>J</td></tr>


Any help would be greatly appreciated.

Below is the xml file.
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="parts.xsl"?>
<Data>
<PartList nID="200" name="Belts">
<Perf_Category nID="1" name="A">
</Perf_Category>
<Perf_Category nID="2" name="B">
</Perf_Category>
<Perf_Category nID="5" name="C">
</Perf_Category>
<Perf_Category nID="4" name="D">
</Perf_Category>
<Perf_Category nID="55" name="E">
</Perf_Category>
<Perf_Category nID="20" name="F" >
</Perf_Category>
<Perf_Category nID="22" name="G">
</Perf_Category>
<Perf_Category nID="29" name="H">
</Perf_Category>
<Perf_Category nID="18" name="I">
</Perf_Category>
<Perf_Category nID="8" name="J">
</Perf_Category>
<Perf_Category nID="98" name="K">
</Perf_Category>
<Perf_Category nID="31" name="L">
</Perf_Category>
</PartList>
</Data>

musicsnoopy67
September 9th, 2003, 12:25 PM
I found the answer to my question if anyone needs to know...example below is an xsl file which picks out particular names...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- <xsl:output omit-xml-declaration="yes" />-->

<xsl:variable name="numCols" select="2" />

<xsl:template match="/">
<table>
<xsl:apply-templates select="/phonelist/person/name[.='Adrian' or .='Henry' or .='Ian' or .='Nick']"
mode="multiColumn" >
<xsl:with-param name="numCols" select="$numCols" />

<xsl:with-param name="nodes"
select="/phonelist/person/name[.='Adrian' or .='Henry' or .='Ian' or .='Nick']" />
</xsl:apply-templates>
</table>

</xsl:template>


<xsl:template match="*" mode="multiColumn">
<xsl:param name="nodes" select="/.." />
<xsl:param name="numCols" select="1" />
<xsl:variable name="vCurPosition" select="position()" />

<xsl:if test="$vCurPosition mod $numCols = 1">

<!-- alternative coloring for odd/even row -->
<xsl:variable name="color"> <xsl:choose> <xsl:when test="$vCurPosition mod (2* $numCols) = 1">aqua</xsl:when>
<xsl:otherwise>red</xsl:otherwise> </xsl:choose>

</xsl:variable>


<tr bgcolor="{$color}">
<xsl:apply-templates
mode="normal"
select="$nodes[position()&gt;=$vCurPosition and position() &lt; $vCurPosition+$numCols
] "

/>


</tr>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="normal">

<td> <xsl:value-of select="." />

</td>
</xsl:template >
</xsl:stylesheet>

The xml file for this example shows below:
<phonelist datecreated="10/01/2001">
<person>
<name>Adrian</name>
</person>
<person>
<name>Alex</name>
</person>
<person>
<name>Andrew</name>
</person>
<person>
<name>Hayley</name>
</person>
<person>
<name>Henry</name>
</person>
<person>
<name>Ian</name>
</person>
<person>
<name>Ivan</name>
</person>
<person>
<name>Jeanne</name>
</person>
<person>
<name>John</name>
</person>
<person>
<name>Kurt</name>
</person>
<person>
<name>Lesley</name>
</person>
<person>
<name>Martin</name>
</person>
<person>
<name>Nick</name>
</person>
<person>
<name>Olivia</name>
</person>
</phonelist>