Only insert those index terms which have @search="true". This happens both for
[irspy-moved-to-github.git] / xsl / irspy2zeerex.xsl
1 <?xml version="1.0"?>
2 <!--
3     $Id: irspy2zeerex.xsl,v 1.20 2007-04-02 10:11:17 sondberg Exp $
4
5     This stylesheet is used by IRSpy to map the internal mixed Zeerex/IRSpy
6     record format into the Zeerex record which we store.
7
8 -->
9 <xsl:stylesheet
10     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
11     xmlns:irspy="http://indexdata.com/irspy/1.0"
12     xmlns="http://explain.z3950.org/dtd/2.0/"
13     xmlns:explain="http://explain.z3950.org/dtd/2.0/"
14     exclude-result-prefixes="irspy explain"
15     version="1.0">
16
17   <xsl:output indent="yes"
18       method="xml"
19       version="1.0"
20       encoding="UTF-8"/>
21
22   <xsl:strip-space elements="*"/>
23
24   <xsl:variable name="old_indexes" select="/*/explain:indexInfo/explain:index"/>
25   <xsl:variable name="use_attr_names" select="document('use-attr-names.xml')"/>
26   <xsl:variable name="ping_date" select="/*/irspy:status/irspy:probe[last()]"/>
27
28
29   <xsl:template match="/*">
30     <explain>
31       <xsl:call-template name="insert-zeerexBase"/>
32       <xsl:call-template name="insert-indexInfo"/>
33       <xsl:call-template name="insert-recordInfo"/>
34       <xsl:call-template name="insert-configInfo"/>
35       <xsl:call-template name="insert-irspySection"/>
36     </explain>
37   </xsl:template>
38
39
40   <xsl:template name="insert-zeerexBase">
41     <xsl:copy-of select="explain:serverInfo"/>
42     <xsl:copy-of select="explain:databaseInfo"/>
43     <metaInfo>
44       <dateModified><xsl:value-of select="$ping_date"/></dateModified>
45     </metaInfo>
46   </xsl:template>
47
48
49   <xsl:template name="insert-indexInfo">
50     <indexInfo>
51       <xsl:choose>
52
53         <!-- Check that search was actually probed -->
54         <xsl:when test="/*/irspy:status/irspy:search">
55           <xsl:for-each select="/*/irspy:status/irspy:search">
56             <xsl:variable name="set" select="@set"/>
57             <xsl:variable name="ap" select="@ap"/>
58             <xsl:variable name="old"
59                 select="$old_indexes[explain:map/explain:attr/@set = $set and
60                                      explain:map/explain:attr/text() = $ap]"/>
61             <xsl:choose>
62               <xsl:when test="$old">
63                 <xsl:call-template name="insert-index-section">
64                   <xsl:with-param name="title" select="$old/explain:title"/>
65                 </xsl:call-template>
66               </xsl:when>
67               <xsl:otherwise>
68                 <xsl:call-template name="insert-index-section"/>
69               </xsl:otherwise>
70             </xsl:choose>
71           </xsl:for-each>
72         </xsl:when>
73
74         <!-- If not, insert what we already had... -->
75         <xsl:otherwise>
76           <xsl:copy-of
77                 select="explain:indexInfo/explain:index[@search='true']"/>
78         </xsl:otherwise>
79       </xsl:choose>
80     </indexInfo>
81   </xsl:template>
82
83
84   <xsl:template name="insert-recordInfo">
85     <recordInfo>
86       <xsl:choose>
87
88         <!-- Did we actually probe record syntaxes? -->
89         <xsl:when test="/*/irspy:status/irspy:record_fetch">
90           <xsl:for-each select="/*/irspy:status/irspy:record_fetch[@ok = 1]">
91             <recordSyntax name="{@syntax}">
92               <elementSet name="F"/> <!-- FIXME: This should be probed too -->
93             </recordSyntax>
94           </xsl:for-each>
95         </xsl:when>
96
97         <!-- If not, use the existing test report... -->
98         <xsl:otherwise>
99           <xsl:copy-of select="explain:recordInfo/*"/>
100         </xsl:otherwise>
101       </xsl:choose>
102     </recordInfo>
103   </xsl:template>
104
105
106   <xsl:template name="insert-configInfo">
107     <configInfo>
108       <xsl:choose>
109         <xsl:when test="/*/irspy:status/irspy:init_opt">
110           <xsl:for-each select="/*/irspy:status/irspy:init_opt">
111             <supports type="z3950_{@option}">1</supports>
112           </xsl:for-each>
113         </xsl:when>
114         <xsl:otherwise>
115           <xsl:copy-of select="explain:configInfo/*"/>
116         </xsl:otherwise>
117       </xsl:choose>
118     </configInfo>
119   </xsl:template>
120
121
122   <!-- 
123        Here we list the bits and pieces of the irspy:status element which we
124        want to keep in the persistent version of the zeerex record.
125   -->
126   <xsl:template name="insert-irspySection">
127     <irspy:status>
128       <xsl:copy-of select="*/irspy:libraryType"/>
129       <xsl:copy-of select="*/irspy:country"/>
130       <xsl:copy-of select="*/irspy:probe"/>
131
132       <xsl:call-template name="insert-latest-nodes">
133         <xsl:with-param name="nodes" select="*/irspy:boolean"/>
134         <xsl:with-param name="what" select="'boolean'"/>
135       </xsl:call-template>
136       
137       <xsl:call-template name="insert-latest-nodes">
138         <xsl:with-param name="nodes" select="*/irspy:named_resultset"/>
139         <xsl:with-param name="what" select="'named_resultset'"/>
140       </xsl:call-template>
141       
142       <xsl:call-template name="insert-latest-nodes">
143         <xsl:with-param name="nodes" select="*/irspy:explain"/>
144         <xsl:with-param name="what" select="'explain'"/>
145       </xsl:call-template>
146
147       <xsl:call-template name="insert-latest-nodes">
148         <xsl:with-param name="nodes" select="*/irspy:serverImplementationId"/>
149         <xsl:with-param name="what" select="'serverImplementationId'"/>
150       </xsl:call-template>
151
152       <xsl:call-template name="insert-latest-nodes">
153         <xsl:with-param name="nodes" select="*/irspy:serverImplementationName"/>
154         <xsl:with-param name="what" select="'serverImplementationName'"/>
155       </xsl:call-template>
156
157       <xsl:call-template name="insert-latest-nodes">
158         <xsl:with-param name="nodes"
159                     select="*/irspy:serverImplementationVersion"/>
160         <xsl:with-param name="what" select="'serverImplementationVersion'"/>
161       </xsl:call-template>
162     </irspy:status>
163   </xsl:template>
164
165
166   <!-- 
167        NB: This template assumes that the irspy:probe nodes come in ascending
168        order. If this is not the case, this template has to do some kind of
169        sorting which makes the whole thing more complicated.
170
171        Dec 12, 2006: Anders
172   -->
173   <xsl:template name="insert-latest-nodes">
174     <xsl:param name="nodes"/>
175     <xsl:param name="what" select="'unspecified'"/>
176     <xsl:param name="i" select="count(*/irspy:probe[@ok='1'])"/>
177     <xsl:variable name="date"
178         select="*/irspy:probe[@ok='1' and position() = $i]"/>
179     <xsl:variable name="latest"
180         select="$nodes[irspy:strcmp(text(), $date) > 0]"/>
181
182     <xsl:choose>
183       <xsl:when test="$latest">
184         <xsl:copy-of select="$latest"/>
185       </xsl:when>
186       <xsl:when test="$i > 0">
187         <xsl:call-template name="insert-latest-nodes">
188           <xsl:with-param name="what" select="$what"/>
189           <xsl:with-param name="nodes" select="$nodes"/>
190           <xsl:with-param name="i" select="$i - 1"/>
191         </xsl:call-template>
192       </xsl:when>
193       <xsl:otherwise>
194         <irspy:missing what="{$what}" when="{$ping_date}"/>
195       </xsl:otherwise>
196     </xsl:choose>
197   </xsl:template>
198
199   
200   <xsl:template match="node() | @*">
201     <xsl:copy>
202       <xsl:apply-templates select="@* | node()"/>
203     </xsl:copy>
204   </xsl:template>
205
206
207   <xsl:template name="insert-index-section">
208     <xsl:param name="update" select="."/>
209     <xsl:param name="title">
210       <xsl:call-template name="insert-index-title">
211         <xsl:with-param name="update" select="$update"/>
212       </xsl:call-template>
213     </xsl:param>
214
215     <xsl:if test="$update/@ok = 1">
216       <index search="true">
217         <title primary="true" lang="en"><xsl:value-of select="$title"/></title>
218         <map primary="true">
219           <attr type="1" set="{$update/@set}">
220             <xsl:value-of select="$update/@ap"/>
221           </attr>
222         </map>
223       </index>
224     </xsl:if>
225   </xsl:template>
226
227
228   <xsl:template name="insert-index-title">
229     <xsl:param name="update"/>
230     <xsl:variable name="name"
231                 select="$use_attr_names/*/map[@attr = $update/@ap and
232                                               @set = $update/@set]/@name"/>
233
234     <xsl:choose>
235       <xsl:when test="string-length($name) &gt; 0"><xsl:value-of
236                                             select="$name"/></xsl:when>
237       <xsl:otherwise><xsl:value-of select="$update/@ap"/></xsl:otherwise>
238     </xsl:choose>
239   </xsl:template>
240
241
242   <xsl:template match="*"/>
243
244 </xsl:stylesheet>