Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How to build a tlabe?

From: Ivan Peters <ivan@--.----.--->
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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent