Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 28th, 2006, 10:34 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default SORTING

Hi,

Note: I am using MSXML and XSLT 1.0

I have the following XML document:

<?xml version="1.0"?>
<Reports xmlns="">
  <Report>
    <Measures>
      <Measure idx="1" heading="Total Pages"/>
      <Measure idx="2" heading="Cost"/>
    </Measures>
    <Columns>
      <ColGrp heading="Year">
        <Col heading="2003"/>
        <Col heading="2004"/>
      </ColGrp>
    </Columns>
    <Rows>
      <RowGrp heading="Journal Full Name">
        <RowGrp heading="General Medicine">
          <Row heading="Journal of Musculoskeletal Med">
            <Cell/>
            <Cell>
              <Msr idx="1" val="16.00"/>
              <Msr idx="2" val="90560"/>
            </Cell>
          </Row>
          <Row heading="Physician &amp; Sports Medicine">
            <Cell/>
            <Cell>
              <Msr idx="1" val="20.00"/>
              <Msr idx="2" val="131930"/>
            </Cell>
          </Row>
        </RowGrp>
        <RowGrp heading="Orthopedics">
          <Row heading="Jnl American Acad of Ortho Sur">
            <Cell/>
            <Cell>
              <Msr idx="1" val="8.00"/>
              <Msr idx="2" val="18440"/>
            </Cell>
          </Row>
          <Row heading="Journal of Bone &amp; Joint Surger">
            <Cell/>
            <Cell>
              <Msr idx="1" val="16.00"/>
              <Msr idx="2" val="40160"/>
            </Cell>
          </Row>
          <Row heading="Orthopedics Today">
            <Cell/>
            <Cell>
              <Msr idx="1" val="20.00"/>
              <Msr idx="2" val="48800"/>
            </Cell>
          </Row>
        </RowGrp>
        <RowGrp heading="">
         <RowGrp heading="">
          <Row heading="Totals">
            <Cell>
              <Msr idx="1" val="0"/>
              <Msr idx="2" val="0"/>
            </Cell>
            <Cell>
              <Msr idx="1" val="140"/>
              <Msr idx="2" val="455410"/>
            </Cell>
          </Row>
        </RowGrp>
      </RowGrp>
    </Rows>
  </Report>
</Reports>

I use the following code to sort it:

<xsl:param name="sortCell" select="1"/>
<xsl:param name="sortCol" select="1"/>

<xsl:template match="Report" >
   <table class="grdMain" border="0" cellspacing="1" cellpadding="0">
          <tbody>
            <xsl:choose>
              <xsl:when test="number($sortCol) != 0 and //Row[position() != last()]">
                <xsl:apply-templates select="//Row" mode="data">
                  <xsl:sort select ="Cell[number($sortCell)]/descendant::Msr[number($sortCol)]/@val" data-type="{$dataType}" order ="{$sortOrder}" />
                </xsl:apply-templates>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates select="//Row" mode="data" />
              </xsl:otherwise>
            </xsl:choose>
          </tbody>
        </table>
</xsl:Template>

Please note that the value of the two parameters is passed to the xslt($sortCell and $sortCol).

The sort works fines but the problem is that it is also sorting the Totals row.

When I sort descending the Total row is being moved to the top of the table and that is wrong. I would like the Totals Row to stay always at the bottom of the table

Therefore, I am doing a test to see if the Row position is not the last before I sort but this is not working.

<xsl:when test="number($sortCol) != 0 and //Row[position() != last()]">

I appreciate any help on this.

Cheers

CP
Reply With Quote
  #2 (permalink)  
Old October 28th, 2006, 10:56 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

I have tried this test but it did not work either. The Totals row is still moving to the top when sort is set to descending.

<xsl:when test="number($sortCol) != 0 and //Row[@heading != 'Totals']">
Reply With Quote
  #3 (permalink)  
Old October 28th, 2006, 05:13 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You logic is:

<xsl:when test="number($sortCol) != 0 and //Row[position() != last()]">

"if $sortCol is not zero and there is a row that is not the last row"

<xsl:apply-templates select="//Row" mode="data">
                  <xsl:sort

"then sort all the rows".

What you want to say is "sort all the rows except the last one and then output the last row". That is:

 <xsl:apply-templates select="(//Row)[position()!=last()]" mode="data">
                  <xsl:sort ... />
 </xsl:apply-templates>
 <xsl:apply-templates select="(//Row)[last()]"/>

Note also the difference between (//Row)[last()] and (//Row[last()]). The first expression selects the last of all the rows, the second expression selects every Row that is the last child of its parent.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old October 29th, 2006, 08:45 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Thanks a lot Michael.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting member XSLT 25 July 13th, 2007 08:44 AM
Sorting sunny76 Excel VBA 2 September 19th, 2005 09:31 PM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
Sorting? pbernardo XSLT 2 October 27th, 2003 11:34 AM
Need help Sorting athanatos XSLT 5 August 11th, 2003 07:51 PM



All times are GMT -4. The time now is 11:43 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.