source: MDService2/branches/MDService_simple3/src/xsl/cmd_functions.xsl @ 1637

Last change on this file since 1637 was 1637, checked in by gaba, 13 years ago

$repository_name removed

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