Tentative support for preserving server implementation ID/Name/Version.
[irspy-moved-to-github.git] / xsl / irspy2zeerex.xsl
1 <?xml version="1.0"?>
2 <!--
3     $Id: irspy2zeerex.xsl,v 1.16 2006-12-18 15:38:54 mike 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 select="explain:indexInfo/*"/>
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">
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:probe"/>
128
129       <xsl:call-template name="insert-latest-nodes">
130         <xsl:with-param name="nodes" select="*/irspy:boolean"/>
131       </xsl:call-template>
132       
133       <xsl:call-template name="insert-latest-nodes">
134         <xsl:with-param name="nodes" select="*/irspy:named_resultset"/>
135       </xsl:call-template>
136       
137       <xsl:call-template name="insert-latest-nodes">
138         <xsl:with-param name="nodes" select="*/irspy:explain"/>
139       </xsl:call-template>
140
141       <xsl:call-template name="insert-latest-nodes">
142         <xsl:with-param name="nodes" select="*/irspy:serverImplementationId"/>
143       </xsl:call-template>
144       <xsl:call-template name="insert-latest-nodes">
145         <xsl:with-param name="nodes" select="*/irspy:serverImplementationName"/>
146       </xsl:call-template>
147       <xsl:call-template name="insert-latest-nodes">
148         <xsl:with-param name="nodes" select="*/irspy:serverImplementationVersion"/>
149       </xsl:call-template>
150     </irspy:status>
151   </xsl:template>
152
153
154   <!-- 
155        NB: This template assumes that the irspy:probe nodes come in ascending
156        order. If this is not the case, this template has to do some kind of
157        sorting which makes the whole thing more complicated.
158
159        Dec 12, 2006: Anders
160   -->
161   <xsl:template name="insert-latest-nodes">
162     <xsl:param name="nodes"/>
163     <xsl:param name="i" select="count(*/irspy:probe[@ok='1'])"/>
164     <xsl:variable name="date"
165         select="*/irspy:probe[@ok='1' and position() = $i]"/>
166     <xsl:variable name="latest"
167         select="$nodes[irspy:strcmp(text(), $date) > 0]"/>
168
169     <xsl:choose>
170       <xsl:when test="$latest">
171         <xsl:copy-of select="$latest"/>
172       </xsl:when>
173       <xsl:when test="$i > 0">
174         <xsl:call-template name="insert-latest-nodes">
175           <xsl:with-param name="nodes" select="$nodes"/>
176           <xsl:with-param name="i" select="$i - 1"/>
177         </xsl:call-template>
178       </xsl:when>
179       <xsl:otherwise>
180         <irspy:missing/>
181       </xsl:otherwise>
182     </xsl:choose>
183   </xsl:template>
184
185   
186   <xsl:template match="node() | @*">
187     <xsl:copy>
188       <xsl:apply-templates select="@* | node()"/>
189     </xsl:copy>
190   </xsl:template>
191
192
193   <xsl:template name="insert-index-section">
194     <xsl:param name="update" select="."/>
195     <xsl:param name="title">
196       <xsl:call-template name="insert-index-title">
197         <xsl:with-param name="update" select="$update"/>
198       </xsl:call-template>
199     </xsl:param>
200
201     <index>
202       <xsl:attribute name="search">
203         <xsl:choose>
204           <xsl:when test="$update/@ok = 1">true</xsl:when>
205           <xsl:otherwise>false</xsl:otherwise>
206         </xsl:choose>
207       </xsl:attribute>
208       <title primary="true" lang="en">
209         <xsl:value-of select="$title"/>
210       </title>
211       <map primary="true">
212         <attr type="1" set="{$update/@set}">
213           <xsl:value-of select="$update/@ap"/>
214         </attr>
215       </map>
216     </index>
217   </xsl:template>
218
219
220   <xsl:template name="insert-index-title">
221     <xsl:param name="update"/>
222     <xsl:variable name="name"
223                 select="$use_attr_names/*/map[@attr = $update/@ap and
224                                               @set = $update/@set]/@name"/>
225
226     <xsl:choose>
227       <xsl:when test="string-length($name) &gt; 0"><xsl:value-of
228                                             select="$name"/></xsl:when>
229       <xsl:otherwise><xsl:value-of select="$update/@ap"/></xsl:otherwise>
230     </xsl:choose>
231   </xsl:template>
232
233
234   <xsl:template match="*"/>
235
236 </xsl:stylesheet>