source: MDService2/trunk/MDService2/src/xsl/cmd_functions.xsl @ 1081

Last change on this file since 1081 was 1081, checked in by vronk, 13 years ago
File size: 12.0 KB
Line 
1<?xml version="1.0"?>
2<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:my="myFunctions">
4
5<!--
6<purpose>generic functions for MDService2</purpose>
7<history>
8        <change on="2010-12-29" type="created" by="vr">extracted from cmd_commons.xsl</change>
9</history>
10
11-->
12<xsl:include href="params.xsl"/>
13
14<!-- <xsl:param name="mode" select="'html'" /> -->
15
16<xsl:param name="dict_file" select="'dict.xml'" />
17<xsl:variable name="dict" select="document($dict_file)" />
18<xsl:variable name="terms_setup" select="document($terms_setup_uri)" />
19<xsl:variable name="terms_flat" select="document($terms_flat_uri)" />
20
21
22<!-- params for include-processing
23moved to params.xsl
24        <xsl:param name="components_uri" select="'http://catalog.clarin.eu/ds/ComponentRegistry/rest/registry/components/'" />         
25        <xsl:param name="prefix_replace" select="'file:///C:/Users/master/3lingua/clarin/CMDI/_repo2/metadata/toolkit/components'" />
26-->
27
28<!--  should be obsoleted by model2matrix.xsl
29but it is not..
30-->
31<xsl:variable name="term_matrix" >
32               
33                <xsl:for-each select="//CMD_Element" >
34                                        <xsl:variable name="context" select="my:context(.)" >
35                                        </xsl:variable>
36                        <Term  elem="{@name}" datcat="{@ConceptLink}" profile="{ancestor::profileDescription[1]/name}" 
37                                                        parent="{ancestor::CMD_Component[1]/@name}"  context="{$context}" >
38                                                                <xsl:copy-of select="." />
39                        </Term>
40                </xsl:for-each>
41       
42</xsl:variable>
43<!--
44<xsl:template match="Term" mode="matrix" >
45               
46                <xsl:for-each select=".//Term" >
47                        <xsl:variable name="context" select="my:context(.)" >
48                                        </xsl:variable>
49                        <Term  elem="{@name}" datcat="{my:term2datcat(@name)}" profile="{ancestor::profileDescription[1]/name}"
50                                                        parent="{ancestor::Term[1]/@name}"  context="{$context}" >
51                                                                <xsl:copy-of select="." />
52                        </Term>
53                </xsl:for-each>
54       
55</xsl:template>
56
57 -->
58 
59<xsl:function name="my:profilematrix" >
60    <xsl:param name="term"/>
61   
62    <xsl:variable name="profile" select="my:profile($term,true())" />
63    <Terms name="{$profile/name}"  id="{$profile/id}">
64        <xsl:for-each select="$profile//CMD_Component|$profile//CMD_Element" >
65                                        <xsl:variable name="context" select="my:context(.)" />                                         
66                                       
67                                        <xsl:variable name="id"  >
68                                                        <xsl:choose>
69                                                                <xsl:when test="@ComponentId">
70                                                                        <xsl:value-of select="@ComponentId" />
71                                                                </xsl:when>
72                                                                <xsl:otherwise>
73                                                                        <xsl:value-of select="ancestor::CMD_Component[@ComponentId][1]/@ComponentId" /><xsl:text>#</xsl:text>
74                                                                        <xsl:for-each select="ancestor::CMD_Component[not(descendant-or-self::CMD_Component[@ComponentId])]" >
75                                                                        <xsl:value-of select="@name" />.</xsl:for-each><xsl:value-of select="@name" />
76                                                                </xsl:otherwise>
77                                                        </xsl:choose>                                                   
78                                        </xsl:variable>
79                        <Term  name="{@name}" datcat="{@ConceptLink}" id="{$id}" 
80                                                        parent="{ancestor::CMD_Component[1]/@name}" context="{$context}" 
81                                                        >
82                                                                <!--  <xsl:copy-of select="." /> -->
83                        </Term>
84                </xsl:for-each>
85        </Terms>
86   
87</xsl:function>
88
89<xsl:function name="my:profile" >
90    <xsl:param name="term"/>
91    <xsl:param name="resolve" /> <!--  true|false-->
92   
93    <!-- <xsl:message>cmdprofiles_uri: <xsl:value-of select="$cmdprofiles_uri" /></xsl:message>  -->
94   
95    <xsl:variable name="profile" select="doc($cmdprofiles_uri)//profileDescription[name=$term]" />
96   
97    <xsl:choose>
98      <xsl:when test="$resolve=true()">
99                <xsl:apply-templates select="$profile" mode="include" />
100      </xsl:when>
101      <xsl:otherwise>
102                <xsl:copy-of select="$profile" />
103      </xsl:otherwise>
104    </xsl:choose>
105   
106       
107</xsl:function>
108
109<!--
110<xsl:function name="my:term2datcat">
111    <xsl:param name="term"/>                             
112    <xsl:value-of select="$term_matrix/*[@elem=$term]/@datcat" />                                       
113</xsl:function>
114-->
115
116        <xsl:function name="my:dict">
117     <xsl:param name="key"/>                             
118     <xsl:value-of select="my:dict($key, $key)" />                                     
119   </xsl:function>
120   
121        <xsl:function name="my:dict">
122     <xsl:param name="key"/>                           
123     <xsl:param name="fallback"/>     
124                <xsl:choose>
125                        <xsl:when test="$dict/list/item[@key=$key]" >
126                                <xsl:value-of select="$dict/list/item[@key=$key]" />
127                        </xsl:when>                     
128                        <xsl:when test="$dict/list/item[.=$key]" >
129                                <xsl:value-of select="$dict/list/item[.=$key]/@key" />
130                        </xsl:when>                     
131                        <xsl:otherwise>
132                        <xsl:value-of select="$fallback" />
133                        </xsl:otherwise>
134                </xsl:choose>
135   </xsl:function>
136               
137        <xsl:function name="my:extractID" >             
138                <xsl:param name="uri" />
139               
140                <xsl:choose>
141                        <xsl:when test="contains($uri,$isocat_base_uri)">
142                                <xsl:value-of select="substring-after($uri, $isocat_base_uri)" />
143                        </xsl:when>
144                        <xsl:when test="contains($uri,$components_id_prefix)">
145                        <!--   <xsl:value-of select="substring-after($uri,$components_id_prefix)" /> -->
146                                <xsl:value-of select="substring-after($uri, $components_id_prefix)" />
147                        </xsl:when>
148                        <xsl:otherwise><xsl:value-of select="$uri" /></xsl:otherwise>
149                </xsl:choose>           
150        </xsl:function>
151       
152       
153        <xsl:function name="my:shortURL" >
154                <xsl:param name="url" />
155                <!--   <xsl:value-of select="replace($url, 'http://www.isocat.org/datcat/','isocat:')" /> -->
156                 <xsl:variable name="matching_termset"  select="if($url!='') then $terms_setup/Termsets/Termset[@url_prefix][starts-with($url,@url_prefix)] else ()" />
157                 <!-- <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]
158                                                else if $terms_setup/Termsets/Termset[@url][starts-with($url,@url)] then $terms_setup/Termsets/Termset[@url][starts-with($url,@url)][1] else ()" /> --> 
159                 <!--<xsl:message>shortURL: <xsl:value-of select="$matching_termset/@url_prefix" />:: <xsl:value-of select="$url" />
160                </xsl:message> -->             
161                <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/@id,','),':')) else $url" />             
162        </xsl:function> 
163       
164        <!--  rewrite specific URLs (linking to target services) to redirect them locally (via MDService)
165        should work for: dcif:dataCategory@pid, pcompone
166        -->
167        <xsl:function name="my:rewriteURL" >
168                <xsl:param name="url" />       
169                <xsl:choose>
170                                <xsl:when test="contains($url,$isocat_base_uri)">
171                                                <xsl:value-of select="replace($url, $isocat_uri_match, $isocat_uri_replace)" />
172                                </xsl:when>     
173                                <xsl:otherwise><xsl:value-of select="concat($detail_complist_prefix, my:extractID($url))" /></xsl:otherwise>
174                        </xsl:choose>           
175        </xsl:function> 
176               
177               
178        <!-- shall be usable to form consistently all urls within xsl
179        started from:   
180        {$base_dir}/values/htmllist/?q={@path} -->
181        <xsl:function name="my:formURL" >
182                <xsl:param name="action" />
183                <xsl:param name="format" />
184                <xsl:param name="q" />
185               
186                <xsl:choose>
187                        <xsl:when test="$action=''">
188                                <xsl:value-of select="concat($base_dir, '/?q=', $q, '&amp;repository=', $repository_id)" />
189                        </xsl:when>                     
190                        <xsl:otherwise>
191                                <xsl:value-of select="concat($base_dir, '/',$action, '/', $format, '/?q=', $q, '&amp;repository=', $repository_id)" />
192                        </xsl:otherwise>
193                </xsl:choose>
194               
195        </xsl:function>
196       
197                <!--  previously known as comppath -->
198        <xsl:function name="my:context" >
199                <xsl:param name="child" />
200                <xsl:variable name="collect" >
201                                <xsl:for-each select="$child/ancestor::CMD_Component|$child/ancestor::Term" >
202                                                <xsl:value-of select="@name" />.</xsl:for-each><xsl:value-of select="$child/@name" />
203                </xsl:variable>
204                <xsl:value-of select="$collect" />     
205        </xsl:function> 
206
207        <xsl:function name="my:encodePID" >
208                <xsl:param name="pid" />                               
209                <xsl:value-of select="encode-for-uri(replace(replace($pid,'/','%2F'),'\.','%2E'))" />   
210        </xsl:function> 
211   
212<!--
213<xsl:function name="my:numberFormat">
214<xsl:param name="unit" />
215        <xsl:choose>
216                <xsl:when test="$unit='count' or $unit='count-distinct'" ><xsl:value-of select="$format_count" /></xsl:when>
217                <xsl:when test="$unit='kg'" ><xsl:value-of select="$format_kg" /></xsl:when>
218                <xsl:when test="$unit='t'" ><xsl:value-of select="$format_t" /></xsl:when>
219                <xsl:when test="$unit='m3'" ><xsl:value-of select="$format_m3" /></xsl:when>
220                <xsl:when test="$unit='per' or $unit='percent'" ><xsl:value-of select="$format_per" /></xsl:when>
221                <xsl:when test="$unit='sum'" ><xsl:value-of select="$format_kg" /></xsl:when>
222                <xsl:otherwise><xsl:value-of select="$format_default" /></xsl:otherwise>               
223        </xsl:choose>
224</xsl:function>
225-->
226
227<xsl:function name="my:index2xpath">
228<xsl:param name="p_ix" />
229
230        <xsl:variable name="ix_string" select="replace($p_ix,'_',' ')" />
231
232        <xsl:variable name="ix_resolved" >
233                <xsl:choose>
234                                <xsl:when test="contains($ix_string,':')">
235                                        <xsl:variable name="prefix" select="substring-before($ix_string,':')" />                               
236                                        <xsl:variable name="termset" select="$terms_setup/Termsets/Termset[@id=$prefix]" />                             
237                                       
238                                        <xsl:message>index2xpath.matching_termset:<xsl:copy-of select="$termset"/>
239                                                                        ix_string:<xsl:value-of select="$ix_string"/>
240                                        </xsl:message>
241                                        <xsl:choose>
242                                        <!-- magic happening here  -->
243                                                <xsl:when test="$termset[@type='dcr' or @type='rr']">
244                                                        <xsl:variable name="expanded_context" select="$terms_flat//context[@path=$ix_string]"></xsl:variable>
245                                                                <xsl:variable name="expanded_query" >
246                                                                        <xsl:text>(</xsl:text>
247                                                                        <xsl:for-each select="distinct-values($expanded_context//context[@elem]/@path)">
248                                                                                <xsl:variable name="prefix" select="substring-before(.,':')" />
249                                                                                <xsl:variable name="termset" select="$terms_setup/Termsets/Termset[@id=$prefix]" />                                                                     
250                                                                                <xsl:value-of select="concat($termset/@name,'//',substring-after(.,':'))" />
251                                                                                <xsl:if test="position()!=last()"><xsl:text>|</xsl:text></xsl:if>
252                                                                        </xsl:for-each>
253                                                                        <xsl:text>)</xsl:text>
254                                                                </xsl:variable>
255                                                                <xsl:message>expanded-context:<xsl:value-of select="$expanded_query" /></xsl:message>
256                                                        <xsl:value-of select="$expanded_query" />
257                                                </xsl:when>
258                                                <xsl:when test="not($termset/@name)"> 
259                                                        <!-- when no matching termset, simply  use the original prefix  -->
260                                                        <xsl:value-of select="concat($prefix,'//',substring-after($ix_string,':'))" />
261                                                </xsl:when>
262                                                <xsl:otherwise>
263                                                        <xsl:value-of select="concat($termset/@name,'//',substring-after($ix_string,':'))" />   
264                                                </xsl:otherwise>
265                                        </xsl:choose>
266                                         
267                                </xsl:when>
268                                <xsl:otherwise><xsl:value-of select="$ix_string" />
269                                </xsl:otherwise>
270                        </xsl:choose>
271                </xsl:variable>
272        <xsl:value-of select="translate($ix_resolved, '.', '/')" ></xsl:value-of>       
273</xsl:function>
274
275<!--  revert from xpath to cmdIndex
276        not inverted to my:index2xpath (yet):
277        only handles simple xpaths (not union expansions)
278  -->
279<xsl:function name="my:xpath2index">
280<xsl:param name="p_xpath" />
281
282        <xsl:variable name="ix_resolved" >
283                <!-- get rid off leading slashes -->
284                <xsl:variable name="stripped" select="if (starts-with($p_xpath,'//')) then substring-after($p_xpath, '//') else $p_xpath"  />
285                <xsl:choose>
286                                <xsl:when test="contains($stripped,'//')">
287                                        <xsl:variable name="prefix" select="substring-before($stripped,'//')" />                               
288                                        <xsl:variable name="termset" select="$terms_setup/Termsets/Termset[@name=$prefix]" />                           
289                                       
290                                        <xsl:message>xpath2index.matching_termset:<xsl:copy-of select="$termset/@id"/>                                                                 
291                                        </xsl:message>
292                                        <xsl:choose>
293                                                <!-- cannot really happen:
294                                                        <xsl:when test="$termset[@type='dcr' or @type='rr']">
295                                                        -->
296                                                <xsl:when test="not($termset/@id)"> 
297                                                        <!-- when no matching termset, simply  use the original prefix  -->
298                                                        <xsl:value-of select="concat($prefix,':',substring-after($stripped,'//'))" />
299                                                </xsl:when>
300                                                <xsl:otherwise>
301                                                        <xsl:value-of select="concat($termset/@id,':',substring-after($stripped,'//'))" />     
302                                                </xsl:otherwise>
303                                        </xsl:choose>                                   
304                                </xsl:when>
305                                <xsl:otherwise><xsl:value-of select="$stripped" />
306                                </xsl:otherwise>
307                        </xsl:choose>
308                </xsl:variable>
309        <xsl:value-of select="translate($ix_resolved, '/', '.')" ></xsl:value-of>       
310</xsl:function>
311
312
313</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.