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 February 19th, 2007, 02:59 PM
Registered User
 
Join Date: Mar 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transform html table to add ids/headers using xsl

Hi,
I have the following html table:

<table width="75%" border="1">
<tr>
<th rowspan="2">header1</th>
<th colspan="2">header2</th>
<th rowspan="2">header3</th>
<th rowspan="2">header4</th>
</tr>
<tr>
<th>header5</th>
<th>header6</th>
</tr>
<tr>
<th>row_header1</th>
<td>data1</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
</tr>
<tr>
<th>row_header2</th>
<td>data5</td>
<td>data6</td>
<td>data7</td>
<td>data8</td>
</tr>
<tr>
<th>row_header3</th>
<td>data9</td>
<td>data10</td>
<td>data11</td>
<td>data12</td>
</tr>
<tr>
<th>row_header4</th>
<td>data13</td>
<td>data14</td>
<td>data15</td>
<td>data16</td>
</tr>
</table>

to which I need to add the id attribute (I was able to do this with the position() function), and the headers attribute programmatically. The headers attribute has pointer values to the columns and rows headers of a cell:

<table width="75%" border="1">
<tr>
<th rowspan="2" id="un_0">header1</th>
<th colspan="2" id="un_1">header2</th>
<th rowspan="2" id="un_2">header3</th>
<th rowspan="2" id="un_3">header4</th>
</tr>
<tr>
<th id="un_4" headers="un_1">header5</th>
<th id="un_5" headers="un_1">header6</th>
</tr>
<tr>
<th id="un_6" headers="un_0">row_header1</th>
<td headers="un_1 un_4 un_6">data1</td>
<td headers="un_1 un_5 un_6">data2</td>
<td headers="un_2 un_6">data3</td>
<td headers="un_3 un_6">data4</td>
</tr>
<tr>
<th id="un_7" headers="un_0">row_header2</th>
<td headers="un_1 un_4 un_7">data5</td>
<td headers="un_1 un_5 un_7">data6</td>
<td headers="un_2 un_7">data7</td>
<td headers="un_3 un_7">data8</td>
</tr>
<tr>
<th id="un_8" headers="un_0">row_header3</th>
<td headers="un_1 un_4 un_8">data9</td>
<td headers="un_1 un_5 un_8">data10</td>
<td headers="un_2 un_8">data11</td>
<td headers="un_3 un_8">data12</td>
</tr>
<tr>
<th id="un_9" headers="un_0">row_header4</th>
<td headers="un_1 un_4 un_9">data13</td>
<td headers="un_1 un_5 un_9">data14</td>
<td headers="un_2 un_9">data15</td>
<td headers="un_3 un_9">data16</td>
</tr>
</table>

Do you have a suggestion on how I can write a stylesheet to do perform this. I have to use XSLT 1.0.
Thanks,
Francine.

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

I'm sorry, but I can't see the logic of why

<th id="un_4" headers="un_1">header5</th>
<th id="un_5" headers="un_1">header6</th>

point to un_1 rather than un_0. I'm afraid specification by example doesn't always work - the logic may be obvious to you, but it's not necessarily obvious to the reader. It would help to submit a much smaller example and explain the functional relationship more clearly.

Having said that, this kind of problem is hard in XSLT 1.0 (and it's no pleasure to solve it for someone who uses XSLT 2.0 every day). It's usually best to tackle it as a multi-pass transformation, with the first pass perhaps adding absolute row/column numbers of the top-left and bottom-right corners of each cell.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 10:12 AM
Registered User
 
Join Date: Mar 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,
There unfortunately is no standard logic for the table as the layout depends on the underlying data. In one case, header2 could be "Census Years" and header5 would be 2001 and header6 2006.
The reason why un_1 in
<th id="un_4" headers="un_1">header5</th>
<th id="un_5" headers="un_1">header6</th>
is because, the header cells span two rows except for header2 which spans two columns and is a pointer for header5 and header6. It would have been great to generate the pointers and ids dynamically using XSLT 1.0 (which the version handled by the built-in processor) but this might very complicated. So as you mention, this might have to be done using more than one transformation; I will use XSLT 2.0 to produce them statically.
Thanks for your suggestion,
Francine.






Similar Threads
Thread Thread Starter Forum Replies Last Post
transform a xml to a html table robert_trudel_fr XSLT 3 December 3rd, 2006 02:16 PM
XSL table to HTML ayamas XSLT 10 September 11th, 2006 06:45 AM
Transform an XSL sheet khanman225 XSLT 8 July 6th, 2006 10:21 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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