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 June 5th, 2007, 03:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you want help debugging your code, then please post the actual code that doesn't work, not some approximation to it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 5th, 2007, 04:40 AM
Registered User
 
Join Date: Jun 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the actual code dealing with the grouping, it's quite long thus I didn't post it before.
The value of $groupBy is passed from a c# program and it works fine.
The template "eachresult" shows the title, author and other information.

This is a part of a MS SharePoint web part XSLT file.

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="urn:schemas-microsoft-com:xslt" extension-element-prefixes="exsl">
<xsl:key name="rows" match="Result" use="PUBLISHDATE" />
<xsl:key name="rows" match="Result" use="AUTHOR" />
<xsl:param name="recordsPerPage" select="10" />
<xsl:param name="groupBy" />

...
...
...

  <xsl:template match="All_Results">
    <xsl:choose>
      <xsl:when test="$groupBy='publishdate'">
          <xsl:apply-templates select="Result[generate-id(.) = generate-id(key('rows', PUBLISHDATE)[1])]">
          <xsl:sort select="PUBLISHDATE"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:when test="$groupBy='author'">
        <xsl:apply-templates select="Result[generate-id(.) = generate-id(key('rows', AUTHOR)[1])]">
          <xsl:sort select="AUTHOR"/>
        </xsl:apply-templates>
      </xsl:when>
     </xsl:choose>
    <xsl:call-template name="pagination" />
  </xsl:template>

 <xsl:template match="Result">
      <xsl:choose>
        <xsl:when test="$groupBy='publishdate'">
            <b>
              <xsl:value-of select="PUBLISHDATE" />
            </b>
            <br />
          <xsl:variable name="groupedset">
            <xsl:for-each select="key('rows', PUBLISHDATE)">
                <xsl:call-template name="eachresult" />
            </xsl:for-each>
          </xsl:variable>
          <xsl:for-each select="exsl:node-set($groupedset)/*[position() mod $recordsPerPage = 1]">
            <xsl:copy-of select=".|following-sibling::*[position() &lt; $recordsPerPage]"/>
          </xsl:for-each>
        </xsl:when>
         <xsl:when test="$groupBy='author'">
           <b>
             <xsl:value-of select="AUTHOR" />
           </b>
           <br />
          <xsl:variable name="groupedset">
            <xsl:for-each select="key('rows', AUTHOR)">
                <xsl:call-template name="eachresult" />
            </xsl:for-each>
          </xsl:variable>
          <xsl:for-each select="exsl:node-set($groupedset)/*[position() mod $recordsPerPage = 1]">
            <xsl:copy-of select=".|following-sibling::*[position() &lt; $recordsPerPage]"/>
          </xsl:for-each>
         </xsl:when>
      </xsl:choose>
</xsl:template>

<xsl:template name="eachresult">
 <b><xsl:value-of select="TITLE"/></b><br />
 <xsl:value-of select="DESCRIPTION"/><br />
 <xsl:value-of select="AUTHOR"/>, <xsl:value-of select="PUBLISHDATE"/><br />
</xsl:template>
I hope this is enough because the other parts are just doing some font formatting...
Thank you very much.
 
Old June 5th, 2007, 05:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The template eachresult is going to generate a sequence something like

<b>Some Title</b>
<br/>
Some description
<br/>
Some author, 2007-02-15
<br/>

So the value of $groupedset will be a document node whose contents are something like:

<b>Some Title</b>
<br/>
Some description
<br/>
Some author, 2007-02-15
<br/>
<b>Some other Title</b>
<br/>
Some other description
<br/>
Some other author, 2007-02-15
<br/>

You're then doing $groupset/*[position() mod $X = 1]. If $X is 3, you're going to select

<b>Some Title</b>
<br/>
<br/>

which doesn't make much sense. You're not selecting the text nodes at all. I think you want eachresult to create a wrapper element around this lot, and then you probably want the copy-of to drop the wrapper element. Say:

<xsl:template name="eachresult">
<result>
 <b><xsl:value-of select="TITLE"/></b><br /> <xsl:value-of select="DESCRIPTION"/><br /> <xsl:value-of select="AUTHOR"/>, <xsl:value-of select="PUBLISHDATE"/><br />
</result>
</xsl:template>

then

          <xsl:for-each select="exsl:node-set($groupedset)/result[position() mod $recordsPerPage = 1]">
            <xsl:copy-of select="(.|following-sibling::*[position() &lt; $recordsPerPage])/node()"/>
          </xsl:for-each>

except that this isn't actually splitting it into pages; I would expect to see something like

          <xsl:for-each select="exsl:node-set($groupedset)/result[position() mod $recordsPerPage = 1]">
<page>
            <xsl:copy-of select="(.|following-sibling::*[position() &lt; $recordsPerPage])/node()"/>
</page>
          </xsl:for-each>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 6th, 2007, 01:52 AM
Registered User
 
Join Date: Jun 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the detailed reply.

I have the codes changed to the following:

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="urn:schemas-microsoft-com:xslt" extension-element-prefixes="exsl">
<xsl:key name="rows" match="Result" use="PUBLISHDATE" />
<xsl:key name="rows" match="Result" use="AUTHOR" />
<xsl:param name="recordsPerPage" select="10" />
<xsl:param name="groupBy" />

...
...
...

  <xsl:template match="All_Results">
    <xsl:choose>
      <xsl:when test="$groupBy='publishdate'">
          <xsl:apply-templates select="Result[generate-id(.) = generate-id(key('rows', PUBLISHDATE)[1])]">
          <xsl:sort select="PUBLISHDATE"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:when test="$groupBy='author'">
          <xsl:apply-templates select="Result[generate-id(.) = generate-id(key('rows', AUTHOR)[1])]">
          <xsl:sort select="AUTHOR"/>
        </xsl:apply-templates>
      </xsl:when>
     </xsl:choose>
    <xsl:call-template name="pagination" />
  </xsl:template>

 <xsl:template match="Result">
      <xsl:choose>
        <xsl:when test="$groupBy='publishdate'">
            <b>
              <xsl:value-of select="PUBLISHDATE" />
            </b>
            <br />
          <xsl:variable name="groupedset">
            <xsl:for-each select="key('rows', PUBLISHDATE)">
                <xsl:call-template name="eachresult" />
            </xsl:for-each>
          </xsl:variable>
          <xsl:for-each select="exsl:node-set($groupedset)/record[position() mod $recordsPerPage = 1]">
            <page>
              <xsl:copy-of select="(.|following-sibling::*[position() &lt; $recordsPerPage])/node()"/>
            </page>
          </xsl:for-each>
        </xsl:when>
         <xsl:when test="$groupBy='author'">
           <b>
             <xsl:value-of select="AUTHOR" />
           </b>
           <br />
          <xsl:variable name="groupedset">
            <xsl:for-each select="key('rows', AUTHOR)">
                <xsl:call-template name="eachresult" />
            </xsl:for-each>
          </xsl:variable>
          <xsl:for-each select="exsl:node-set($groupedset)/record[position() mod $recordsPerPage = 1]">
            <xsl:copy-of select="(.|following-sibling::*[position() &lt; $recordsPerPage])/node()"/>
          </xsl:for-each>
         </xsl:when>
      </xsl:choose>
</xsl:template>

<xsl:template name="eachresult">
<record>
 <b><xsl:value-of select="TITLE"/></b><br />
 <xsl:value-of select="DESCRIPTION"/><br />
 <xsl:value-of select="AUTHOR"/>, <xsl:value-of select="PUBLISHDATE"/><br />
</record>
</xsl:template>

With $recordsPerPage set to 4, the result is something like the following format:

Code:
GROUP A
<page>
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>
</page>

GROUP B
<page>
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>
</page>
<page>
<record>Record of Article</record>
<record>Record of Article</record>
</page>

GROUP C
<page>
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>
</page>
<page>
<record>Record of Article</record>
</page>
The format that I want is something like the following:

Code:
GROUP A
<page>
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>

GROUP B
<record>Record of Article</record>
</page> 
<page>
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>
</page>
<page>
<record>Record of Article</record>

GROUP C
<record>Record of Article</record>
<record>Record of Article</record>
<record>Record of Article</record>
</page>
<page> 
<record>Record of Article</record>
<record>Record of Article</record>
</page>
position() returns the position of the "record" inside a GROUP and so one page has wrong number of records. What should I change the code it format correctly?

Thanks in advance.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Protect cells and allow grouping/un-grouping sfreuden Excel VBA 4 December 14th, 2006 08:01 AM
grouping and pagination sasidhar79 Crystal Reports 1 December 6th, 2005 01:12 PM
pagination Regornil Pro JSP 2 May 31st, 2005 06:10 AM
Please.....Pagination junemo PHP Databases 2 September 25th, 2004 01:14 PM
pagination junemo Oracle 0 July 1st, 2004 04:07 AM





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