xsl-list
[Top] [All Lists]

Re: [xsl] Re: Structuring multiple HTML tables based on the value of a child node

2007-06-05 10:07:14
I would also suggest reading the "grouping" pages at Jeni's site (I
too spent lot of time on her site, to learn the techniques).

But just for fun, I attempted to solve this problem as follows.

XSLT 1.0 solution (uses Muenchian grouping technique):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

 <xsl:output method="html" />

 <xsl:key name="by-color" match="sign" use="color" />

 <xsl:template match="signs">
   <html>
     <head>
       <title/>
     </head>
     <body>
       <xsl:for-each select="sign[generate-id() =
generate-id(key('by-color', color)[1])]">
         <xsl:sort select="color"/>
         <table frame="all" colsep="1" rowsep="1" id="{color}">
           <tr>
             <th>Name</th>
             <th>Shape</th>
           </tr>
           <xsl:for-each select="key('by-color', color)">
        <xsl:sort select="name"/>
        <tr>
          <td>
            <xsl:value-of select="name"/>
          </td>
          <td>
            <xsl:value-of select="shape"/>
          </td>
        </tr>
            </xsl:for-each>
         </table>
       </xsl:for-each>
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet>

XSLT 2.0 solution:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">

 <xsl:output method="html" />

 <xsl:template match="signs">
   <html>
     <head>
       <title/>
     </head>
     <body>
       <xsl:for-each-group select="sign" group-by="color">
         <xsl:sort select="current-grouping-key()"/>
         <table frame="all" colsep="1" rowsep="1"
id="{current-grouping-key()}">
           <tr>
             <th>Name</th>
             <th>Shape</th>
           </tr>
           <xsl:for-each select="current-group()">
        <xsl:sort select="name"/>
        <tr>
           <td>
              <xsl:value-of select="name"/>
           </td>
           <td>
              <xsl:value-of select="shape"/>
           </td>
         </tr>
            </xsl:for-each>
         </table>
       </xsl:for-each-group>
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet>


On 6/5/07, Mark Peters <flickrmeister(_at_)gmail(_dot_)com> wrote:
Hi Everyone,

Sorry for the long subject line. I've been reading Dave Pawson's site
for a starting point on a particular project, but I'm not even sure
how to distill it into browse/search terms. None of the general
headings I've explored seem to quite describe it.

Here's what I'm trying to do. I'd like to structure my data as
multiple HTML tables based on the value of a child node.

For example, in the sample input file below, I'd like to generate
three HTML tables, one for each <color> value. The tables would
include two columns: Name and Shape. The table rows display the name
and shape data for each color. The <table> element would include an id
attribute that contains the <color> value.


Sample input file:

<signs>
  <sign>
     <name>stop</name>
     <shape>hexagon</shape>
     <color>red</color>
  </sign>
  <sign>
      <name>yield</name>
     <shape>triangle</shape>
      <color>yellow</color>
   </sign>
  <sign>
      <name>steep incline</name>
     <shape>diamond</shape>
      <color>yellow</color>
   </sign>
  <sign>
      <name>slippery when wet</name>
     <shape>diamond</shape>
      <color>yellow</color>
   </sign>
  <sign>
      <name>city name</name>
     <shape>rectangle</shape>
      <color>green</color>
   </sign>
</signs>


If I use <for-each>, I end up with a table for every <sign> element.
How could someone create a single table for each <color> value?

Here's what I have so far:

   <xsl:template match="\">
     <xsl:for-each select="signs/sign/color">
     <xsl:sort select="."/>
       <table frame="all" colsep="1" rowsep="1">
           <xsl:attribute name="id"><xsl:value-of select="."/></xsl:attribute>
            <tr>
                <th>Name</entry>
                <th>Shape</entry>
            </tr>
            <xsl:for-each select="../name">
                 <xsl:sort select="."/>
                     <tr>
                         <td>
                            <xsl:value-of select="."/>
                         </td>
                         <td>
                            <xsl:value-of select="shape"/>
                         </td>
                     </tr>
                 </xsl:for-each>
           </table>
       </xsl:for-each>
   </xsl:template>


Thanks for any suggestions. I appreciate all of the help the kind
people on this list have provided over the past year. I've learned a
lot from reading the posts and exploring on my own.

Regards,
Mark


--
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>