Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Different table columns for odd and even elements [Thread Next] Re: Different table columns for odd and even elementsTo: NULL Date: 6/1/2006 10:11:00 AM
"ScottL" <slanders770@m...> wrote in message
news:uzIonoBhGHA.3652@T......
> I have a XML doc similar to the following:
>
> <Person ID="A" Name="Steve">
> <Person ID="B" Name="Jane">
> <Person ID="C" Name="Joe">
> <Person ID="D" Name="Bob">
> <Person ID="E" Name="Dan">
> <Person ID="F" Name="Harry">
>
> I want to represent it in a table as follows:
> Person Name Person Name
> A Steve B Jane
> C Joe D Bob
> E Dan F Harry
>
> The XSL below is incomplete but it will give you an idea of (how bad I am
at
> XSL) how far I have gotten.
>
> <xsl:variable name="Persons" select="/Person"/>
> <table border="1">
>
> <xsl:for-each select="$Persons">
> <xsl:if test="position() mod 2 = 1">
> <xsl:for-each select="@*">
> <tr>
> <td>
> <xsl:value-of select="name()"/>
> </td>
> <td>
> <xsl:value-of select="."/>
> </td>
> </tr>
>
> </xsl:for-each>
> </xsl:if>
>
>
> <xsl:if test="position() mod 2 = 0">
> <xsl:for-each select="@*">
> <td>
> <xsl:value-of select="name()"/>
> </td>
> <td>
> <xsl:value-of select="."/>
> </td>
> </xsl:for-each>
> </xsl:if>
>
> </xsl:for-each>
>
> </table>
>
> Can anyone help me with this. I am currenlty use the position() mod 2 = 0
> and position() mod 2 = 1 expressions to get odd and even elements but I
> cannot get them to dispay in the appropriate columns.
> Any ideas would be greatly appreciated.
>
> Thanks!
>
> Scott Landers
>
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" />
<xsl:param name="cellCount" select="2" />
<xsl:template match="Persons">
<table rules="all">
<thead>
<tr>
<xsl:call-template name="headers">
<xsl:with-param name="depth" select="$cellCount" />
</xsl:call-template>
</tr>
</thead>
<xsl:for-each select="Person[position() mod $cellCount = 1]">
<tr>
<xsl:for-each select=". | following-sibling::Person[position() <
$cellCount]">
<td><xsl:value-of select="@ID" /></td>
<td><xsl:value-of select="@Name" /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template name="headers">
<xsl:param name="depth" />
<xsl:if test="$depth > 0">
<th>ID</th><th>Name</th>
<xsl:call-template name="headers">
<xsl:with-param name="depth" select="$depth - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
You can control the number or records presented per row by changing the
cellCount parameter. If using an XSL Processor you can do this
programmatically.
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
