source: SMC/trunk/SMC/src/scripts/smc_functions.xsl @ 2231

Last change on this file since 2231 was 2231, checked in by vronk, 12 years ago

added util functions

File size: 11.7 KB
Line 
1<?xml version="1.0"?>
2<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
3 xmlns:my="myFunctions">
4
5<!--
6<purpose>functions for SMC</purpose>
7<history>
8        <change on="2011-10-28" type="created" by="vr">based on cmd_functions.xsl</change>
9</history>
10-->
11
12<!-- did not work
13        <xsl:variable name="all-terms-nested" select="doc('file:/C:/Users/m/3lingua/clarin/CMDI/_repo2/SMC/data/cmd-terms-nested.xml')" />
14        <xsl:key name="term-name" match="//Term" use="@name"></xsl:key>
15-->
16 <!--
17 @param profiles - list of <profileDescription>
18 -->
19        <xsl:function name="my:profiles2termsets" >
20                <xsl:param name="profiles"/>
21                <xsl:param name="nested"/>
22               
23                <Termsets count="{count($profiles)}">
24                <xsl:for-each select="$profiles" >
25                        <xsl:variable name="profile_id" select="id"></xsl:variable>
26                        <Termset name="{name}"  id="{$profile_id}" type="CMD_Profile">
27                               
28                                        <!-- flattening the structure! -->
29                                <xsl:choose>
30                                        <xsl:when test="$nested">
31                                                <xsl:apply-templates select="CMD_ComponentSpec/CMD_Component" mode="make-term">
32                                                        <xsl:with-param name="nested" select="$nested"></xsl:with-param>
33                                                </xsl:apply-templates>
34                                        </xsl:when>
35                                        <xsl:otherwise>
36                                                <xsl:apply-templates select=".//CMD_Component|.//CMD_Element" mode="make-term">
37                                                        <xsl:with-param name="nested" select="$nested"></xsl:with-param>
38                                                </xsl:apply-templates>
39                                        </xsl:otherwise>
40                                </xsl:choose>
41                                       
42<!--                                   
43                                <xsl:for-each select=".//CMD_Component|.//CMD_Element" >
44                                                <xsl:variable name="context" select="my:context(.)" />                                         
45                                       
46                                        <xsl:variable name="type" select="name()" />                                   
47                                        <xsl:variable name="id"  >
48                                                <xsl:choose>
49                                                        <xsl:when test="@ComponentId">
50                                                                <xsl:value-of select="@ComponentId" />
51                                                        </xsl:when>
52                                                        <!-\- top component = profile -\->
53                                                        <xsl:when test="not(exists(ancestor::CMD_Component))">
54                                                                <xsl:value-of select="$profile_id" />
55                                                        </xsl:when>
56                                                        <xsl:otherwise>
57                                                                <xsl:value-of select="ancestor::CMD_Component[@ComponentId][1]/@ComponentId" /><xsl:text>#</xsl:text>
58                                                                <xsl:for-each select="ancestor::CMD_Component[not(descendant-or-self::CMD_Component[@ComponentId])]" >
59                                                                        <xsl:value-of select="@name" />.</xsl:for-each><xsl:value-of select="@name" />
60                                                        </xsl:otherwise>
61                                                </xsl:choose>                                                   
62                                        </xsl:variable>
63                                        <Term  type="{$type}" name="{@name}" datcat="{@ConceptLink}" id="{$id}"  elem="{@name}"
64                                                parent="{ancestor::CMD_Component[1]/@name}" path="{$context}"
65                                                >
66                                                <!-\-  <xsl:copy-of select="." /> -\->
67                                        </Term>
68                                        </xsl:for-each>
69-->                     </Termset>
70                </xsl:for-each>
71                </Termsets>
72        </xsl:function>
73
74        <xsl:template match="CMD_Component|CMD_Element" mode="make-term">
75        <xsl:param name="nested" select="true()"></xsl:param>
76        <xsl:variable name="context" select="my:context(.)" />                                         
77       
78        <xsl:variable name="type" select="name()" />                                   
79        <xsl:variable name="id"  >
80                <xsl:choose>
81                        <xsl:when test="@ComponentId">
82                                <xsl:value-of select="@ComponentId" />
83                        </xsl:when>
84                        <!-- top component = profile -->
85                        <xsl:when test="not(exists(ancestor::CMD_Component))">
86                                <xsl:value-of select="(ancestor::profileDescription/id, ancestor::CMD_ComponentSpec/Header/ID)[1]" />
87                        </xsl:when>
88                        <xsl:otherwise>
89                                <xsl:value-of select="ancestor::CMD_Component[@ComponentId][1]/@ComponentId" /><xsl:text>#</xsl:text>
90                                <xsl:for-each select="ancestor::CMD_Component[not(descendant-or-self::CMD_Component[@ComponentId])]" >
91                                        <xsl:value-of select="@name" />.</xsl:for-each><xsl:value-of select="@name" />
92                        </xsl:otherwise>
93                </xsl:choose>                                                   
94        </xsl:variable>
95               
96        <Term  type="{$type}" name="{@name}" datcat="{@ConceptLink}" id="{$id}"  elem="{@name}"
97                parent="{ancestor::CMD_Component[1]/@name}" path="{$context}"
98                >
99                <!--  <xsl:copy-of select="." /> -->
100                <xsl:if test="$nested">
101                        <xsl:apply-templates select="CMD_Component|CMD_Element" mode="make-term">
102                                <xsl:with-param name="nested" select="$nested"></xsl:with-param>
103                        </xsl:apply-templates>
104                </xsl:if>
105        </Term>
106</xsl:template>
107
108<!--
109<xsl:function name="my:profile2termset" >
110    <xsl:param name="term"/>
111   
112    <xsl:variable name="profile" select="my:profile($term,true())" />
113        <xsl:copy-of select="my:profiles2termsets($profile)" />
114       
115</xsl:function>
116
117<xsl:function name="my:profile" >
118    <xsl:param name="term"/>
119    <xsl:param name="resolve" /> <!-\-  true|false-\->
120   
121    <!-\- <xsl:message>cmdprofiles_uri: <xsl:value-of select="$cmdprofiles_uri" /></xsl:message>  -\->
122   
123        <xsl:variable name="profile" select="$cmd_profiles//profileDescription[name=$term or $term='all']" />
124   
125    <xsl:choose>
126      <xsl:when test="$resolve=true()">
127                <xsl:apply-templates select="$profile" mode="include" />
128      </xsl:when>
129      <xsl:otherwise>
130                <xsl:copy-of select="$profile" />
131      </xsl:otherwise>
132    </xsl:choose>
133   
134       
135</xsl:function>-->
136
137<!--  constructs a dot-path from ancestor-CMD_component-elements -->
138<xsl:function name="my:context" >
139        <xsl:param name="child" />
140        <xsl:variable name="collect" >
141                        <xsl:for-each select="$child/ancestor::CMD_Component|$child/ancestor::Term" >
142                                        <xsl:value-of select="@name" />.</xsl:for-each><xsl:value-of select="$child/@name" />
143        </xsl:variable>
144        <xsl:value-of select="$collect" />     
145</xsl:function> 
146
147
148        <xsl:function name="my:diffContext" >
149                <xsl:param name="c1" />
150                <xsl:param name="c2" />
151                <!-- <xsl:message>C1:<xsl:copy-of select="count($c1/ancestor::Term/@name)" /></xsl:message>
152                        <xsl:message><xsl:value-of select="$c1/ancestor::Term/@name" /></xsl:message>
153                        <xsl:message>C2:<xsl:value-of select="$c2/ancestor::Term/@name" /></xsl:message>
154                -->
155                <!--            <xsl:value-of select="($c1/ancestor::Term/@name except $c2/ancestor::Term/@name)[last()]" />-->
156               
157                <!-- <xsl:value-of select="string-join($c1/ancestor::Term/@name except $c2/ancestor::Term/@name,'.')" /> -->
158                <!--   <xsl:copy-of select="$c1/ancestor::Term/@name except $c2/ancestor::Term/@name" />-->
159                <xsl:variable name="diff" select="$c1/ancestor::Term/xs:string(@name[not(.=$c2/ancestor::Term/xs:string(@name))])" />
160                <!-- <xsl:message>diff: <xsl:value-of select="$diff" /></xsl:message>  -->
161                <xsl:copy-of select="$diff" />
162               
163        </xsl:function>
164       
165        <xsl:template match="*" mode="min-context" >
166                        <xsl:copy>
167                                <xsl:copy-of select="@*"></xsl:copy-of>
168                                <xsl:apply-templates mode="min-context">                                       
169                                </xsl:apply-templates>
170                        </xsl:copy>
171                </xsl:template>
172       
173<!--  computing minimal unique path/index -->
174<xsl:template match="Term" mode="min-context" >
175                <xsl:param name="all-terms" select="$cmd-terms-nested" />
176                <xsl:param name="term" select="." />
177               
178               
179                <!--<xsl:variable name="termset_id" select="if (exists(ancestor::Termset/@id)) then ancestor::Termset/@id else ancestor::Termset/@name" />-->
180        <xsl:variable name="termset_name" select="ancestor::Termset/@name" />
181               
182                <!-- unable to use lookup here - error:
183                        net.sf.saxon.tree.wrapper.VirtualCopy "cannot be cast" to net.sf.saxon.om.DocumentInfo -->
184        <xsl:variable name="ambi_terms" select="$all-terms//Term[lower-case(@name)=lower-case($term/@name)]" />
185        <!--    loookup did not work!
186                <xsl:variable name="ambi_terms" >       
187                <xsl:call-template name="lookup">
188                        <xsl:with-param name="key" select="$term/@name"></xsl:with-param>
189                </xsl:call-template>
190                <xsl:for-each select="$all-terms-nested">
191                        <xsl:value-of select="key('term-name', lower-case($term/@name))"/>                     
192                </xsl:for-each>
193                </xsl:variable> -->
194               
195                <xsl:variable name="min-path" >                 
196                        <!-- don't apply prefix for the root element (=profile-name) -->               
197                        <xsl:if test="not($term/parent::Termset)" >
198                                <xsl:value-of select="$termset_name" ></xsl:value-of><xsl:text>:</xsl:text>
199                        </xsl:if> 
200                       
201                        <xsl:choose>           
202                                <xsl:when test="count($ambi_terms) &gt; 1" >
203                                        <xsl:variable name="ambi_terms_parent" select="$ambi_terms/*[@name=$term/@name]" />
204                                        <xsl:variable name="term1" select="." />
205                                        <xsl:message>Term:<xsl:value-of select="$term1/@path" />|ambi: <xsl:value-of select="count($ambi_terms)" />|</xsl:message>
206                                        <xsl:variable name="minimal_contexts" >
207                                                <xsl:for-each select="$ambi_terms/*[not(@path=$term1/@path)]" > 
208                                                        <!--                                            [position() &gt; current()/position()]" >                       -->
209                                                        <item>                                                                 
210                                                                <xsl:variable name="diff"  select="my:diffContext($term1,.)" />
211                                                                <!-- <xsl:message>diff: <xsl:value-of select="count($diff)" /></xsl:message>-->
212                                                                <xsl:copy-of select="$diff" />                                                                   
213                                                               
214                                                        </item>                                                                 
215                                                </xsl:for-each>
216                                        </xsl:variable>
217                                        <!-- DEBUG:                             diffcontext1:<xsl:value-of select="count($minimal_contexts)" />
218                                                |direct: <xsl:value-of select="count($minimal_contexts[1]/*)" />: <xsl:value-of select="$minimal_contexts[1]/*" />|
219                                                |item1: <xsl:value-of select="count($minimal_contexts/item[1]/*)" />: <xsl:value-of select="$minimal_contexts/item[1]/*" />|
220                                                cnt_mci:<xsl:value-of select="$minimal_contexts/*/count(tokenize(.,' '))" ></xsl:value-of>/
221                                                max-strlen:<xsl:value-of select="max($minimal_contexts/*/string-length(.))" ></xsl:value-of>
222                                        -->                                                     
223                                        <xsl:variable name="last_contexts" select="distinct-values($minimal_contexts/*/tokenize(.,' ')[last()])" ></xsl:variable>
224                                        <!--  to get the order right -->
225                                        <xsl:for-each select="$last_contexts" >
226                                                <xsl:sort select="index-of(tokenize($term1/@context,'\.'),.)[1]" /> 
227                                                <xsl:value-of select="." />.</xsl:for-each>
228                                        <!-- DEBUG:     cnt_lc:<xsl:value-of select="count($last_contexts)" />}
229                                                cnt_tok:<xsl:value-of select="count(tokenize($term1/@context,'\.'))" />
230                                               
231                                                <xsl:for-each select="tokenize($term1/@context,'\.')" >
232                                                tokenizing-context:<xsl:value-of select="." />,<xsl:value-of select="current()" />|
233                                                <xsl:if test="$last_contexts[.=current()]" >
234                                                {<xsl:value-of select="." />}
235                                               
236                                                </xsl:if>
237                                                </xsl:for-each>
238                                        -->
239                                        <!--                                                            <xsl:value-of select="string-join(distinct-values($minimal_contexts/*/tokenize(.,' ')[last()]),'.')" ></xsl:value-of>           -->                                             
240                                </xsl:when>
241                                <xsl:otherwise>                 
242                                </xsl:otherwise>
243                        </xsl:choose>
244                        <xsl:value-of select="@name" ></xsl:value-of>
245                </xsl:variable>
246               
247                <xsl:copy>
248                        <xsl:copy-of select="@*"></xsl:copy-of>
249                        <!--            <xsl:copy-of select="$path" />-->
250                        <xsl:attribute name="min-path" select="$min-path" />
251                       
252                        <xsl:apply-templates select="Term" mode="min-context" >
253                                <xsl:with-param name="all-terms" select="$all-terms"></xsl:with-param>
254                        </xsl:apply-templates>
255
256                </xsl:copy>                                                     
257               
258        </xsl:template>
259        <!--
260        <xsl:template name="lookup">
261                <xsl:param name="key"/>
262                <xsl:for-each select="$all-terms-nested">       
263                        <xsl:value-of select="key('term-name', $key)"/>
264                        <xsl:message><xsl:value-of select="key('term-name', $key)"/></xsl:message>
265                </xsl:for-each>
266                <xsl:message></xsl:message>
267        </xsl:template>
268       
269        -->
270       
271        <xsl:function name="my:shortURL" >
272                <xsl:param name="url" />
273                <!--   <xsl:value-of select="replace($url, 'http://www.isocat.org/datcat/','isocat:')" /> -->
274                <xsl:variable name="matching_termset"  select="if($url!='') then $termsets_config/Termsets/*[url_prefix][not(url_prefix='')][starts-with($url,url_prefix)] else ()" />
275                <!-- <xsl:variable name="matchinge_termset"  select="if($url!='') then if ($terms_setup/Termsets/Termset[@url_prefix][starts-with($url,@url_prefix)]) then $terms_setup/Termsets/Termset[@url_prefix][starts-with($url,@url_prefix)][1]
276                        else if $terms_setup/Termsets/Termset[@url][starts-with($url,@url)] then $terms_setup/Termsets/Termset[@url][starts-with($url,@url)][1] else ()" /> --> 
277                <xsl:message>shortURL: <xsl:value-of select="$matching_termset/url_prefix" />:: <xsl:value-of select="$url" />
278                </xsl:message>                 
279                <xsl:value-of select="if ($matching_termset/url_prefix and $matching_termset/url_prefix!='' and $url!='') then replace($url, $matching_termset[1]/url_prefix, concat(string-join($matching_termset/key,','),':')) else $url" />         
280        </xsl:function> 
281       
282       
283</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.