Re: [xsl] Create Table

Subject: Re: [xsl] Create Table
From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
Date: Mon, 22 Mar 2010 10:28:30 +0100
The table body can also be generated using an XPath expression which avoids
the spurious non-breaking space and lets you specify attribute field's values
in a sequence:

<tbody>
   <!-- ... -->
   <xsl:for-each
select="BOM/interface-categories/category[@category='Source']">
      <tr>
        <xsl:for-each select="for $x in ('file','version','date','user',
                                         'cr_number','comment') return
             if (data[@section='changes' and @field=$x]) then
                 data[@section='changes' and @field=$x] else '&#160;'">
           <td><xsl:value-of select="."/></td>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
</tbody>

-W

On Sat, Mar 20, 2010 at 10:06 PM, Eric J. Bowman <eric@xxxxxxxxxxxxxxxx>
wrote:
> bernie bonn wrote:
>>
>> There are a series of data nodes that represent either headings or
>> diffs or detail to the headings. The headings are denoted by the
>> attribute section=changes.  Basically,  every time I encounter this
>> section I want to make a new table row and fill the cells with the
>> different value of the element, or I could even create a whole new
>> table.
>>
>
> I've given a solution which includes <tbody> elements, you can
> extrapolate that into different tables if needed.
>
>>
>> The problem I am having is testing if a fild is missing, some
>> entries don't have a field="file for example.  In that case I have to
>> leave a blank cell.
>>
>
> Using a recursive push solution is best for building the structure of
> the tables, while filling the cells in your case calls for a pull
> solution.  I mocked up a source file that isn't an exact fit, but does
> illustrate your issue, that's listed first, followed by the XSLT.
>
> My thinking, obviously, is that an extra nbsp doesn't hurt anything.
>
> HTH,
> Eric
>
> <?xml version='1.0' encoding='utf-8'?>
> <server-manifest>
>    <category-source>
>        <interface-categories name='Source Changes' category='Source'>
>            <data section='diff'>if (reportService != null)</data>
>            <data section='diff'>return true;</data>
>            <data section='diff'>return false;</data>
>            <data section='changes'
field='file'>ConvertHistoryWages.java</data>
>            <data section='changes'
field='version'>\main\spr2010_apr_dev\2</data>
>            <data section='changes' field='date'>20100310.102844</data>
>            <data section='changes' field='user'>jryan</data>
>            <data section='changes' field='cr_number'>602018</data>
>            <data section='changes' field='comment'>fix for log 5960</data>
>            <data section='diff'>1296a1297,1298</data>
>            <data section='diff'></data>
>        </interface-categories>
>        <interface-categories name='Source Changes' category='Source'>
>            <data section='diff'>if (reportService != null)</data>
>            <data section='diff'>return true;</data>
>            <data section='diff'>return false;</data>
>            <data section='changes'
field='file'>ConvertHistoryWages.java</data>
>            <data section='changes'
field='version'>\main\spr2010_apr_dev\2</data>
>            <data section='changes' field='date'>20100310.102844</data>
>            <data section='changes' field='cr_number'>602018</data>
>            <data section='changes' field='comment'>fix for log 5960</data>
>            <data section='diff'>1296a1297,1298</data>
>            <data section='diff'></data>
>        </interface-categories>
>    </category-source>
>    <category-source>
>        <interface-categories name='Source Changes' category='Source'>
>            <data section='diff'>if (reportService != null)</data>
>            <data section='diff'>return true;</data>
>            <data section='diff'>return false;</data>
>            <data section='changes'
field='file'>ConvertHistoryWages.java</data>
>            <data section='changes' field='date'>20100310.102844</data>
>            <data section='changes' field='user'>jryan</data>
>            <data section='changes' field='cr_number'>602018</data>
>            <data section='diff'>1296a1297,1298</data>
>            <data section='diff'></data>
>        </interface-categories>
>    </category-source>
> </server-manifest>
>
> <?xml version='1.0' encoding='utf-8'?>
> <xsl:stylesheet version='1.0'
>    xmlns='http://www.w3.org/1999/xhtml'
>    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
>    <xsl:output omit-xml-declaration='no' method='xml' indent='yes'
xml:space='default' encoding='utf-8'/>
>    <xsl:template match='/'>
>        <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
>            <head>
>                <title>table example</title>
>                <style
type='text/css'>table{table-layout:fixed;width:auto}</style>
>            </head>
>            <body>
>                <table width='0' summary='summary goes here for
accessibility'>
>                    <caption>caption goes here for accessibility</caption>
>                    <thead>
>                        <tr>
>                            <th scope='col'>File</th>
>                            <th scope='col'>Version</th>
>                            <th scope='col'>Date</th>
>                            <th scope='col'>User</th>
>                            <th scope='col'>CR Number</th>
>                            <th scope='col'>Comment</th>
>                        </tr>
>                    </thead>
>                    <tfoot><!-- optional -->
>                        <tr>
>                            <th>File</th>
>                            <th>Version</th>
>                            <th>Date</th>
>                            <th>User</th>
>                            <th>CR Number</th>
>                            <th>Comment</th>
>                        </tr>
>                    </tfoot>
>                    <xsl:apply-templates select='//category-source'/>
>                </table>
>            </body>
>        </html>
>    </xsl:template>
>    <xsl:template match='category-source'>
>        <tbody><xsl:apply-templates
select='./interface-categories'/></tbody>
>    </xsl:template>
>    <xsl:template match='interface-categories'>
>        <tr>
>            <td scope='row'><xsl:value-of
select="./data[@field='file']"/>&#160;</td>
>            <td><xsl:value-of select="./data[@field='version']"/>&#160;</td>
>            <td><xsl:value-of select="./data[@field='date']"/>&#160;</td>
>            <td><xsl:value-of select="./data[@field='user']"/>&#160;</td>
>            <td><xsl:value-of
select="./data[@field='cr_number']"/>&#160;</td>
>            <td><xsl:value-of select="./data[@field='comment']"/>&#160;</td>
>        </tr>
>    </xsl:template>
> </xsl:stylesheet>

Current Thread