Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 9th, 2009, 08:22 AM
Friend of Wrox
Points: 533, Level: 8
Points: 533, Level: 8 Points: 533, Level: 8 Points: 533, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default XML Table to Html

I have got this table in xml format below, where the tcols trows and ccols crows attributes all specify number of table columns, number of table rows, table cell colspans and table cell rowspans. Now I had the XSL to convert this xml table to a standard html table working until I came across the table cell rowspan (crows) greater than one. Attached is my XSL as well, any suggestions on this, as I know that my code doesn't accomodate rowspans??

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <TABELLA data_mod="2008-10-30 12:20:59" id="2899" locale="it_IT" colonne="3" righe="2"
        colonna_1="33%" colonna_2="33%" colonna_3="33%">
        <grown>
            <TAB_2899>
                <story>
                    <Table xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table"
                        aid:trows="3" aid:tcols="2" summary="NOCHANGE">
                        <Cell aid:table="cell" aid:crows="2" aid:ccols="1" aid:ccolwidth="184"
                            larghezza="33%">
                            row 1 col 1
                        </Cell>
                        <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="184"
                            larghezza="33%">
                            row 1 col 2
                        </Cell>
                        <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="184"
                            larghezza="33%">row 2 col 2</Cell>
                        <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="184"
                            larghezza="33%">row 2 col 2</Cell>
                        <Cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="184"
                            larghezza="33%">row 2 col 2</Cell>
                    </Table>
                </story>
            </TAB_2899>
        </grown>
    </TABELLA>
</Root>
XSL code

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" 
    version="2.0">
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head><title></title></head>
            <body>
                <xsl:for-each select="Root/TABELLA">
                    <xsl:variable name="numberofrows" select="grown/*/story/Table/attribute::aid:trows"></xsl:variable>
                    <xsl:variable name="numberofcols" select="grown/*/story/Table/attribute::aid:tcols"></xsl:variable>
                    <table>
                        <xsl:call-template name="output">
                            <xsl:with-param name="numberofrows" select="$numberofrows"></xsl:with-param>
                            <xsl:with-param name="numberofcols" select="$numberofcols"></xsl:with-param>
                            <xsl:with-param name="Table" select="grown/*/story/Table"></xsl:with-param>
                            <xsl:with-param name="counter" select="1"></xsl:with-param>
                        </xsl:call-template>
                    </table>
                </xsl:for-each>    
            </body>
        </html>
    </xsl:template>
    <xsl:template name="output">
        <xsl:param name="numberofrows"></xsl:param>
        <xsl:param name="numberofcols"></xsl:param>
        <xsl:param name="Table"></xsl:param>
        <xsl:param name="counter"></xsl:param>
        <xsl:choose>
            <xsl:when test="$counter &lt;= $numberofrows">
                <tr>
                    <xsl:choose>
                        <xsl:when test="$counter = 1">
                            <!--this is the header row in each table-->
                            <xsl:attribute name="bgcolor">blue</xsl:attribute>
                        </xsl:when>
                        <xsl:when test="$counter > 1">
                            <!--this are the other rows in each table-->
                            <xsl:attribute name="bgcolor">red</xsl:attribute>
                        </xsl:when>
                    </xsl:choose>
                    
                    
                    <xsl:for-each select="$Table/Cell[   
                        sum(preceding-sibling::Cell/attribute::aid:ccols) &lt; ($numberofcols * $counter)   and  
                        sum(preceding-sibling::Cell/attribute::aid:ccols) &gt;= ($numberofcols * ($counter - 1)) ]">            
                        <td>
                            <xsl:if test="@aid:ccols &gt; 1">
                                <xsl:attribute name="colspan" select="@aid:ccols"></xsl:attribute>
                            </xsl:if>    
                            <xsl:if test="@aid:crows &gt; 1">
                                <xsl:attribute name="rowspan" select="@aid:crows"></xsl:attribute>
                            </xsl:if>    
                            <xsl:apply-templates></xsl:apply-templates>
                        </td>
                    </xsl:for-each>
                </tr>
                <xsl:call-template name="output">
                    <xsl:with-param name="numberofrows" select="$numberofrows"></xsl:with-param>
                    <xsl:with-param name="numberofcols" select="$numberofcols"></xsl:with-param>
                    <xsl:with-param name="Table" select="$Table"></xsl:with-param>
                    <xsl:with-param name="counter" select="$counter + 1"></xsl:with-param>
                </xsl:call-template>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="_B">
        <xsl:apply-templates></xsl:apply-templates>
    </xsl:template>
    <xsl:template match="_centro">
        <xsl:apply-templates></xsl:apply-templates>
    </xsl:template>
    <xsl:template match="img_html">
        <img>
            <xsl:attribute name="src" select="@href"></xsl:attribute>
        </img>
    </xsl:template>
</xsl:stylesheet>
I have got the number of rows right its just the rowspan. Please enlighten me!
Reply With Quote
  #2 (permalink)  
Old March 9th, 2009, 08:45 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This kind of problem certainly isn't easy. I think a general solution involves building an expanded version of the table in which there's an entry for each (1x1) unit cell, which means expanding a rowspan or colspan to a number of unit cells; perhaps in this expanded version each unit cell can contain a reference to the top-left corner of the larger original cell in which it is contained. But the details depend on exactly what you want to do.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old March 9th, 2009, 08:54 AM
Friend of Wrox
Points: 533, Level: 8
Points: 533, Level: 8 Points: 533, Level: 8 Points: 533, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

I knew it was a hard problem, thankful that you said that.

Actually its not my problem, I have been helping someone else on another forum http://forums.tizag.com/showthread.php?t=12139
and they give me a sample table and each time I solve the problem and the XSL code gets a bit harder, then they give me a more complex table.

At the moment I am one step ahead of them thinking about the cell rowspan as I can see that they will come back to me with a table with this in it.

Anyway if I find the solution which I doubt, i will post it.

Thanks.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AID XML to html table JohnBampton XSLT 1 March 8th, 2009 09:15 AM
transform a xml to a html table robert_trudel_fr XSLT 3 December 3rd, 2006 02:16 PM
constructing a HTML table from xml data using xslt rameshnarayan XSLT 0 September 19th, 2005 06:53 AM
XML to HTML Table with Sorting and Distinct VictorMk XSLT 4 April 23rd, 2004 06:29 PM
XML List -> HTML table JPMRaptor XSLT 1 November 14th, 2003 10:19 PM



All times are GMT -4. The time now is 04:44 AM.


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