Re: [xsl] Create Table

Subject: Re: [xsl] Create Table
From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
Date: Tue, 23 Mar 2010 09:53:27 +0100
This uses the grouping feature of XSLT 2.0. Notice that I assume that each
block of <data section="changes" field="..."> for one logical change is
separated from the next one by at least one <data section="diff"...> or any
other attribute @section value other than "changes". - If this does not hold,
it'll become a little difficult since any field can be missing, which would
not
permit to separate the groups cleanly.

  <tbody>
    <tr>  <!--****Static Table Headings***-->
      <th>File</th>
      <th>Version</th>
      <th>Date</th>
      <th>User</th>
      <th>CR Number</th>
      <th>Comment</th>
    </tr>
    <xsl:for-each-group  select="BOM/interface-categories/data"
group-adjacent="@section">
      <xsl:if test="current-grouping-key() = 'changes'">
        <tr>
        <xsl:for-each select="for $x in
('file','version','date','user','cr_number','comment') return
             ((current-group()[@field=$x],'&#160;')[1])">
           <td><xsl:value-of select="."/></td>
        </xsl:for-each>
      </tr>
      </xsl:if>
    </xsl:for-each-group>
  </tbody>


On Mon, Mar 22, 2010 at 8:57 PM, bernie bonn <moochambo@xxxxxxxxx> wrote:
>
> HI Eric & List,
>
> Thanks for this solution.  There is another gotcha.  I realized I didn't
make clearthat, all the changes are inside of one parent element 
<interface-categories name='Source Changes' category='Source'>.
> So I need to switch on <data> children inside of that.  How I know it is the
start of a new change and table row, is the data node will have the attribute
'changes'  <data section='changes'
field='file'>ConvertHistoryWages.java</data>.  There can be mutliple 'changes'
inside the parent element <interface-changes>.
> I apologize, for not being clear.  Any additional guidance is sure
appreciated.
>
> Thanks.,
> Bernie
>
>
>
>
> ----- Original Message ----
> From: Eric J. Bowman <eric@xxxxxxxxxxxxxxxx>
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Cc: bernie bonn <moochambo@xxxxxxxxx>
> Sent: Sat, March 20, 2010 5:06:36 PM
> Subject: Re: [xsl] Create Table
>
> 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']"/> </td>
>             <td><xsl:value-of select="./data[@field='version']"/> </td>
>             <td><xsl:value-of select="./data[@field='date']"/> </td>
>             <td><xsl:value-of select="./data[@field='user']"/> </td>
>             <td><xsl:value-of select="./data[@field='cr_number']"/> </td>
>             <td><xsl:value-of select="./data[@field='comment']"/> </td>
>         </tr>
>     </xsl:template>
> </xsl:stylesheet>

Current Thread