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 March 17th, 2005, 07:09 AM
Registered User
 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default does grouping sort too?

The following code gives an alphabetical list of h2's, 1 for each report "@type". In the xml file the "Report"s are NOT in alphabetical order by type. Why is this? In addition I need to sort the reports by title within each group, which so far eludes me.

TIA

<xsl:for-each-group select="/Reports/Report" group-by="@type">
<h2><xsl:value-of select="current-grouping-key()"/></h2>[list]<xsl:for-each select="current-group()/Rpt_Title">
<li><a href="{concat('DESC_',current()/../Rpt_Code,'.html')}">
<xsl:apply-templates/></a></li>
</xsl:for-each></ul>
</xsl:for-each-group>

xml:
<Reports>
<Report type='Transactions'>
<Rpt_Code>TXNSEC_04</Rpt_Code>
<Rpt_Title>Detail of Transactions</Rpt_Title>
</Report>
<Report type='Data Extract'>
<Rpt_Code>DWLD_03</Rpt_Code>
<Rpt_Title>Dividends Data Extract</Rpt_Title>
</Report>
</Reports>

result:
<h2>Data Extract</h2>[list]<li><a href="DESC_DWLD_03.html">Dividends Data Extract</a></li></ul>
<h2>Transactions</h2>[list]<li><a href="DESC_TXNSEC_04.html">Detail of Transactions</a></li></ul>


 
Old March 17th, 2005, 07:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The default order of groups is "order of first appearance" - i.e. the groups are ordered according to the position of their first item in the original population.

To sort the groups according to the value of the grouping key, use
<xsl:sort select="current-grouping-key()"/> immediately within <xsl:for-each-group>

To sort the items within each group, use an <xsl:sort> element as a child of the <xsl:apply-templates> instruction.

If you think you are getting results that don't follow the spec, it's useful to say what XSLT processor you are using.

Michael Kay
http://www.saxonica.com/

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 17th, 2005, 09:45 AM
Registered User
 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mhkay
 The default order of groups is "order of first appearance" - i.e. the groups are ordered according to the position of their first item in the original population.


Is "order of first appearance" the order in which they are encountered in the xml file going from top to bottom or is it something else? I am using an evaluation version of XMLSpy 2005r3.

Thanks for your help,
KSmith

 
Old March 17th, 2005, 10:14 AM
Registered User
 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Frustrating ... even this yields alpha by type no matter how the xml is organized.

<xsl:for-each-group select="/Reports/Report" group-by="@type">
<xsl:sort select="current-grouping-key()" order="descending" data-type="text"/>

Lost ...



 
Old March 17th, 2005, 10:41 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

"order of first appearance" reflects the order of items in the result of the expression in the select attribute of xsl:for-each-group. In most cases this will be document order (what you describe as "the order in which they are encountered in the xml file going from top to bottom") but not necessarily. For example if you do <xsl:for-each-group select="reverse(//a)"> then it will be reverse document order.

It sounds from your description as if XML Spy has a bug here, which wouldn't be that surprising, as their XSLT 2.0 processor is very new.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
Protect cells and allow grouping/un-grouping sfreuden Excel VBA 4 December 14th, 2006 08:01 AM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM
Sibling sort after grouping mazzer XSLT 0 October 27th, 2005 06:00 PM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM





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