<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>5166</bug_id>
          
          <creation_ts>2007-10-09 05:07:05 +0000</creation_ts>
          <short_desc>[XSLT 2++] Enhancement: sorting and grouping</short_desc>
          <delta_ts>2014-05-15 14:00:11 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>XPath / XQuery / XSLT</product>
          <component>XSLT 3.0</component>
          <version>Recommendation</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Windows XP</op_sys>
          <bug_status>CLOSED</bug_status>
          <resolution>LATER</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>enhancement</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Michael Kay">mike</reporter>
          <assigned_to name="Michael Kay">mike</assigned_to>
          <cc>viktor_b_68</cc>
          
          <qa_contact name="Mailing list for public feedback on specs from XSL and XML Query WGs">public-qt-comments</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>17089</commentid>
    <comment_count>0</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2007-10-09 05:07:05 +0000</bug_when>
    <thetext>Discussions on grouping in XQuery today, and solving some tricky positional grouping problems for a client over the last week, prompt me to capture extensions that could be made to XSLT grouping and sorting capabilities:

(a) allow xsl:variable before xsl:sort, to compute a value that can be used both in the sort key expression and in the subsequent processing of the relevant item:

&lt;xsl:for-each select=&quot;product&quot;&gt;
  &lt;xsl:variable name=&quot;profitability&quot; select=&quot;price - cost&quot;/&gt;
  &lt;xsl:sort select=&quot;$profitability&quot;/&gt;
  &lt;xsl:value-of select=&quot;$profitability&quot;/&gt;
&lt;/xsl:for-each&gt;

(b) allow grouping keys to be specified in a separate group element:

&lt;xsl:for-each-group select=&quot;product&quot;&gt;
  &lt;xsl:group-by select=&quot;price&quot;/&gt;
  &lt;xsl:value-of select=&quot;avg(current-group()/cost)&quot;/&gt;
&lt;/xsl:for-each-group&gt;

(c) use this to allow composite grouping keys

&lt;xsl:for-each-group select=&quot;product&quot;&gt;
  &lt;xsl:group-by select=&quot;category&quot;/&gt;
  &lt;xsl:group-by select=&quot;colour&quot;/&gt;
  &lt;xsl:value-of select=&quot;avg(current-group()/cost)&quot;/&gt;
&lt;/xsl:for-each-group&gt;

(d) allow control over how a sequence-valued group key is handled

&lt;xsl:group-by select=&quot;author&quot; compare=&quot;each | first | only | sequence | set&quot;/&gt;

(each assigns to one group for each value of the key; first ignores all but the first, only requires exactly one, sequence compares the sequences as sequences, set compares them as sets)

(e) allow variables to be declared before the group-by:

&lt;xsl:for-each-group select=&quot;product&quot;&gt;
  &lt;xsl:variable name=&quot;profitability&quot; select=&quot;(price - cost) div cost&quot;/&gt;
  &lt;xsl:variable name=&quot;profit-band&quot; select=&quot;$profitability idiv 0.2&quot;/&gt;
  &lt;xsl:group-by select=&quot;$profit-band&quot;/&gt;
  &lt;xsl:value-of select=&quot;format-number($profit-band)&quot;/&gt;
&lt;/xsl:for-each-group&gt;

or

&lt;xsl:for-each-group select=&quot;*&quot;&gt;
  &lt;xsl:variable name=&quot;is-bullet&quot; select=&quot;boolean(self::bullet)&quot;/&gt;
  &lt;xsl:group-adjacent select=&quot;$is-bullet&quot;/&gt;
  &lt;xsl:choose&gt;
    &lt;xsl:when test=&quot;$is-bullet&quot;&gt;
       ...
&lt;/xsl:for-each-group&gt;

(e) group-starting-when in place of group-starting-with; the value is an expression rather than a pattern, and a new group starts when the expression is true. Allows the same concepts as above to be used for this case (and group-ending-when).

&lt;xsl:for-each-group select=&quot;para&quot;&gt;
  &lt;xsl:variable name=&quot;is-top-level&quot; select=&quot;@level = 0&quot;/&gt;
  &lt;xsl:variable name=&quot;is-heading&quot; select=&quot;title and not preceding-sibling::*[1]/self::title&quot;/&gt;
  &lt;xsl:group-starting-when test=&quot;$is-top-level and $is-heading&quot;/&gt;
  &lt;xsl:if test=&quot;$is-heading&quot;&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>17115</commentid>
    <comment_count>1</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2007-10-10 17:32:44 +0000</bug_when>
    <thetext>See also bug #4400</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>17121</commentid>
    <comment_count>2</comment_count>
    <who name="Sharon Adler">sca.w3c</who>
    <bug_when>2007-10-10 19:32:10 +0000</bug_when>
    <thetext>Considered as an enhancement request for VNext</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>21796</commentid>
    <comment_count>3</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2008-09-05 13:42:12 +0000</bug_when>
    <thetext>*** Bug 4400 has been marked as a duplicate of this bug. ***</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>24713</commentid>
    <comment_count>4</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2009-04-14 10:13:44 +0000</bug_when>
    <thetext>See also bug #4921

Use case encountered today: an up-conversion from a text file - needed to use group-starting-with on the sequence of text lines obtained effectively from the unparsed-text-lines() function. Had to convert these strings to nodes because group-starting-with only works on a sequence of nodes!

Possible solutions:

(a) group-starting-when as suggested above (accepting an expression rather than a pattern)

(b) patterns that match atomic values - this would also allow generalizing apply-templates to work on atomic values. For example, allow the pattern &quot;.[predicate]&quot; to match an atomic value for which the predicate is true. That&apos;s a bit inelegant for the case of &quot;.[. instance of xs:string]&quot; - perhaps allow &quot;~xs:string&quot; or type(xs:string) for that case.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>36960</commentid>
    <comment_count>5</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2010-07-16 14:48:24 +0000</bug_when>
    <thetext>Concerning (a), we can see the justification for this capability but are concerned about how the semantics would be specified. Would it require introducing the concept of tuple streams? Need to revisit.

Concerning (b)-(d) we have looked at composite grouping keys under bug #5577

For (e) the concerns about semantics are the same as (a) only more so.

For the second (e), positional grouping enhancements are considered under bug #4921.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>49334</commentid>
    <comment_count>6</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2011-06-08 20:28:31 +0000</bug_when>
    <thetext>Resolved at the Prague F2F March 2011 to mark this as &quot;Later&quot;.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>