source: MDRepository/trunk/xquery/group.xsl @ 1045

Last change on this file since 1045 was 1045, checked in by vronk, 13 years ago

mainly added new API-function: scanIndex
(group.xsl used for aggregating)

File size: 3.1 KB
Line 
1<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
2 xmlns:xs="http://www.w3.org/2001/XMLSchema"
3version="2.0">
4
5<!--
6two tasks (in separate calls, managed by $mode-param):
71. produces an index by grouping content       
82. selects a subsequence of the produced content
9-->
10
11<xsl:param name="mode" select="'aggregate'" />
12<xsl:param name="sort" select="'text'" />
13<xsl:param name="filter" select="''" />
14<xsl:param name="filter-mode" select="if (ends-with($filter,'*')) then 'starts-with' else 'contains'" /> <!-- contains, starts-with -->
15<xsl:param name="start-item" select="100" />
16<xsl:param name="max-items" select="100" />
17
18    <xsl:template match="/">
19<!--            <Terms>
20                        <params mode="{$mode}" sort="{$sort}"  /> -->
21                <xsl:choose>
22                        <xsl:when test="$mode='subsequence'" >
23                                <xsl:apply-templates mode="subsequence" />
24                        </xsl:when>
25                        <xsl:otherwise>
26                                <xsl:apply-templates />
27                        </xsl:otherwise>
28                </xsl:choose>
29    </xsl:template>
30
31    <xsl:template match="Term">
32                <xsl:variable name="nodes" select="*" />
33                <xsl:variable name="count-text" select="count($nodes/text()[.!=''])" />
34                <xsl:variable name="distinct-text-count" select="count(distinct-values($nodes/text()))" />
35
36                <Term name="{*[1]/name()}" count="{count($nodes)}" count_text="{$count-text}"  count_distinct_text="{$distinct-text-count}">
37                        <xsl:copy-of select="@*" />
38            <xsl:for-each-group select="*" group-by="text()">
39                <v key="{text()}" count="{count(current-group())}"/>
40            </xsl:for-each-group>
41        </Term>
42    </xsl:template>
43
44    <xsl:template match="Term" mode="subsequence">
45
46        <!-- this may be potentially expensive and we may need to store the index already sorted (which is the normal/sane way to do!) -->
47                <xsl:variable name="filtered" select="*[if ($filter!='') then
48                                                                                                                if ($filter-mode='starts-with') then starts-with(@key,substring-before($filter,'*'))
49                                                                                                                                        else contains(@key, $filter)
50                                                                                                                else true()]" />
51                <xsl:variable name="ordered" >
52                        <xsl:choose>
53                        <xsl:when test="$sort='size'" > 
54                                <xsl:for-each select="$filtered" >
55                                        <xsl:sort select="@count" data-type="number" order="descending" />
56                                        <xsl:copy-of select="." />
57                                </xsl:for-each>
58                        </xsl:when>
59                        <xsl:otherwise>
60                                <xsl:for-each select="$filtered" >
61                                        <xsl:sort select="@key" data-type="text" order="ascending" />                                   
62                                        <xsl:copy-of select="."/>
63                                </xsl:for-each>
64                        </xsl:otherwise>
65                        </xsl:choose>
66                </xsl:variable>
67
68<!--            <xsl:variable name="count-items" select="count($filtered)" />-->
69                <xsl:copy>
70                        <xsl:copy-of select="@*" />
71<!--                    <xsl:value-of select="count($ordered/*)" /> -->
72<!--                    <xsl:attribute name="count_items" select="if (xs:integer($count-items) &gt; xs:integer($max-items)) then $max-items else $count-items" /> -->
73                                <xsl:apply-templates select="$ordered/*[xs:integer(position()) &gt;= xs:integer($start-item) and (xs:integer(position()) &lt; (xs:integer($start-item) + xs:integer($max-items)))]" />
74                </xsl:copy>
75    </xsl:template>
76
77        <xsl:template match="v" >
78                <xsl:copy>
79                        <xsl:copy-of select="@*" />
80                        <xsl:attribute name="pos" select="position()" />
81                </xsl:copy>
82        </xsl:template>
83
84</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.