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