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 March 9th, 2009, 08:22 AM
Friend of Wrox
 
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!
 
Old March 9th, 2009, 08:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old March 9th, 2009, 08:54 AM
Friend of Wrox
 
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.





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





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