Re: [xsl] Can I group 3 levels somehow

Subject: Re: [xsl] Can I group 3 levels somehow
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 22 Mar 2002 20:42:51 -0500
At 2002-03-21 20:17 +0000, you wrote:
I am trying to group xml elements using three attributes.

i.e.

- Firstly, I want to group them by the imagetype

---- Then within these imagetype groups, group by ordinal

---------- then within imagetype and ordinal I want to groupby imageparentid

Is this possible

I find grouping at multiple levels very straightforward with variables. I hope the example below helps ... unfortunately your test data didn't have any duplication at the third level of depth of your requirement, so I'm not 100% this is going to give you what you need ... but it does give you the three levels of grouping that you ask for above.


The idea is to work with only the number of nodes reduced from the previous level of grouping.

....................... Ken

T:\ftemp>type gary.xml
<?xml version="1.0" encoding="utf-8"?>
<article>
<images>
<image ordinal="1" imgsrc="/upload_images/sony_printer_dscs85-146.jpg" imagex="146" imagey="129" alt="Some techy equipment" imagesize="articlethird" caption="Picture 4 caption here" bytesize="5947" imageparentid="" imagetype="article" imageid="13224" />
<image ordinal="2" imgsrc="/upload_images/psp7-146.jpg" imagex="146" imagey="110" alt="screen grabber" imagesize="articlethird" caption="This is a silly picture number 2" bytesize="7276" imageparentid="" imagetype="article" imageid="13222" />
<image ordinal="1" imgsrc="/upload_images/Sample-1462.jpg" imagex="146" imagey="109" alt="Someone about to sprint" imagesize="articlethird" caption="sport picture image 3" bytesize="3911" imageparentid="" imagetype="gallery" imageid="13223" />
<image ordinal="1" imgsrc="/upload_images/Sample-146.jpg" imagex="146" imagey="109" alt="alt tag image 1" imagesize="articlethird" caption="caption for picture 1" bytesize="3911" imageparentid="13223" imagetype="gallery" imageid="13225" />
<image ordinal="2" imgsrc="/upload_images/psp7-1462.jpg" imagex="146" imagey="110" alt="alt6" imagesize="articlethird" caption="capt6" bytesize="7276" imageparentid="" imagetype="gallery" imageid="13260" />
<image ordinal="2" imgsrc="/upload_images/Sample-1461.jpg" imagex="146" imagey="109" alt="alt7" imagesize="articlethird" caption="caption7" bytesize="3911" imageparentid="13260" imagetype="gallery" imageid="13261" />
<image ordinal="3" imgsrc="/upload_images/psp7-146.jpg" imagex="146" imagey="110" alt="alt3" imagesize="articlethird" caption="caption3" bytesize="7276" imageparentid="13223" imagetype="gallery" imageid="13257" />
<image ordinal="4" imgsrc="/upload_images/psp7-1461.jpg" imagex="146" imagey="110" alt="alt4" imagesize="articlethird" caption="caption4" bytesize="7276" imageparentid="" imagetype="gallery" imageid="13258" />
<image ordinal="5" imgsrc="/upload_images/sony_printer_dscs85-146.jpg" imagex="146" imagey="129" alt="alt5" imagesize="articlethird" caption="caption5" bytesize="5947" imageparentid="" imagetype="gallery" imageid="13259" />
<image ordinal="1" imgsrc="/upload_images/dummy_thumbnail.jpg" imagex="60" imagey="60" alt="I'm the alt tag" imagesize="smallthumbnail" caption="" bytesize="" imageparentid="" imagetype="index" imageid="13209" />
</images>
</article>


T:\ftemp>type gary.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:import href="file:///x:/samp/copyall.xsl"/>

<xsl:output/>

<xsl:template match="/">
  <xsl:variable name="all-images" select="/article/images/image"/>
  <xsl:for-each select="$all-images">
    <xsl:sort select="@imagetype"/>
    <xsl:if test="generate-id(.) =
                  generate-id($all-images[@imagetype=current()/@imagetype])">
      <xsl:variable name="imagetypes"
                   select="$all-images[@imagetype=current()/@imagetype]"/>
      <h2>Image Type - <xsl:value-of select="@imagetype"/></h2>
      <xsl:for-each select="$imagetypes">
        <xsl:sort select="@ordinal"/>
        <xsl:if test="generate-id(.) =
                      generate-id($imagetypes[@ordinal=current()/@ordinal])">
          <xsl:variable name="ordinals"
                        select="$imagetypes[@ordinal=current()/@ordinal]"/>
          <h3>Ordinal - <xsl:value-of select="@ordinal"/></h3>
          <xsl:for-each select="$ordinals">
            <xsl:sort select="@imageparentid"/>
            <xsl:if test="generate-id(.) =
                          generate-id($ordinals[@imageparentid =
                                      current()/@imageparentid])">
              <xsl:variable name="imageparentids"
                  select="$ordinals[@imageparentid=current()/@imageparentid]"/>
              <h4>Image parent id - <xsl:value-of select="@imageparentid"/>
              </h4>
              <table border="1">
                <xsl:for-each select="$imageparentids">
                  <tr>
                    <td><xsl:value-of select="@ordinal" /></td>
                    <td><xsl:value-of select="@imgsrc" /></td>
                    <td><xsl:value-of select="@imagex" /></td>
                    <td><xsl:value-of select="@imagey" /></td>
                    <td><xsl:value-of select="@alt" /></td>
                    <td><xsl:value-of select="@imagesize" /></td>
                    <td><xsl:value-of select="@bytesize" /></td>
                    <td><xsl:value-of select="@imageparentid" /></td>
                    <td><xsl:value-of select="@imagetype" /></td>
                    <td><xsl:value-of select="@imageid" /></td>
                  </tr>
                </xsl:for-each>
              </table>
            </xsl:if>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt gary.xml gary.xsl gary.htm


-- Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO: June 17-21, 2002 - : 3-days XML Information Modeling: July 31-August 2, 2002

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                        Definitive XSLT & XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:   2002-04-08,09,10,11,05-06,07,09,10,13,20,
-                                06-04,07,10,11,13,14,17,20,07-31


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



Current Thread