Remember to get the irspy:xxx attributes copied to the output tree.
[irspy-moved-to-github.git] / xsl / irspy2zeerex.xsl
1 <?xml version="1.0"?>
2 <!--
3     $Id: irspy2zeerex.xsl,v 1.10 2006-11-02 08:28:20 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
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-irspySection"/>
34     </explain>
35   </xsl:template>
36
37
38   <xsl:template name="insert-zeerexBase">
39     <xsl:copy-of select="explain:serverInfo"/>
40     <xsl:copy-of select="explain:databaseInfo"/>
41     <metaInfo>
42       <dateModified><xsl:value-of
43                     select="/*/irspy:status/*[last()]"/></dateModified>
44     </metaInfo>
45   </xsl:template>
46
47
48   <xsl:template name="insert-indexInfo">
49     <indexInfo>
50       <xsl:for-each select="/*/irspy:status/irspy:search">
51         <xsl:variable name="set" select="@set"/>
52         <xsl:variable name="ap" select="@ap"/>
53         <xsl:variable name="old"
54             select="$old_indexes[explain:map/explain:attr/@set = $set and
55                                  explain:map/explain:attr/text() = $ap]"/>
56         <xsl:choose>
57           <xsl:when test="$old">
58             <xsl:call-template name="insert-index-section">
59               <xsl:with-param name="title" select="$old/explain:title"/>
60             </xsl:call-template>
61           </xsl:when>
62           <xsl:otherwise>
63             <xsl:call-template name="insert-index-section"/>
64           </xsl:otherwise>
65         </xsl:choose>
66       </xsl:for-each>
67     </indexInfo>
68   </xsl:template>
69
70
71   <xsl:template name="insert-recordInfo">
72     <recordInfo>
73       <xsl:for-each select="/*/irspy:status/irspy:record_fetch[@ok = 1]">
74         <recordSyntax name="{@syntax}">
75           <elementSet name="F"/> <!-- FIXME: This should be probed too -->
76         </recordSyntax>
77       </xsl:for-each>
78     </recordInfo>
79   </xsl:template>
80
81
82   <!-- 
83        Here we list the bits and pieces of the irspy:status element which we
84        want to keep in the persistent version of the zeerex record.
85        Simply add "| irspy:xxx" to the select attribute.
86   -->
87   <xsl:template name="insert-irspySection">
88     <irspy:status>
89       <xsl:for-each select="*/irspy:probe   |
90                             */irspy:boolean |
91                             */irspy:explain">
92         <xsl:copy-of select="."/>
93       </xsl:for-each>
94     </irspy:status>
95   </xsl:template>
96
97   
98   <xsl:template match="node() | @*">
99     <xsl:copy>
100       <xsl:apply-templates select="@* | node()"/>
101     </xsl:copy>
102   </xsl:template>
103
104
105   <xsl:template name="insert-index-section">
106     <xsl:param name="update" select="."/>
107     <xsl:param name="title">
108       <xsl:call-template name="insert-index-title">
109         <xsl:with-param name="update" select="$update"/>
110       </xsl:call-template>
111     </xsl:param>
112
113     <index>
114       <xsl:attribute name="search">
115         <xsl:choose>
116           <xsl:when test="$update/@ok = 1">true</xsl:when>
117           <xsl:otherwise>false</xsl:otherwise>
118         </xsl:choose>
119       </xsl:attribute>
120       <title primary="true" lang="en">
121         <xsl:value-of select="$title"/>
122       </title>
123       <map primary="true">
124         <attr type="1" set="{$update/@set}">
125           <xsl:value-of select="$update/@ap"/>
126         </attr>
127       </map>
128     </index>
129   </xsl:template>
130
131
132   <xsl:template name="insert-index-title">
133     <xsl:param name="update"/>
134     <xsl:variable name="name"
135                 select="$use_attr_names/*/map[@attr = $update/@ap and
136                                               @set = $update/@set]/@name"/>
137
138     <xsl:choose>
139       <xsl:when test="string-length($name) &gt; 0"><xsl:value-of
140                                             select="$name"/></xsl:when>
141       <xsl:otherwise><xsl:value-of select="$update/@ap"/></xsl:otherwise>
142     </xsl:choose>
143   </xsl:template>
144
145
146   <xsl:template match="*"/>
147
148 </xsl:stylesheet>