source: MDService2/trunk/MDService2/src/xsl/mdset2view.xsl @ 1495

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

started cleaning up code, adding inline-documentation;
also changes to output method in xsl (xhtml)

File size: 14.1 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:saxon="http://saxon.sf.net/"
7  xmlns:my="myFunctions"
8  exclude-result-prefixes="saxon my xs" 
9>
10<!--
11<purpose> generate html view (probably various formats of a MDRepository response.</purpose>
12<params>
13<param name=""></param>
14</params>
15<history>      
16        <change on="2010-03-26" type="created" by="vr">based on cmdi/scripts/cmdinst2view.xsl</change> 
17</history>
18-->
19<xsl:import href="model2matrix.xsl"/>
20<xsl:import href="terms2view.xsl"/>
21
22<!--  method="xhtml" is saxon-specific! prevents  collapsing empty <script> tags, that makes browsers choke -->
23<xsl:output method="xhtml" media-type="application/xhtml+xml" indent="yes" encoding="UTF-8" 
24doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" /> 
25
26  <!--
27  if this is added  to the output-element, it terribly slows down requests with recordset being injected via document() function
28   (like: http://localhost:8080/MDService2/docs/htmlpagetest/queries2)
29  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /> 
30  --> 
31
32<xsl:include href="cmd_commons.xsl"/>
33
34
35<xsl:param name="startItem">1</xsl:param>
36<xsl:param name="maximumItems">50</xsl:param>
37
38<!-- <xsl:param name="format" select="'table'" /> table|list|detail -->
39<xsl:param name="cols" >Id,Title,Location
40<!--
41<col label="id">Id</col>
42<col label="id">idno</col>
43<col label="name">Name</col>
44<col label="title">Title</col>
45<col label="title">title</col>
46<col label="loc" >Location</col>
47<col label="loc" >pubPlace</col>
48<col label="lang" >language</col>
49
50 -->
51</xsl:param>
52
53<xsl:param name="mode" select="'html'" />
54<xsl:param name="title" >
55        <xsl:text>MDRecordSet </xsl:text>
56</xsl:param>
57
58<xsl:variable name="columns" >
59<!-- <xsl:message>COLUMNS: <xsl:value-of select="$cols" /><xsl:value-of select="string-length($cols)" /></xsl:message>  -->
60        <xsl:variable name="colarray" select="tokenize($cols,',')"/>
61        <xsl:for-each select="$colarray" >
62                <col key="{.}" label="{replace(.,'^.*\.([^.]+)$','$1')}" xpath_osbolete="{replace(.,'\.','/')}" xpath="{my:index2xpath(.)}" />
63                <!-- if (contains(.,'.')) then reverse(.) else . -->
64        </xsl:for-each>
65        <xsl:if test="count($colarray)=0" >     
66                <xsl:copy-of select="$autocolumns"></xsl:copy-of>
67                <!-- <xsl:message>AUTOCOLUMNS: <xsl:copy-of select="$autocolumns" /><xsl:value-of select="count($autocolumns)" /></xsl:message> -->
68        </xsl:if>
69       
70</xsl:variable>
71
72<xsl:variable name="autocolumns" >
73        <xsl:variable name="precompute">
74                <xsl:for-each-group select="//Components//*[not(*)][exists(text())]" group-by="name()" >                       
75                        <xsl:sort select="sum(current-group()/string-length(string-join(text(),';')))" order="descending" />
76                        <!-- <xsl:message>precomputing: <xsl:value-of select="name()" />
77                                count: <xsl:value-of select="count(current-group())" /> sum:<xsl:value-of select="sum(current-group()/string-length(string-join(text(),';')))" /></xsl:message> -->
78                        <col key="{name()}" label="{name()}" xpath="{name()}" />
79                </xsl:for-each-group>
80        </xsl:variable>
81         <!-- <xsl:message>precompute: <xsl:copy-of select="$precompute" /><xsl:value-of select="count($precompute)" /></xsl:message>-->
82        <xsl:copy-of select="$precompute/*[position() &lt; $autocolumns_limit]"></xsl:copy-of> 
83</xsl:variable>
84
85<xsl:template name="continue-root" >
86
87<xsl:for-each select="searchRetrieveResponse" >
88<div>
89        <xsl:if test="not(contains($format, 'htmlsimple'))">
90                <xsl:call-template name="header" />
91        </xsl:if>
92        <xsl:choose>
93                                <xsl:when test="contains($format,'htmltable')">
94                                        <xsl:apply-templates select="records" mode="table" />
95                                </xsl:when>                             
96                                <xsl:when test="contains($format,'htmllist')">
97                                        <xsl:apply-templates select="records" mode="list" />
98                                </xsl:when>                             
99                                <xsl:when test="contains($format, 'htmldetail')">
100                                        <xsl:apply-templates select="records" mode="detail" />
101                                </xsl:when>
102                                <xsl:when test="contains($format, 'htmlpage')">
103                                        <xsl:apply-templates select="records" mode="table" />
104                                </xsl:when>
105                                <xsl:when test="contains($format, 'htmlpagelist')">
106                                        <xsl:apply-templates select="records" mode="list" />
107                                </xsl:when>             
108                                <xsl:when test="contains($format, 'htmlsimple')">
109                                        <xsl:apply-templates select="records" mode="simple" />
110                                </xsl:when>             
111                                <xsl:otherwise>mdset2view: unrecognized format: <xsl:value-of select="$format" /></xsl:otherwise>
112        </xsl:choose>
113        </div>
114</xsl:for-each> 
115       
116</xsl:template> 
117       
118       
119<xsl:template match="*" >
120        <!-- <xsl:param name="lv" select="1" />-->
121        <!--<xsl:variable name="lv" select="count(ancestor::d)" /> -->
122        <!-- <xsl:if test="string-length(string-join(.//text(),'')) &gt; 1" > -->               
123                <xsl:variable name="has_text" >
124                        <xsl:choose>
125                        <!-- FIXME: text()[1] ? is that safe?-->
126                                <xsl:when test="normalize-space(text()[1])='Unspecified'">unspecified</xsl:when>
127                                <xsl:when test="not(normalize-space(text()[1])='')">text</xsl:when>                             
128                                <xsl:otherwise>empty</xsl:otherwise>
129                        </xsl:choose>
130                       
131                </xsl:variable>
132                <div class="elem {$has_text}" ><span class="elem_name"><xsl:value-of select="name()" />:</span>
133                <span class="elem_value"><xsl:value-of select="text()" /></span>
134                        <xsl:apply-templates select="*" />                                     
135                </div>
136       
137</xsl:template> 
138       
139        <!--
140sample header: 
141<numberOfRecords>524</numberOfRecords>
142    <echoedSearchRetrieveRequest>//Title[contains(.,'an')] /db/cmdi-mirror/silang_data 1 50</echoedSearchRetrieveRequest>
143    <diagnostics>50</diagnostics>
144    -->
145<xsl:template name="header">
146        <div class="result-header" >
147        <xsl:attribute name="max_value"><xsl:value-of select="numberOfRecords" /></xsl:attribute>
148        <xsl:variable name="cnt_hits" select="number(diagnostics)" />
149       
150        <span class="label" >hits: </span><span class="value hilight"><xsl:value-of select="numberOfRecords" /></span>;
151        <span class="label" >from:</span><span><input type="text" class="value start_record paging-input"><xsl:attribute name="value">
152    <xsl:value-of select="$startItem"/>
153  </xsl:attribute>
154        </input></span>
155        <span class="label" >max:</span><span><input type="text" class="value maximum_records paging-input" ><xsl:attribute name="value">
156    <xsl:value-of select="if(number($cnt_hits) &lt; number($maximumItems)) then $cnt_hits else $maximumItems"/>   
157  </xsl:attribute></input></span>
158  <span class="cmd cmd_reload" /> 
159<!--    <button class="reload">reload</button>-->
160        <span class="cmd cmd_prev">     
161        <xsl:choose>
162                <xsl:when test="$startItem = '1'">
163                        <xsl:attribute name="disabled">disabled</xsl:attribute>
164                </xsl:when>
165                </xsl:choose>
166        </span>
167        <span class="cmd cmd_next">     
168        <xsl:choose>
169                <xsl:when test="$maximumItems &gt; numberOfRecords or $maximumItems = numberOfRecords">
170                        <xsl:attribute name="disabled">disabled</xsl:attribute>
171                </xsl:when>
172                </xsl:choose>
173        </span>
174        <span class="cmd cmd_detail" /> 
175        <!--  <div class='ui-context-dialog ui-widget-content columns-wrapper'>  -->
176                <div class='ui-widget-content columns-wrapper'>
177                        <div class='cmds'><span class="cmd cmd_reload" /><span class="cmd cmd_save" /><span class="cmd cmd_savenew" /></div>
178                        <div class='query-columns'>
179                        <label>Columns</label>
180                                <xsl:call-template name="columns" />
181                        </div>
182                </div>   
183    </div>
184    <div class="note" ><span class="label" >echo: </span><span class="value"><xsl:value-of select="echoedSearchRetrieveRequest" /></span>;<span class="label" >duration: </span><span class="value"><xsl:value-of select="extraResponseData/duration" /></span>;</div>
185</xsl:template> 
186
187<xsl:template name="columns">
188        <table>
189        <tr><td valign="top">
190        <div class="columns-selected" > 
191        <!--
192                <span class="label">selected columns</span>
193                <ul class="query-columnslist">
194                        <xsl:for-each select="$columns/*">
195                                <li><xsl:value-of select="@label"/><span class='cmd cmd_del'/></li>
196                        </xsl:for-each>
197                </ul>
198         -->
199        </div>
200        </td><td valign="top">
201                <div>
202                        <span class="label" >format:</span>
203                        <select class="value-format">
204                       
205                        <option value="htmltable">
206                                <xsl:choose>
207                                <xsl:when test="contains($format, 'htmltable')">
208                                        <xsl:attribute name="selected">selected</xsl:attribute>
209                                </xsl:when>
210                                </xsl:choose><xsl:text>table</xsl:text>         
211                        </option>
212                        <option value="htmllist">
213                                <xsl:choose>
214                                <xsl:when test="contains($format, 'htmllist')">
215                                        <xsl:attribute name="selected">selected</xsl:attribute>
216                                </xsl:when>
217                                </xsl:choose><xsl:text>list</xsl:text>
218                        </option>
219                        </select>
220                </div>
221        <xsl:if test="extraResponseData/usedProfiles" >
222                <div class="used-profiles">
223                        <span class="label">used profiles in whole result</span>
224                        <ul>
225                                <xsl:for-each select="extraResponseData/usedProfiles/profile" >
226                                        <li><a href="{@id}" ><xsl:value-of select="@name"/></a><span class="note">|<xsl:value-of select="@count"/>|</span></li>
227                                </xsl:for-each>
228                        </ul>
229                </div>
230        </xsl:if>
231        </td></tr>     
232        <xsl:variable name="resultSummary" >           
233                <!-- wrap in <Terms> to make digestible for model2matrix -->
234                <xsl:variable name="terms" >
235                        <Terms>
236                        <!-- TODO: if already provided by mdrepo, use that, otherwise DIY -->           
237                        <xsl:choose>
238                                <xsl:when test="extraResponseData/resultSummary"><xsl:copy-of select="extraResponseData/resultSummary/Term" /></xsl:when>
239                                <xsl:otherwise>
240                                <!-- TODO! -->
241                                </xsl:otherwise>
242                        </xsl:choose>
243                        </Terms>
244                </xsl:variable>
245                <xsl:apply-templates select="$terms" mode="matrix" />
246        </xsl:variable>         
247        <!--   <xsl:message><xsl:value-of select="$resultSummary/*/name()" /> </xsl:message> -->
248       
249        <tr><td colspan="2">   
250        <div class="result-summary">   
251                <span class="label">structure summary of shown result</span>   
252                <!--  calling template in terms2view.xsl -->
253                <xsl:apply-templates select="$resultSummary" mode="terms-tree" />               
254        </div> 
255        </td></tr>
256        </table>
257</xsl:template>
258
259<xsl:template match="records" mode="table">
260   
261        <table class="show">           
262                <thead><tr><th>pos</th>         
263                        <xsl:for-each select="$columns/*">
264                                <th title="{@key}" ><xsl:value-of select="@label"/></th>
265                        </xsl:for-each>                         
266                </tr></thead>
267                <tbody>         
268                        <xsl:apply-templates select="CMD" mode="table" />                       
269                </tbody>
270        </table>
271
272</xsl:template>
273
274
275<xsl:template match="records" mode="list">
276   
277        <table class="show">           
278                <thead><tr><th>pos</th><th>record</th>                                         
279                </tr></thead>
280                <tbody>         
281                        <xsl:apply-templates select="CMD" mode="list" />                       
282                </tbody>
283        </table>
284
285</xsl:template>
286
287<!-- FIXME:   there should be a @match=`record` template to make it SRU-compatible -->
288
289<xsl:template match="CMD" mode="table">
290
291        <xsl:variable name="curr_record" select="." />
292        <xsl:variable name="fields" >
293                <xsl:for-each select="$columns/*">
294                                <!-- <td ><xsl:value-of select="$curr_cmd//*[name()=current()]" /></td> -->
295        <!--    <xsl:message><xsl:copy-of select="."></xsl:copy-of> </xsl:message> -->
296                                         <xsl:variable name="curr_col_xpath" select="@xpath" />
297                                <td>                   
298                                        <xsl:for-each select="$curr_record">
299                                                <xsl:call-template name="format-field" >
300                                                        <xsl:with-param name="elems" select="saxon:evaluate(concat('.//', $curr_col_xpath))" />
301                                                </xsl:call-template>                                   
302                                       
303                                        </xsl:for-each>
304                                </td>
305                                </xsl:for-each>
306          </xsl:variable>
307
308        <xsl:call-template name="record-table-row" >
309                <xsl:with-param name="fields" select="$fields" />
310        </xsl:call-template>
311       
312</xsl:template>
313
314<xsl:template match="CMD" mode="list">
315
316                <xsl:variable name="curr_record" select="." />                 
317                <xsl:variable name="cmd_sel_fields" >
318                        <xsl:for-each select="$columns/*">
319                                <xsl:variable name="curr_col_xpath" select="@xpath" />                                                 
320                                <xsl:for-each select="$curr_record">                           
321                                        <xsl:copy-of select="saxon:evaluate(concat('.//', $curr_col_xpath))" />
322                                </xsl:for-each>                         
323                        </xsl:for-each>
324                </xsl:variable>
325                <xsl:variable name="fields" >
326                        <div>
327                                <xsl:apply-templates select="$cmd_sel_fields" mode="format-xmlelem" />
328                        </div>
329                </xsl:variable>
330
331        <xsl:call-template name="record-table-row" >
332                <xsl:with-param name="fields" select="$fields" />
333        </xsl:call-template>
334                                                                                       
335</xsl:template>
336
337
338<xsl:template name="record-table-row">
339<xsl:param name="fields" />
340<!-- @field absolute_position compute records position over whole recordset, ie add `startItem` (important when paging)
341 -->
342        <xsl:variable name="absolute_position" select="if (number($startItem)=number($startItem)) then number($startItem) + position() - 1 else  position()" />
343       
344                <tr>
345                <td rowspan="2">
346                        <xsl:choose>
347                                <xsl:when test="exists(.//MdSelfLink)" >
348                                        <xsl:choose>
349                                                <xsl:when test="contains($format, 'htmlsimple')" >
350                                                <a class="internal" href="{concat('../../record/htmldetail/', my:encodePID(.//MdSelfLink), '?repository=', $repository_name)}" >
351                                                                <xsl:value-of select="$absolute_position" />
352                                                </a>                                           
353                                                </xsl:when>
354                                                <xsl:otherwise>
355                                                <a class="internal" href="{concat('record/htmldetail/', my:encodePID(.//MdSelfLink), '?repository=', $repository_name)}" >
356                                                                <xsl:value-of select="$absolute_position" />
357                                                </a>
358                                                </xsl:otherwise>
359                                        </xsl:choose>
360                                        <span class="cmd cmd_detail" ></span>                           
361                                        <span class="cmd cmd_save" ></span>                                             
362                                </xsl:when>
363                                <xsl:otherwise><xsl:value-of select="$absolute_position" /></xsl:otherwise>
364                        </xsl:choose>
365                </td>           
366                <td colspan="{count($columns/*)}" >
367                                <xsl:call-template name="getContext" />
368                        <div class="title" ><xsl:call-template name="getName" /></div>                 
369                </td>
370                </tr>
371                <tr>
372                        <xsl:copy-of select="$fields" />                                               
373                </tr>                                                                   
374</xsl:template>
375
376
377<xsl:template match="records" mode="simple">
378   
379    <form action="" method="get">
380                                <input type="text" name="q" ><xsl:attribute name="value"><xsl:value-of select="$q"/></xsl:attribute> </input>
381                               
382                                <input type="submit" value="Submit" > </input><br/>
383                                <label>startItem: </label><input type="text" name="startItem" ><xsl:attribute name="value"><xsl:value-of select="$startItem"/></xsl:attribute> </input><br/>
384                                <label> maxItems: </label><input type="text" name="maximumItems" ><xsl:attribute name="value"><xsl:value-of select="$maximumItems"/></xsl:attribute> </input>
385        </form>
386        <xsl:call-template name="header" />     
387        <table class="show">           
388                <thead><tr><th>pos</th>         
389                        <xsl:for-each select="$columns/*">
390                                <th title="{@key}" ><xsl:value-of select="@label"/></th>
391                        </xsl:for-each>                         
392                </tr></thead>
393                <tbody> 
394                        <xsl:apply-templates select="CMD" mode="table" />                               
395                </tbody>
396        </table>
397
398</xsl:template>
399
400</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.