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

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

$repository_name removed

File size: 16.8 KB
Line 
1<?xml version="1.0" encoding="utf-8"?>
2<xsl:stylesheet
3  version="2.0"
4  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5  xmlns:xs="http://www.w3.org/2001/XMLSchema"
6  xmlns:my="myFunctions"
7>
8<!--
9<purpose> generate a termMatrix for a model summary (queryModel)</purpose>
10<strategy>match/merge model and CMD-profiles/components</strategy>
11<params>
12<param name=""></param>
13</params>
14<history>
15        <change on="2010-10-09" type="created" by="vr"></change>
16</history>
17-->
18<xsl:import href="cmd_commons.xsl"/>
19
20<xsl:output method="xml" indent="yes" > </xsl:output>
21
22<xsl:template name="continue-root">
23        <xsl:apply-templates mode="matrix"  />
24</xsl:template>
25<xsl:param name="title" select="'Model matrix'"></xsl:param>
26
27
28<!--<xsl:variable name="termsets-model" select="" />-->
29<!--<xsl:variable name="termsets-datcat" select="" />-->
30
31
32<xsl:template match="Termset|Terms" mode="matrix" >
33
34        <!--  hack for bug: missing value in: Terms/Term/@name -->
35        <xsl:variable name="termset_name" select="if (not(Term/@name='')) then Term/@name else replace(Term/@path,'/','')"></xsl:variable>
36
37        <xsl:message>processing model-term: <xsl:value-of select="$termset_name" /> </xsl:message>
38       
39        <xsl:choose>
40        <xsl:when test="$termset_name='all'" >
41                <xsl:call-template name="full-matrix" />               
42        </xsl:when>     
43        <xsl:when test="$termset_name='Components'" >
44                <xsl:message>model2matrix.Components.repo_name: <xsl:value-of select="$repository" /></xsl:message>
45                <!--  get the whole Termset for every root-node (teiHeader, TCP, ...) -->
46                <Termsets>
47                <xsl:for-each select="Term/Term" >
48                        <xsl:variable name="termset_name" select="@name" />                                             
49                       
50                        <xsl:variable name="termset_matrix" >
51                                <xsl:choose>
52                                        <!-- if existing children, process directly those (enrich them in a separate matrix-run  -->
53                                        <xsl:when test="Term">
54                                                <xsl:variable name="terms" >                                                   
55                                                        <Termset>
56                                                                <xsl:copy-of select="." />
57                                                        </Termset>     
58                                                </xsl:variable>                                         
59                                               
60                                                <xsl:apply-templates select="$terms" mode="matrix" />                                           
61                                        </xsl:when>
62                                        <!--  else get the full summary for given termset -->
63                                        <xsl:otherwise>
64                                        <xsl:variable name="resolved_termset_uri" select="concat($model_matrix_uri,$termset_name,'&amp;maxdepth=8&amp;repository=',$repository)" />
65                                                <xsl:message>processing model-term($resolved_termset_uri): <xsl:value-of select="$resolved_termset_uri" /> </xsl:message>                       
66                                                <xsl:copy-of select="document($resolved_termset_uri)" /> 
67                                        </xsl:otherwise>
68                                </xsl:choose>
69                        </xsl:variable>
70                       
71                        <!--<xsl:message><xsl:copy-of select="$termset" ></xsl:copy-of></xsl:message> --> 
72
73<!-- this should be obsolete..
74                        <xsl:variable name="termset_matrix" >
75                                <xsl:for-each select="$termset" >
76                                        <xsl:apply-templates mode="matrix"  />
77                                </xsl:for-each>                         
78                        </xsl:variable>
79         -->                                   
80                        <Termset>
81                                <!-- <xsl:copy-of select="$terms_setup//Termset[@name=$termset_name]/@*"  />  -->
82                                <xsl:copy-of select="$termset_matrix/Termset/@*" />
83
84                                <xsl:copy-of select="$termset_matrix/Termset/*" />                     
85                        </Termset>                     
86                </xsl:for-each>
87                </Termsets>
88        </xsl:when>
89        <xsl:otherwise>
90                <xsl:variable name="termset_name" select="Term/@name" />
91                <xsl:variable name="profile" select="my:profilematrix($termset_name)" />
92                <xsl:message>corresponding profile: <xsl:value-of select="concat($profile/@name, '#', $profile/@id)"/>  </xsl:message>
93                <!--  <xsl:copy-of select="$profile"  /> -->
94
95                <xsl:variable name="setup_entry" select="$terms_setup//Termset[@name=$termset_name]" />
96                <xsl:message>corresponding terms_setup-entry: <xsl:value-of select="$setup_entry/concat(@name, '#', @id)"/>  </xsl:message>
97
98                <xsl:variable name="prematrix">
99                        <Terms name="{$termset_name}">
100                                <xsl:copy-of select="@*"  />
101                                <xsl:copy-of select="$setup_entry/@*"  />
102                                <xsl:apply-templates mode="matrix"  >
103                                        <xsl:with-param name="profile" select="$profile"></xsl:with-param>
104                                </xsl:apply-templates>                         
105                        </Terms>
106                </xsl:variable>
107               
108                <xsl:variable name="matrix">
109                        <Termset>
110                                <xsl:copy-of select="$prematrix/Terms/@*" />
111                        <xsl:for-each select="$prematrix/Terms/Term">
112                       
113                                <xsl:apply-templates select="." mode="min-context" >
114                                        <xsl:with-param name="matrix" select="$prematrix"></xsl:with-param>
115                                </xsl:apply-templates>
116                                        <!--   
117                                <xsl:copy>
118                                <xsl:copy-of select="@*"></xsl:copy-of>
119                                        <xsl:attribute name="path" select="$path" />
120                                </xsl:copy>
121                                -->
122                        </xsl:for-each> 
123                        </Termset>
124                </xsl:variable>
125               
126                <xsl:copy-of select="$matrix" />       
127               
128        </xsl:otherwise>       
129        </xsl:choose>
130       
131
132</xsl:template>
133
134<xsl:template name="full-matrix" >
135<!--  collect all model termMatrix -->
136                <xsl:variable name="model_termsets_uri" select="concat($model_matrix_uri,'Components', '&amp;maxdepth=1&amp;repository=',$repository)" />
137        <xsl:message>full-matrix.model_termsets_uri: <xsl:value-of select="$model_termsets_uri"/>  </xsl:message>                               
138                <xsl:variable name="model_termsets" select="document($model_termsets_uri)" />
139
140
141                <!--  collect all datcat terms -->
142                <xsl:variable name="datcats_uri" select="$datcats_terms_uri" /> 
143                <xsl:variable name="datcat_termsets" select="document($datcats_uri)" />
144
145                <!--  resolve/invert datcat->Terms -->         
146                <xsl:variable name="datcat_termsets_resolved" >
147                                <xsl:apply-templates select="$datcat_termsets//Termset[@type='dcr']" mode="resolve-datcat" >
148                                        <xsl:with-param name="model_termsets" select="$model_termsets" />
149                                </xsl:apply-templates> 
150                </xsl:variable>
151               
152                <!-- +  resolve RR: rels(datcat,datcat) -->             
153                <xsl:variable name="relcat_termsets_resolved" >
154                                <xsl:apply-templates select="$datcat_termsets//Termset[@type='rr']" mode="resolve-relcat" >
155                                        <xsl:with-param name="datcat_termsets" select="$datcat_termsets_resolved" />
156                                </xsl:apply-templates> 
157                </xsl:variable>
158                                               
159        <Termsets>
160                <xsl:copy-of select="$model_termsets/Termsets/*" />
161                <xsl:copy-of select="$datcat_termsets_resolved/*" />           
162                <xsl:copy-of select="$relcat_termsets_resolved/*" />
163        </Termsets>
164       
165               
166</xsl:template>
167
168<xsl:template match="Termsets|Termset" mode="resolve-datcat" >
169        <xsl:param name="model_termsets" />
170        <xsl:copy>
171                <xsl:copy-of select="@*"></xsl:copy-of>
172                <xsl:apply-templates mode="resolve-datcat">
173                                <xsl:with-param name="model_termsets" select="$model_termsets" />
174                </xsl:apply-templates>
175                       
176        </xsl:copy>
177</xsl:template>
178
179<xsl:template match="Term" mode="resolve-datcat" >
180        <xsl:param name="model_termsets" />
181        <xsl:copy>
182                <xsl:attribute name="path" select="concat(ancestor::Termset/@id,':',@name)"></xsl:attribute>
183                <xsl:copy-of select="*|@*"></xsl:copy-of>
184                <xsl:variable name="matching_model_terms" select="$model_termsets//Term[@datcat=current()/@datcat]" />
185<!--            <xsl:attribute name="name" select="$matching_model_terms/"></xsl:attribute>  -->
186                <xsl:copy-of select="$matching_model_terms" />
187        </xsl:copy>
188</xsl:template>
189
190<xsl:template match="Termsets|Termset" mode="resolve-relcat" >
191        <xsl:param name="datcat_termsets" />
192        <xsl:copy>
193                <xsl:copy-of select="@*"></xsl:copy-of>
194                <xsl:apply-templates mode="resolve-relcat">
195                                <xsl:with-param name="datcat_termsets" select="$datcat_termsets" />
196                </xsl:apply-templates>                 
197        </xsl:copy>
198</xsl:template>
199               
200<xsl:template match="Term[@type='rel']" mode="resolve-relcat" >
201        <xsl:param name="datcat_termsets" />
202        <xsl:copy>
203                <xsl:variable name="matching_datcat_terms" select="$datcat_termsets//Term[@type='datcat'][@datcat=current()/@datcat]" />
204                <!-- <xsl:variable name="matching_datcat_relterms" select="$datcat_termsets//Term[@datcat=current()/@datcat]" /> -->
205                <xsl:variable name="name" select="($matching_datcat_terms//@name)[1]"></xsl:variable>
206                <xsl:attribute name="name" select="$name"></xsl:attribute>
207                <xsl:attribute name="path" select="concat(ancestor::Termset/@id,':',$name)"></xsl:attribute>
208                <xsl:copy-of select="@*"></xsl:copy-of>         
209                <xsl:copy-of select="$matching_datcat_terms" />
210                <xsl:apply-templates select="Term[@type='rel']" mode="resolve-relcat">
211                        <xsl:with-param name="datcat_termsets" select="$datcat_termsets"/>
212                </xsl:apply-templates>         
213        </xsl:copy>
214</xsl:template>
215
216<!--
217<xsl:template match="@*|*" mode="resolve-datcat" >      
218        <xsl:copy-of select="." />
219</xsl:template>
220 -->
221
222<!--  computing minimal unique path/index -->
223<xsl:template match="Term" mode="min-context" >
224        <xsl:param name="matrix" />
225        <xsl:param name="term" select="." />
226       
227       
228        <xsl:variable name="termset_id" select="if (exists(ancestor::Terms/@id)) then ancestor::Terms/@id else ancestor::Terms/@name" />
229       
230        <xsl:variable name="ambi_terms" select="$matrix//Term[lower-case(@name)=lower-case($term/@name)]" />
231                       
232        <xsl:variable name="path" >                     
233                        <!-- don't apply prefix for the root element (=profile-name) -->               
234                        <xsl:if test="not($term/parent::Terms)" >
235                                <xsl:value-of select="$termset_id" ></xsl:value-of><xsl:text>:</xsl:text>
236                        </xsl:if> 
237                       
238                        <xsl:choose>           
239                                <xsl:when test="count($ambi_terms) &gt; 1" >
240                                                <xsl:variable name="ambi_terms_parent" select="$ambi_terms[@name=$term/@name]" />
241                                                        <xsl:variable name="term1" select="." />
242                                                        <xsl:message>Term:<xsl:value-of select="$term1/@context" />|ambi: <xsl:value-of select="count($ambi_terms)" />|</xsl:message>
243                                                        <xsl:variable name="minimal_contexts" >
244                                                                <xsl:for-each select="$ambi_terms[not(@context=$term1/@context)]" > 
245                <!--                                            [position() &gt; current()/position()]" >                       -->
246                                                                        <item>                                                                 
247                                                                                <xsl:variable name="diff"  select="my:diffContext($term1,.)" />
248                                                                                <!-- <xsl:message>diff: <xsl:value-of select="count($diff)" /></xsl:message>-->
249                                                                                <xsl:copy-of select="$diff" />                                                                   
250                                                                                                                                                                                                                       
251                                                                        </item>                                                                 
252                                                                </xsl:for-each>
253                                                        </xsl:variable>
254<!-- DEBUG:                             diffcontext1:<xsl:value-of select="count($minimal_contexts)" />
255|direct: <xsl:value-of select="count($minimal_contexts[1]/*)" />: <xsl:value-of select="$minimal_contexts[1]/*" />|
256|item1: <xsl:value-of select="count($minimal_contexts/item[1]/*)" />: <xsl:value-of select="$minimal_contexts/item[1]/*" />|
257                                                 cnt_mci:<xsl:value-of select="$minimal_contexts/*/count(tokenize(.,' '))" ></xsl:value-of>/
258                                                         max-strlen:<xsl:value-of select="max($minimal_contexts/*/string-length(.))" ></xsl:value-of>
259                                                         -->                                                     
260                                                <xsl:variable name="last_contexts" select="distinct-values($minimal_contexts/*/tokenize(.,' ')[last()])" ></xsl:variable>
261                                                <!--  to get the order right -->
262                                                <xsl:for-each select="$last_contexts" >
263                                                        <xsl:sort select="index-of(tokenize($term1/@context,'\.'),.)[1]" /> 
264                                                                <xsl:value-of select="." />.</xsl:for-each>
265                        <!-- DEBUG:     cnt_lc:<xsl:value-of select="count($last_contexts)" />}
266                                                cnt_tok:<xsl:value-of select="count(tokenize($term1/@context,'\.'))" />
267                                                 
268                                                <xsl:for-each select="tokenize($term1/@context,'\.')" >
269                                                                tokenizing-context:<xsl:value-of select="." />,<xsl:value-of select="current()" />|
270                                                        <xsl:if test="$last_contexts[.=current()]" >
271                                                                        {<xsl:value-of select="." />}
272
273                                                        </xsl:if>
274                                                </xsl:for-each>
275                                                -->
276<!--                                                            <xsl:value-of select="string-join(distinct-values($minimal_contexts/*/tokenize(.,' ')[last()]),'.')" ></xsl:value-of>           -->                                             
277                                </xsl:when>
278                                <xsl:otherwise>                 
279                                </xsl:otherwise>
280                        </xsl:choose>
281                        <xsl:value-of select="@name" ></xsl:value-of>
282        </xsl:variable>
283       
284        <xsl:copy>
285                <xsl:copy-of select="@*"></xsl:copy-of>
286<!--            <xsl:copy-of select="$path" />-->
287                        <xsl:attribute name="path" select="$path" />
288               
289                <xsl:apply-templates select="Term" mode="min-context" >
290                                <xsl:with-param name="matrix" select="$matrix"></xsl:with-param>
291                </xsl:apply-templates>
292                       
293                <xsl:copy-of select="v"  />
294        </xsl:copy>                                                     
295       
296</xsl:template>
297
298<xsl:function name="my:diffContext" >
299        <xsl:param name="c1" />
300        <xsl:param name="c2" />
301         <!-- <xsl:message>C1:<xsl:copy-of select="count($c1/ancestor::Term/@name)" /></xsl:message>
302         <xsl:message><xsl:value-of select="$c1/ancestor::Term/@name" /></xsl:message>
303         <xsl:message>C2:<xsl:value-of select="$c2/ancestor::Term/@name" /></xsl:message>
304          -->
305<!--            <xsl:value-of select="($c1/ancestor::Term/@name except $c2/ancestor::Term/@name)[last()]" />-->
306
307        <!-- <xsl:value-of select="string-join($c1/ancestor::Term/@name except $c2/ancestor::Term/@name,'.')" /> -->
308        <!--   <xsl:copy-of select="$c1/ancestor::Term/@name except $c2/ancestor::Term/@name" />-->
309         <xsl:variable name="diff" select="$c1/ancestor::Term/xs:string(@name[not(.=$c2/ancestor::Term/@name)])" />
310         <!-- <xsl:message>diff: <xsl:value-of select="$diff" /></xsl:message>  -->
311           <xsl:copy-of select="$diff" />
312       
313</xsl:function>
314
315<!--  obsoleted?!  -->
316<xsl:function name="my:pick" >
317        <xsl:param name="partial_result" />
318        <xsl:param name="set" />
319
320                <xsl:choose>
321                        <xsl:when test="count($set/*) &gt; 1" >
322                        <xsl:message>count-rest:<xsl:value-of select="count($set/*[position()&gt; 1])" /></xsl:message>                                   
323<!--                                    <xsl:variable  name="pickone" select="$set/*[1]/tokenize(.,' ')[last()]" />
324                        <xsl:variable  name="rest"  select="my:pick($pickone  $set/*[position()&gt; 1]))" />
325                               
326                                        <xsl:message>rest:<xsl:value-of select="count($rest)" /></xsl:message>
327                                -->
328                                <!--  <xsl:choose>
329                                        <xsl:when test="count($intersect &gt; 0)" >                                     
330                                                        <xsl:copy-of select="$intersect" />                                                     
331                                        </xsl:when>
332                                        <xsl:otherwise>
333                                                <xsl:message>no-intersect:</xsl:message>
334                                                        <xsl:message><xsl:value-of select="count($set/*[1])" />/<xsl:value-of select="count($set/*[1])" /></xsl:message>
335                                                        <xsl:copy-of select="$set/*[1] union my:intersect($set/*[position()&gt; 1])" />
336                                        </xsl:otherwise>
337                                </xsl:choose>
338                                 -->                           
339                        </xsl:when>
340                        <xsl:when test="count($set) = 1" >
341                                <xsl:copy-of select="$set[1]/*" />             
342                                <xsl:message>setitem1:<xsl:copy-of select="$set[1]/*" /></xsl:message>
343                        </xsl:when>
344                        <xsl:otherwise>                         
345                        </xsl:otherwise>
346                </xsl:choose>
347
348       
349</xsl:function> 
350
351<xsl:function name="my:intersect" >     
352        <xsl:param name="set" />
353                <xsl:message>set-size:<xsl:value-of select="count($set/*)" /></xsl:message>
354                <xsl:choose>
355                        <xsl:when test="count($set/*) &gt; 1" >
356                        <xsl:message>count-rest:<xsl:value-of select="count($set/*[position()&gt; 1])" /></xsl:message>
357                                        <xsl:variable  name="rest"  select="my:intersect($set/*[position()&gt; 1])" />
358                                        <xsl:message>rest:<xsl:value-of select="count($rest)" /></xsl:message> 
359                                <xsl:variable  name="intersect" select="$set/*[1][.=$rest/*]" />
360                               
361                                <xsl:message>intersect:<xsl:value-of select="count($intersect)" /></xsl:message>
362                                <xsl:choose>
363                                        <xsl:when test="count($intersect &gt; 0)" >                                     
364                                                        <xsl:copy-of select="$intersect" />                                                     
365                                        </xsl:when>
366                                        <xsl:otherwise>
367                                                <xsl:message>no-intersect:</xsl:message>
368                                                        <xsl:message><xsl:value-of select="count($set/*[1])" />/<xsl:value-of select="count($set/*[1])" /></xsl:message>
369                                                        <xsl:copy-of select="$set/*[1] union my:intersect($set/*[position()&gt; 1])" />
370                                        </xsl:otherwise>
371                                </xsl:choose>                           
372                        </xsl:when>
373                        <xsl:when test="count($set) = 1" >
374                                <xsl:copy-of select="$set[1]/*" />             
375                                <xsl:message>setitem1:<xsl:copy-of select="$set[1]/*" /></xsl:message>
376                        </xsl:when>
377                        <xsl:otherwise>                         
378                        </xsl:otherwise>
379                </xsl:choose>
380</xsl:function>
381
382<!--
383<xsl:function name="my:intersect" >
384        <xsl:param name="set" />
385       
386       
387                        <xsl:value-of select="my:intersect(terms1 intersect *" />
388
389        <xsl:for-each select="$set/item" >
390                <xsl:variable name="terms1" select="*" />                       
391                <xsl:for-each select="$set/item" >
392                        <xsl:value-of select="my:intersect_pairwise(terms1,terms2) intersect *" />
393                       
394                </xsl:for-each>
395        </xsl:for-each>
396       
397</xsl:function>
398-->
399
400<xsl:template match="Term" mode="matrix" >
401        <xsl:param name="profile" />
402
403                        <xsl:variable name="context" select="my:context(.)" >
404                                                        <!--  <xsl:for-each select="ancestor::CMD_Component" >
405                                                                <xsl:value-of select="@name" />.</xsl:for-each>-->
406                                        </xsl:variable> 
407                       
408                        <xsl:variable name="corresp_cmdterm" select="if (parent::Terms) then $profile else $profile//*[@context=$context]" />                                   
409                        <!--
410                        <xsl:message>context: <xsl:copy-of select="$context" /> </xsl:message> 
411                        <xsl:message>corresp_cmdterm: <xsl:copy-of select="$profile/Term[@context=$context]" /> </xsl:message>--> 
412                        <xsl:variable name="datcat" select="$corresp_cmdterm/@datcat" >
413                                        <!--  <xsl:for-each select="ancestor::CMD_Component" >
414                                                <xsl:value-of select="@name" />.</xsl:for-each>-->
415                        </xsl:variable>
416                        <Term  elem="{@name}" datcat="{$datcat}" corresponding_component="{$corresp_cmdterm/@id}" 
417                                                        parent="{ancestor::Term[1]/@name}"  context="{$context}" >
418                                                        <xsl:copy-of select="@*[not(name()='path')]" /> 
419                                <xsl:apply-templates mode="matrix" >
420                                        <xsl:with-param name="profile" select="$profile" />
421                                </xsl:apply-templates>
422                               
423                        </Term>
424                       
425                       
426       
427</xsl:template>
428
429</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.