Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - How to build a tlabe? >Thread Next - Re: How to build a table? Re: How to build a tlabe?To: NULL Date: 6/5/2006 10:00:00 AM
anotherron wrote:
> I am trying to build a 2-column table but am getting errors. The code that I
> am using is similar to:
>
> <table>
> <xsl:for-each match="parent">
> <xsl:if test="position() mod 2 = 1">
> <tr>
> </xsl:if>
> <td>
> <xsl:apply-templates match="child"/>
> </td>
> <xsl:if test="position() mod 2 = 0")
> </tr>
> </xsl:if>
> </xsl:for-each>
> </table>
>
> The error refers to the <tr> line and states that there is no corresponding
> </tr>. I see why this is an error, but I do not know how to correct the
> problem. Any help will be greatly appreciated.
The problem here is that you can't really process the elements one at a
time and keep your xml well formed. In a more traditional programming
environment I'd process the parent elements 2 at a time by getting the
for loop to increment 2 each time.
for i = 1 to numParents step 2
<tr>
process parent[i]
process parent[i+1]
</tr>
next i
But, xsl for loops can't do that. Well, actually, they can. You just
need to change the way you think about it. Instead of stepping through
every second element, you need to reduce the node set you are processing
to every second element.
<xsl:for-each match="parent[position() mod 2 = 1]">
Now you can drop the xsl:if and process the two columns by calling
apply-templates twice. Once with the child and once with the next child.
For example:
<table>
<xsl:for-each match="parentposition() mod 2 = 1]">
<tr>
<td>
<xsl:apply-templates match="child"/>
</td>
<td>
<xsl:apply-templates match="following-sibling::child[position()=1]"/>
</td>
</tr>
</xsl:for-each>
</table>
For a good example of a more general purpose way of doing this for any
number of columns, check out this thread:
http://groups.google.com/group/microsoft.public.xsl/browse_frm/thread/3ab6ad3504389348/94064a2822bc214f
Ivan
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
