Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 18th, 2007, 02:10 PM
Registered User
 
Join Date: Jun 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Algorithmic problem transformin table with colspan

Hi. I need to generate a html table from this xml structure(very simplified for the example):

<Row>
  <Cell>
    <Text>Hello</Text>
    <colspan>2</colspan>
  </Cell>
  <Cell>
    <Text>World</Text>
  </Cell>
</Row>
more Row(s) here
.
.
.

this should be transformed into something like:
<table>
<tr>
<td colspan="2">Hello</td>
</tr>
more TR(s) here
.
.
.
notice that the 'World' cell should not appear because of the colspan.
The first level element Row represent each row (tr), and the second level element Cell represent each cell (td). So, THE PROBLEM IS: when I have a colspan=2 in one cell I don't have to convert the next Cell element into another td; and if I have a colspan=n I have to avoid converting the next n-1 Cell elements to td tags.
I can't change the xml structure to avoid sending Cell elements for cells that are not going to be visible. Nowadays I'm parsing this with two nested for-each, but the problem is that I'm always inserting more td than the required when one of the Cell come with colspan set. I can't reassign variables, so I can't keep a count of how many Cell elements should I avoid next.
I'm looking for an idea, a hint, anything that helps me to transform this XML in this way (I know it would be much easier if the cell that should not appear doesn't have an associated Cell element, but I can't change this!!!)
I would really appreciate any help.
Thanks,

Federico
 
Old June 18th, 2007, 02:29 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Looks to me as if you need something like this:

<xsl:template match="Row">
 <tr>
  <xsl:apply-templates select="Cell"/>
 </tr>
</xsl:template>

<xsl:template match="Cell">
  <xsl:variable name="latestColspan" select="preceding-sibling::Cell[@colspan][1]"/>
  <xsl:if test="not($latestColspan and count($latestColspan/preceding-sibling::Cell + $latestColspan/@colspan) &gt; count(current()/preceding-sibling::Cell))">
  <td>
    <xsl:copy-of select="@colspan"/>
    <xsl:apply-templates/>
  </td>
</xsl:if>
</xsl:template>

As always, the answer is to stop thinking algorithmically, and try to think of the output as a function of the input: "how can I decide by looking at the input whether the current cell should be output or not?"



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling the gaps of rowspan/colspan sources maikm XSLT 3 August 27th, 2008 03:36 AM
problems with escaping characters when transformin lookingForAnswer XSLT 0 February 13th, 2007 06:29 PM
Table Problem Ben Horne Dreamweaver (all versions) 1 October 25th, 2004 03:17 AM
Lookup Table Problem mikericc Access VBA 2 January 19th, 2004 07:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.