Re: [xsl] Problem with Muenchian grouping

Subject: Re: [xsl] Problem with Muenchian grouping
From: jean pujol <jean.pujol@xxxxxxxxxxxxxxx>
Date: Mon, 10 Nov 2003 13:09:35 +0100
Thank you all for your replies.
However I have another problem:

Here is my XSL file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
 <xsl:output method="html"/>
 <xsl:key name="records-by-name" match="//*[action]" use="name()"/>
 <xsl:template match="/">
  <html><head><title>Viewer</title></head><body bgColor="white">
  <xsl:for-each select="//*[generate-id(.) =
generate-id(key('records-by-name',name())[1])]" >
   <xsl:sort select="name()" />
   <h3><b><xsl:value-of select="name()"/> table</b></h3>
   <table border="1" cellpadding="5" borderColor="black">
   <thead>
   <th align="center" bgColor="lightgrey">Selection</th>
   <xsl:for-each select="./*">
    <th align="center" bgColor="lightgrey"><xsl:value-of select="name()"/>
</th>
   </xsl:for-each>
   </thead>
   <xsl:for-each select="key('records-by-name',name())">
    <tr>
    <td align="center"><input type="checkbox" checked="true"/></td>
    <xsl:for-each select="./*">
     <xsl:choose>
      <xsl:when test="string-length(text()) &lt; 1"><td><i>empty
record</i></td></xsl:when>
      <xsl:when test="text()='update'"><td align="center"
bgColor="dodgerblue"><xsl:value-of select="text()"/></td></xsl:when>
      <xsl:when test="text()='insert'"><td align="center"
bgColor="limegreen"><xsl:value-of select="text()"/></td></xsl:when>
      <xsl:when test="text()='delete'"><td align="center"
bgColor="mediumorchid"><xsl:value-of select="text()"/></td></xsl:when>
      <xsl:otherwise><td align="center"><xsl:value-of
select="text()"/></td></xsl:otherwise>
     </xsl:choose>
    </xsl:for-each>
    </tr>
   </xsl:for-each>
   </table><br/><br/>
  </xsl:for-each>
  </body></html>
 </xsl:template>
</xsl:stylesheet>

I group elements that have an <action> child by name and then for each group I
build an HTML table. Members of the same group usually have the same set of
childnodes, but this is not always the case, and this is a problem, given the
way I build the tables: I get the first member of the group and set the
headers of the table to the names of its children (<th align="center"
bgColor="lightgrey"><xsl:value-of select="name()"/> </th>). When other members
of the group have more children, the values of those latter are put in a new
column without header...

So, instead of getting the first member of the group, I would like to get the
one with the greatest number of children (to get all the headers, it's not a
problem if some fields are blank in the table). Is this possible?

Wendell Piez a écrit :

> Jean,
>
> At 12:29 PM 11/7/2003, you wrote:
>
> >I'm interested in the children of <data> that have an <action> child: I
> >would like to group them by name and perform some processing on them.
> >The problem is I cannot know those names (that is, each element is
> >optional), and the number of elements of each category is undefined.
> >So I don't know how to define the <xsl:key> element that I need.
> >I have tried <xsl:key name="records-by-name" match="/*/*[action]"
> >use="name()"> but that doesn't seem to work.
>
> I'd suggest
>
> <xsl:key name="records-by-name" match="*[action]" use="local-name()"/>
>
> This is different from yours in that:
> - it matches any element with an action child, not just those directly
> below the top level
> - it uses the local-name() function, as a precaution against namespace
> confusion. (Though name() should work and you don't have to change this.)
>
> The first fix is what will help in your case, since the elements you want
> (judging from your sample data) are three levels below the root, not two
> (for example, /dump/data/project), and hence won't match "/*/*[action]".
>
> >Nor can I identify the elements appearing first in their "group":
> >my_element_name[generate-id(.) = generate-id(key('records-by-name',
> >name())[1])] because precisely I don't know what to put instead of
> >"my_element_name".
>
> Just *[generate-id(.) = generate-id(key('records-by-name', name())[1])] --
> this matches any element on the child axis.
>
> >  I think there is a problem in the way I want to reach
> >the name (the tag name) of those /*/*[action] nodes. But when I replace
> >'name()' by 'self::name()' it raises an error (isn't 'self' a correct
> >axis?).
>
> self:: is an axis, but name() is a function, not a node test, so
> self::name() is illegal.
>
> I hope this is enough to get you back on track--
>
> Cheers,
> Wendell
>
> >In fact I have mostly tried to adapt the example given by Dr Jenni
> >Tennison in the "Cocoon, sql and xslt" thread, but in my case, the
> >grouping is done depending on the names of the elements I want to group,
> >not on their children's values.
> >
> >I hope my question is clear.
> >
> >Thanks in advance,
> >
> >Jean
> >
> >
> >  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
> ======================================================================
> Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
> Mulberry Technologies, Inc.                http://www.mulberrytech.com
> 17 West Jefferson Street                    Direct Phone: 301/315-9635
> Suite 207                                          Phone: 301/315-9631
> Rockville, MD  20850                                 Fax: 301/315-8285
> ----------------------------------------------------------------------
>    Mulberry Technologies: A Consultancy Specializing in SGML and XML
> ======================================================================
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread