source: MDService2/trunk/MDService2/src/xsl/model2matrix.xsl @ 1029

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

changes to columns, terms (unfinished) ; .ui-context-dialog

File size: 16.5 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="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>processing model-term: 1  <xsl:value-of select="$repository_uri" /></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" >
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                                                        <Terms>
56                                                                <xsl:copy-of select="." />
57                                                        </Terms>       
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_uri)" />
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                        <xsl:variable name="termset_matrix" >
74                                <xsl:for-each select="$termset" >
75                                        <xsl:apply-templates mode="matrix"  />
76                                </xsl:for-each>                         
77                        </xsl:variable>
78                                               
79                        <Termset>
80                                <!-- <xsl:copy-of select="$terms_setup//Termset[@name=$termset_name]/@*"  />  -->
81                                <xsl:copy-of select="$termset_matrix/Terms/@*" />
82
83                                <xsl:copy-of select="$termset_matrix/Terms/*" />                       
84                        </Termset>                     
85                </xsl:for-each>
86                </Termsets>
87        </xsl:when>
88        <xsl:otherwise>
89                <xsl:variable name="termset_name" select="Term/@name" />
90                <xsl:variable name="profile" select="my:profilematrix($termset_name)" />
91                <xsl:message>corresponding profile: <xsl:value-of select="concat($profile/@name, '#', $profile/@id)"/>  </xsl:message>
92                <!--  <xsl:copy-of select="$profile"  /> -->
93
94                <xsl:variable name="setup_entry" select="$terms_setup//Termset[@name=$termset_name]" />
95                <xsl:message>corresponding terms_setup-entry: <xsl:value-of select="$setup_entry/concat(@name, '#', @id)"/>  </xsl:message>
96
97                <xsl:variable name="prematrix">
98                        <Terms name="{$termset_name}">
99                                <xsl:copy-of select="@*"  />
100                                <xsl:copy-of select="$setup_entry/@*"  />
101                                <xsl:apply-templates mode="matrix"  >
102                                        <xsl:with-param name="profile" select="$profile"></xsl:with-param>
103                                </xsl:apply-templates>                         
104                        </Terms>
105                </xsl:variable>
106               
107                <xsl:variable name="matrix">
108                        <Terms>
109                                <xsl:copy-of select="$prematrix/Terms/@*" />
110                        <xsl:for-each select="$prematrix/Terms/Term">
111                       
112                                <xsl:apply-templates select="." mode="min-context" >
113                                        <xsl:with-param name="matrix" select="$prematrix"></xsl:with-param>
114                                </xsl:apply-templates>
115                                        <!--   
116                                <xsl:copy>
117                                <xsl:copy-of select="@*"></xsl:copy-of>
118                                        <xsl:attribute name="path" select="$path" />
119                                </xsl:copy>
120                                -->
121                        </xsl:for-each> 
122                        </Terms>
123                </xsl:variable>
124               
125                <xsl:copy-of select="$matrix" />       
126               
127        </xsl:otherwise>       
128        </xsl:choose>
129       
130
131</xsl:template>
132
133<xsl:template name="full-matrix" >
134<!--  collect all model termMatrix -->
135                <xsl:variable name="model_termsets_uri" select="concat($model_matrix_uri,'Components', '&amp;maxdepth=1&amp;repository=',$repository_uri)" />
136        <xsl:message>full-matrix.model_termsets_uri: <xsl:value-of select="$model_termsets_uri"/>  </xsl:message>                               
137                <xsl:variable name="model_termsets" select="document($model_termsets_uri)" />
138
139
140                <!--  collect all datcat terms -->
141                <xsl:variable name="datcats_uri" select="$datcats_terms_uri" /> 
142                <xsl:variable name="datcat_termsets" select="document($datcats_uri)" />
143
144                <!--  resolve/invert datcat->Terms -->         
145                <xsl:variable name="datcat_termsets_resolved" >
146                                <xsl:apply-templates select="$datcat_termsets//Termset[@type='dcr']" mode="resolve-datcat" >
147                                        <xsl:with-param name="model_termsets" select="$model_termsets" />
148                                </xsl:apply-templates> 
149                </xsl:variable>
150               
151                <!-- +  resolve RR: rels(datcat,datcat) -->             
152                <xsl:variable name="relcat_termsets_resolved" >
153                                <xsl:apply-templates select="$datcat_termsets//Termset[@type='rr']" mode="resolve-relcat" >
154                                        <xsl:with-param name="datcat_termsets" select="$datcat_termsets_resolved" />
155                                </xsl:apply-templates> 
156                </xsl:variable>
157                                               
158        <Termsets>
159                <xsl:copy-of select="$model_termsets/Termsets/*" />
160                <xsl:copy-of select="$datcat_termsets_resolved/*" />           
161                <xsl:copy-of select="$relcat_termsets_resolved/*" />
162        </Termsets>
163       
164               
165</xsl:template>
166
167<xsl:template match="Termsets|Termset" mode="resolve-datcat" >
168        <xsl:param name="model_termsets" />
169        <xsl:copy>
170                <xsl:copy-of select="@*"></xsl:copy-of>
171                <xsl:apply-templates mode="resolve-datcat">
172                                <xsl:with-param name="model_termsets" select="$model_termsets" />
173                </xsl:apply-templates>
174                       
175        </xsl:copy>
176</xsl:template>
177
178<xsl:template match="Term" mode="resolve-datcat" >
179        <xsl:param name="model_termsets" />
180        <xsl:copy>
181                <xsl:attribute name="path" select="concat(ancestor::Termset/@id,':',@name)"></xsl:attribute>
182                <xsl:copy-of select="*|@*"></xsl:copy-of>
183                <xsl:variable name="matching_model_terms" select="$model_termsets//Term[@datcat=current()/@datcat]" />
184<!--            <xsl:attribute name="name" select="$matching_model_terms/"></xsl:attribute>  -->
185                <xsl:copy-of select="$matching_model_terms" />
186        </xsl:copy>
187</xsl:template>
188
189<xsl:template match="Termsets|Termset" mode="resolve-relcat" >
190        <xsl:param name="datcat_termsets" />
191        <xsl:copy>
192                <xsl:copy-of select="@*"></xsl:copy-of>
193                <xsl:apply-templates mode="resolve-relcat">
194                                <xsl:with-param name="datcat_termsets" select="$datcat_termsets" />
195                </xsl:apply-templates>                 
196        </xsl:copy>
197</xsl:template>
198               
199<xsl:template match="Term[@type='rel']" mode="resolve-relcat" >
200        <xsl:param name="datcat_termsets" />
201        <xsl:copy>
202                <xsl:variable name="matching_datcat_terms" select="$datcat_termsets//Term[@datcat=current()/@datcat]" />
203                <!-- <xsl:variable name="matching_datcat_relterms" select="$datcat_termsets//Term[@datcat=current()/@datcat]" /> -->
204                <xsl:variable name="name" select="($matching_datcat_terms//@name)[1]"></xsl:variable>
205                <xsl:attribute name="name" select="$name"></xsl:attribute>
206                <xsl:attribute name="path" select="concat(ancestor::Termset/@id,':',$name)"></xsl:attribute>
207                <xsl:copy-of select="@*"></xsl:copy-of>         
208                <xsl:copy-of select="$matching_datcat_terms" />
209                <xsl:apply-templates select="Term" mode="resolve-relcat">
210                        <xsl:with-param name="datcat_termsets" select="$datcat_termsets"/>
211                </xsl:apply-templates>         
212        </xsl:copy>
213</xsl:template>
214
215<!--
216<xsl:template match="@*|*" mode="resolve-datcat" >      
217        <xsl:copy-of select="." />
218</xsl:template>
219 -->
220
221<xsl:template match="Term" mode="min-context" >
222        <xsl:param name="matrix" />
223        <xsl:param name="term" select="." />
224       
225       
226        <xsl:variable name="termset_id" select="if (exists(ancestor::Terms/@id)) then ancestor::Terms/@id else ancestor::Terms/@name" />
227       
228        <xsl:variable name="ambi_terms" select="$matrix//Term[lower-case(@name)=lower-case($term/@name)]" />
229                       
230        <xsl:variable name="path" >             
231                        <xsl:value-of select="$termset_id" ></xsl:value-of><xsl:text>:</xsl:text> 
232                       
233                        <xsl:choose>           
234                                <xsl:when test="count($ambi_terms) &gt; 1" >
235                                                <xsl:variable name="ambi_terms_parent" select="$ambi_terms[@name=$term/@name]" />
236                                                        <xsl:variable name="term1" select="." />
237                                                        <xsl:message>Term:<xsl:value-of select="$term1/@context" />|ambi: <xsl:value-of select="count($ambi_terms)" />|</xsl:message>
238                                                        <xsl:variable name="minimal_contexts" >
239                                                                <xsl:for-each select="$ambi_terms[not(@context=$term1/@context)]" > 
240                <!--                                            [position() &gt; current()/position()]" >                       -->
241                                                                        <item>                                                                 
242                                                                                <xsl:variable name="diff"  select="my:diffContext($term1,.)" />
243                                                                                <!-- <xsl:message>diff: <xsl:value-of select="count($diff)" /></xsl:message>-->
244                                                                                <xsl:copy-of select="$diff" />                                                                   
245                                                                                                                                                                                                                       
246                                                                        </item>                                                                 
247                                                                </xsl:for-each>
248                                                        </xsl:variable>
249<!-- DEBUG:                             diffcontext1:<xsl:value-of select="count($minimal_contexts)" />
250|direct: <xsl:value-of select="count($minimal_contexts[1]/*)" />: <xsl:value-of select="$minimal_contexts[1]/*" />|
251|item1: <xsl:value-of select="count($minimal_contexts/item[1]/*)" />: <xsl:value-of select="$minimal_contexts/item[1]/*" />|
252                                                 cnt_mci:<xsl:value-of select="$minimal_contexts/*/count(tokenize(.,' '))" ></xsl:value-of>/
253                                                         max-strlen:<xsl:value-of select="max($minimal_contexts/*/string-length(.))" ></xsl:value-of>
254                                                         -->                                                     
255                                                <xsl:variable name="last_contexts" select="distinct-values($minimal_contexts/*/tokenize(.,' ')[last()])" ></xsl:variable>
256                                                <!--  to get the order right -->
257                                                <xsl:for-each select="$last_contexts" >
258                                                        <xsl:sort select="index-of(tokenize($term1/@context,'\.'),.)[1]" /> 
259                                                                <xsl:value-of select="." />.</xsl:for-each>
260                        <!-- DEBUG:     cnt_lc:<xsl:value-of select="count($last_contexts)" />}
261                                                cnt_tok:<xsl:value-of select="count(tokenize($term1/@context,'\.'))" />
262                                                 
263                                                <xsl:for-each select="tokenize($term1/@context,'\.')" >
264                                                                tokenizing-context:<xsl:value-of select="." />,<xsl:value-of select="current()" />|
265                                                        <xsl:if test="$last_contexts[.=current()]" >
266                                                                        {<xsl:value-of select="." />}
267
268                                                        </xsl:if>
269                                                </xsl:for-each>
270                                                -->
271<!--                                                            <xsl:value-of select="string-join(distinct-values($minimal_contexts/*/tokenize(.,' ')[last()]),'.')" ></xsl:value-of>           -->                                             
272                                </xsl:when>
273                                <xsl:otherwise>                 
274                                </xsl:otherwise>
275                        </xsl:choose>
276                        <xsl:value-of select="@name" ></xsl:value-of>
277        </xsl:variable>
278       
279        <xsl:copy>
280                <xsl:copy-of select="@*"></xsl:copy-of>
281<!--            <xsl:copy-of select="$path" />-->
282                        <xsl:attribute name="path" select="$path" />
283               
284                <xsl:apply-templates select="Term" mode="min-context" >
285                                <xsl:with-param name="matrix" select="$matrix"></xsl:with-param>
286                </xsl:apply-templates>
287                       
288                <xsl:copy-of select="v"  />
289        </xsl:copy>                                                     
290       
291</xsl:template>
292
293<xsl:function name="my:diffContext" >
294        <xsl:param name="c1" />
295        <xsl:param name="c2" />
296         <!-- <xsl:message>C1:<xsl:copy-of select="count($c1/ancestor::Term/@name)" /></xsl:message>
297         <xsl:message><xsl:value-of select="$c1/ancestor::Term/@name" /></xsl:message>
298         <xsl:message>C2:<xsl:value-of select="$c2/ancestor::Term/@name" /></xsl:message>
299          -->
300<!--            <xsl:value-of select="($c1/ancestor::Term/@name except $c2/ancestor::Term/@name)[last()]" />-->
301
302        <!-- <xsl:value-of select="string-join($c1/ancestor::Term/@name except $c2/ancestor::Term/@name,'.')" /> -->
303        <!--   <xsl:copy-of select="$c1/ancestor::Term/@name except $c2/ancestor::Term/@name" />-->
304         <xsl:variable name="diff" select="$c1/ancestor::Term/xs:string(@name[not(.=$c2/ancestor::Term/@name)])" />
305         <!-- <xsl:message>diff: <xsl:value-of select="$diff" /></xsl:message>  -->
306           <xsl:copy-of select="$diff" />
307       
308</xsl:function>
309
310<!--  obsoleted?!  -->
311<xsl:function name="my:pick" >
312        <xsl:param name="partial_result" />
313        <xsl:param name="set" />
314
315                <xsl:choose>
316                        <xsl:when test="count($set/*) &gt; 1" >
317                        <xsl:message>count-rest:<xsl:value-of select="count($set/*[position()&gt; 1])" /></xsl:message>                                   
318<!--                                    <xsl:variable  name="pickone" select="$set/*[1]/tokenize(.,' ')[last()]" />
319                        <xsl:variable  name="rest"  select="my:pick($pickone  $set/*[position()&gt; 1]))" />
320                               
321                                        <xsl:message>rest:<xsl:value-of select="count($rest)" /></xsl:message>
322                                -->
323                                <!--  <xsl:choose>
324                                        <xsl:when test="count($intersect &gt; 0)" >                                     
325                                                        <xsl:copy-of select="$intersect" />                                                     
326                                        </xsl:when>
327                                        <xsl:otherwise>
328                                                <xsl:message>no-intersect:</xsl:message>
329                                                        <xsl:message><xsl:value-of select="count($set/*[1])" />/<xsl:value-of select="count($set/*[1])" /></xsl:message>
330                                                        <xsl:copy-of select="$set/*[1] union my:intersect($set/*[position()&gt; 1])" />
331                                        </xsl:otherwise>
332                                </xsl:choose>
333                                 -->                           
334                        </xsl:when>
335                        <xsl:when test="count($set) = 1" >
336                                <xsl:copy-of select="$set[1]/*" />             
337                                <xsl:message>setitem1:<xsl:copy-of select="$set[1]/*" /></xsl:message>
338                        </xsl:when>
339                        <xsl:otherwise>                         
340                        </xsl:otherwise>
341                </xsl:choose>
342
343       
344</xsl:function> 
345
346<xsl:function name="my:intersect" >     
347        <xsl:param name="set" />
348                <xsl:message>set-size:<xsl:value-of select="count($set/*)" /></xsl:message>
349                <xsl:choose>
350                        <xsl:when test="count($set/*) &gt; 1" >
351                        <xsl:message>count-rest:<xsl:value-of select="count($set/*[position()&gt; 1])" /></xsl:message>
352                                        <xsl:variable  name="rest"  select="my:intersect($set/*[position()&gt; 1])" />
353                                        <xsl:message>rest:<xsl:value-of select="count($rest)" /></xsl:message> 
354                                <xsl:variable  name="intersect" select="$set/*[1][.=$rest/*]" />
355                               
356                                <xsl:message>intersect:<xsl:value-of select="count($intersect)" /></xsl:message>
357                                <xsl:choose>
358                                        <xsl:when test="count($intersect &gt; 0)" >                                     
359                                                        <xsl:copy-of select="$intersect" />                                                     
360                                        </xsl:when>
361                                        <xsl:otherwise>
362                                                <xsl:message>no-intersect:</xsl:message>
363                                                        <xsl:message><xsl:value-of select="count($set/*[1])" />/<xsl:value-of select="count($set/*[1])" /></xsl:message>
364                                                        <xsl:copy-of select="$set/*[1] union my:intersect($set/*[position()&gt; 1])" />
365                                        </xsl:otherwise>
366                                </xsl:choose>                           
367                        </xsl:when>
368                        <xsl:when test="count($set) = 1" >
369                                <xsl:copy-of select="$set[1]/*" />             
370                                <xsl:message>setitem1:<xsl:copy-of select="$set[1]/*" /></xsl:message>
371                        </xsl:when>
372                        <xsl:otherwise>                         
373                        </xsl:otherwise>
374                </xsl:choose>
375</xsl:function>
376
377<!--
378<xsl:function name="my:intersect" >
379        <xsl:param name="set" />
380       
381       
382                        <xsl:value-of select="my:intersect(terms1 intersect *" />
383
384        <xsl:for-each select="$set/item" >
385                <xsl:variable name="terms1" select="*" />                       
386                <xsl:for-each select="$set/item" >
387                        <xsl:value-of select="my:intersect_pairwise(terms1,terms2) intersect *" />
388                       
389                </xsl:for-each>
390        </xsl:for-each>
391       
392</xsl:function>
393-->
394
395<xsl:template match="Term" mode="matrix" >
396        <xsl:param name="profile" />
397
398                        <xsl:variable name="context" select="my:context(.)" >
399                                                        <!--  <xsl:for-each select="ancestor::CMD_Component" >
400                                                                <xsl:value-of select="@name" />.</xsl:for-each>-->
401                                        </xsl:variable> 
402                       
403                        <xsl:variable name="corresp_cmdterm" select="$profile//*[@context=$context]" />                                 
404                        <!--
405                        <xsl:message>context: <xsl:copy-of select="$context" /> </xsl:message> 
406                        <xsl:message>corresp_cmdterm: <xsl:copy-of select="$profile/Term[@context=$context]" /> </xsl:message>--> 
407                        <xsl:variable name="datcat" select="$corresp_cmdterm/@datcat" >
408                                        <!--  <xsl:for-each select="ancestor::CMD_Component" >
409                                                <xsl:value-of select="@name" />.</xsl:for-each>-->
410                        </xsl:variable>
411                        <Term  elem="{@name}" datcat="{$datcat}" corresponding_component="{$corresp_cmdterm/@id}" 
412                                                        parent="{ancestor::Term[1]/@name}"  context="{$context}" >
413                                                        <xsl:copy-of select="@*[not(name()='path')]" /> 
414                                <xsl:apply-templates mode="matrix" >
415                                        <xsl:with-param name="profile" select="$profile" />
416                                </xsl:apply-templates>
417                               
418                        </Term>
419                       
420                       
421       
422</xsl:template>
423
424</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.