RE: [xsl] Sorting and grouping with xsl:analyze

Subject: RE: [xsl] Sorting and grouping with xsl:analyze
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 26 Oct 2006 13:54:16 +0100
I get the impression that you want the final sort to apply to all the
numbers, not just to each Title-group in turn. That means the second
for-each-group can't be inside the first for-each-group. You need to capture
all the numbers in all the titles in a variable, which you achieve by
wrapping the xsl:for-each-group in xsl:variable, and then apply grouping on
the contents of this variable. Or in fact, distinct-values. So:

<xsl:variable name="all-numbers" as="xs:decimal*">
  <xsl:for-each-group select=".....//Title">
     ...
     <xsl:sequence select="xs:decimal(.)"/>
     ...
  </xsl:for-each-group>
</xsl:variable>

<xsl:perform-sort select="distinct-values($all-numbers)">
  <xsl:sort/>
</xsl:perform-sort>

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


> -----Original Message-----
> From: Philip Vallone [mailto:philip.vallone@xxxxxxxxxxx]
> Sent: 26 October 2006 12:19
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Sorting and grouping with xsl:analyze
>
> Hello,
>
> I am having trouble grouping and sorting a string. From a
> previous post (RE:
> [xsl] Function to Extract integer from string), I extract
> some integers from a string. Now I am trying to group & sort
> those integers. The following XSLT outputs to html:
>
> <xsl:for-each-group select="$XML1/manual/title/document(.)//Title"
> group-by=".">
> 	<xsl:if test="contains(., ''')">
> 		<xsl:variable name="chap" select="."/>
> 			<xsl:variable name="all"
> select="replace(.,'[^ 0-9\.]','')"/>
> 			<xsl:variable name="numbers" as="xs:string*">
> 				<xsl:analyze-string select="."
> regex="\d+\.?\d*">
> 					<xsl:matching-substring>
> 						<xsl:sequence
> select="."/>
> 					</xsl:matching-substring>
> 				</xsl:analyze-string>
> 			</xsl:variable>
> 		<xsl:for-each-group select="current-group()"
> group-by="$numbers">
> 		<xsl:sort/>
> 		<xsl:for-each select="current-grouping-key()">
> 			<xsl:value-of select="."/>
> 			<p/>
> 			</xsl:for-each>
> 		</xsl:for-each-group>
> 	</xsl:if>
> </xsl:for-each-group>
>
> The XML file looks like this:
>
> <manual>
> 	<title>MelChapter1.xml</title>
> 	<title>Chapter9a.xml</title>
> </manual>
>
>
>
> The Results look like this:
>
> 21.197
> 21.197
> 21.199
> 91.407
> 1
> 121.133
> 91.12
> 121.135
>
> I want to have it look like this:
> 1
> 21.197
> 21.199
> 91.12
> 91.407
> 121.133
> 121.135
>
> Any help would be great.
>
> Phil

Current Thread