Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 31st, 2007, 08:46 AM
Registered User
 
Join Date: Oct 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default xslt grouping with attributes

Got the following XML

<?xml version='1.0' encoding='iso-8859-1' ?>
<?xml-stylesheet type='text/xsl' href='piv.xsl'?>
<all>
<h m='1' n='a'>51</h>
<h m='2' n='a'>6</h>
<h m='3' n='a'>7</h>
<h m='4' n='a'>8</h>
<h m='1' n='b'>52</h>
<h m='3' n='b'>5</h>
<h m='1' n='c'>53</h>
<h m='2' n='c'>5</h>
<h m='3' n='c'>5</h>
<h m='4' n='c'>5</h>
<h m='5' n='c'>5</h>
<h m='1' n='d'>54</h>
<h m='2' n='d'>5</h>
<h m='3' n='d'>5</h>
<h m='4' n='d'>5</h>
<h m='1' n='e'>55</h>
<h m='2' n='e'>5</h>
<h m='3' n='e'>5</h>
</all>

and wish to diplay as crosstab, using @m and @n

however in process I've got this xslt

<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="*"/>
  <xsl:output method="html" />


  <xsl:key name="tw-key" match="h" use="@n" />

  <xsl:key name="month-key" match="h" use="@m" />

  <xsl:variable name = "tw_names" select="//h[generate-id()=generate-id(key('tw-key',@n)[1])]/@n"/>

  <xsl:variable name = "month_names" select="//h[generate-id()=generate-id(key('month-key',@m)[1])]/@m"/>

              <xsl:template match="/">
              <html>
                      <title>Test</title>
                      <body>
                              <xsl:apply-templates mode="d0a"/>
                      </body>
                      </html>
              </xsl:template>


      <xsl:template match="all" mode = 'd0a'>
      template d0
              <table>
                    <xsl:for-each select ="$tw_names">
                    <xsl:sort select="$tw_names"/>
                    <xsl:for-each select ="$month_names">
                    <xsl:sort select="$month_names"/>
                      <tr><td align='right'>- month:</td>
                      <td> <xsl:value-of select="$month_names[//@m=current()]"/> </td>
                      <td></td>
                      <td><xsl:value-of select="@m"/></td>
                      <td><xsl:value-of select="format-number(sum(h),'#,##0')"/></td>
                      </tr>

                  </xsl:for-each>
                      <tr><td> name:</td>
                      <td>xx- <xsl:value-of select="$tw_names[//@n=current()]"/> -xx</td>
                      <td></td>
                      <td><xsl:value-of select="@m"/></td>
                      <td><xsl:value-of select="format-number(sum(h),'#,##0')"/></td>
                      </tr>

                  </xsl:for-each>
              </table>
      </xsl:template>

  </xsl:stylesheet>

and it instead of giving me

- month: 1 0
- month: 2 0
- month: 3 0
- month: 4 0
name: a 0
- month: 1 0
- month: 3 0
name: b 0
- month: 3 0
- month: 4 0
- month: 5 0
name: c 0
- month: 1 0
- month: 2 0
- month: 3 0
- month: 4 0
name: d 0
- month: 1 0
- month: 2 0
- month: 3 0
name: e 0


its just giving me
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
name: a 0
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
name: a 0
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
name: a 0
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
name: a 0
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
- month: 1 0
name: a 0

Help .. I've been stuck for yonks now!





 
Old October 31st, 2007, 09:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Couple of things spring out.

<xsl:for-each select="$month_names">

</xsl:for-each>

Inside the above for each loop the context switches to the attribute axis - you effectively ARE the @m attribute.

Therefore @m results in nothing, because the 'm' attribute doesn't have an attribute (i.e. h/@m/@m isn't valid).
Also the @m attribute doesn't have a h element under it if you want the h element its the parent of the current context, i.e. ".." or "../self::h" to be sure its an 'h' element and to illustrate what you are trying to sum.

Likewise, the I'm not sure what the "$month_names[//@m=current()]" is trying to do, but "." seems to output "1" to "5".

(as an aside [//@m=current()] will return true, and hence will just return the first $month_name, because this predicate is simply testing if there is a @m attribute that equals the current (from the for-each loop) @m attribute.)

/- Sam Judson : Wrox Technical Editor -/
 
Old October 31st, 2007, 10:15 AM
Registered User
 
Join Date: Oct 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Sam, that's helpful. I've been trying to create a pivot table with all the values of @n as rows and @m as columns with the value of h as the table contents, but is a bit beyond me at the moment!!

 
Old October 31st, 2007, 10:41 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
<xsl:variable name="root" select="//h" />


            <xsl:template match="/" priority="1">
            <table>
                <tr>
                    <td> </td>
                <xsl:for-each select="$tw_names">
                    <td><xsl:value-of select="."/></td>
                </xsl:for-each>    
                </tr>
                <xsl:for-each select="$month_names">
                    <xsl:variable name="current_m" select="."/>
                    <tr>
                        <td><xsl:value-of select="."/></td>
                        <xsl:for-each select="$tw_names">
                            <xsl:variable name="current_n" select="."/>
                            <td><xsl:value-of select="$root[@m=$current_m and @n=$current_n]" /></td>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </table>
             </xsl:template>
/- Sam Judson : Wrox Technical Editor -/
 
Old October 31st, 2007, 10:56 AM
Registered User
 
Join Date: Oct 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sam, if I ever get a chance I'll gladly buy you several beers!!!!


Very many thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping with XSLT 1.0 rodmcleay XSLT 1 January 14th, 2008 11:42 PM
XSLT Grouping vernc XSLT 2 October 2nd, 2007 03:59 AM
Grouping based on attributes values Chamkaur XSLT 4 June 21st, 2006 05:51 AM
XSLT 1.0 Grouping kwilliams XSLT 0 January 11th, 2006 06:30 PM
XSLT Grouping Help Missy XSLT 0 December 14th, 2005 10:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.