Handling the case where no existing indexInfo or recordInfo elements exist.
[irspy-moved-to-github.git] / xsl / irspy2zeerex.xsl
1 <?xml version="1.0"?>
2 <!--
3     $Id: irspy2zeerex.xsl,v 1.7 2006-10-27 13:41:57 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:preserve-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
27   
28   <xsl:template match="node() | @*">
29     <xsl:copy>
30       <xsl:apply-templates select="@* | node()"/>
31     </xsl:copy>
32   </xsl:template>
33
34
35   <xsl:template match="/*">
36     <explain>
37       <xsl:apply-templates select="explain:serverInfo   |
38                                    explain:databaseInfo |
39                                    explain:metaInfo"/>
40                                    
41       <xsl:call-template name="insert-indexInfo"/>
42       <xsl:call-template name="insert-recordInfo"/>
43     </explain>
44   </xsl:template>
45
46
47   <xsl:template name="insert-indexInfo">
48     <indexInfo>
49       <xsl:for-each select="/*/irspy:status/irspy:search">
50         <xsl:variable name="set" select="@set"/>
51         <xsl:variable name="ap" select="@ap"/>
52         <xsl:variable name="old"
53             select="$old_indexes[explain:map/explain:attr/@set = $set and
54                                  explain:map/explain:attr/text() = $ap]"/>
55         <xsl:choose>
56           <xsl:when test="$old">
57             <xsl:call-template name="insert-index-section">
58               <xsl:with-param name="title" select="$old/explain:title"/>
59             </xsl:call-template>
60           </xsl:when>
61           <xsl:otherwise>
62             <xsl:call-template name="insert-index-section"/>
63           </xsl:otherwise>
64         </xsl:choose>
65       </xsl:for-each>
66     </indexInfo>
67   </xsl:template>
68
69
70   <xsl:template name="insert-recordInfo">
71     <recordInfo>
72       <xsl:for-each select="/*/irspy:status/irspy:record_fetch[@ok = 1]">
73         <recordSyntax name="{@syntax}">
74           <elementSet name="F"/> <!-- FIXME: This should be probed too -->
75         </recordSyntax>
76       </xsl:for-each>
77     </recordInfo>
78   </xsl:template>
79
80
81   <xsl:template name="insert-index-section">
82     <xsl:param name="update" select="."/>
83     <xsl:param name="title">
84       <xsl:call-template name="insert-index-title">
85         <xsl:with-param name="update" select="$update"/>
86       </xsl:call-template>
87     </xsl:param>
88
89     <index>
90       <xsl:attribute name="search">
91         <xsl:choose>
92           <xsl:when test="$update/@ok = 1">true</xsl:when>
93           <xsl:otherwise>false</xsl:otherwise>
94         </xsl:choose>
95       </xsl:attribute>
96       <title primary="true" lang="en">
97         <xsl:value-of select="$title"/>
98       </title>
99       <map primary="true">
100         <attr type="1" set="{$update/@set}">
101           <xsl:value-of select="$update/@ap"/>
102         </attr>
103       </map>
104     </index>
105   </xsl:template>
106
107
108   <xsl:template name="insert-index-title">
109     <xsl:param name="update"/>
110     <xsl:variable name="name"
111                 select="$use_attr_names/*/map[@attr = $update/@ap and
112                                               @set = $update/@set]/@name"/>
113
114     <xsl:choose>
115       <xsl:when test="string-length($name) &gt; 0"><xsl:value-of
116                                             select="$name"/></xsl:when>
117       <xsl:otherwise><xsl:value-of select="$update/@ap"/></xsl:otherwise>
118     </xsl:choose>
119   </xsl:template>
120
121
122   <xsl:template match="explain:dateModified">
123     <dateModified><xsl:value-of
124                     select="/*/irspy:status/*[last()]"/></dateModified>
125   </xsl:template>
126
127
128 </xsl:stylesheet>