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

Last change on this file since 1918 was 1918, checked in by gaba, 12 years ago

autocomplete - explain.xml

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