Added several type casts due to no portable way of doing printf of
[yaz-moved-to-github.git] / util / pqf2pqf.xsl
1 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
2   version="1.0">
3
4   <xsl:output indent="yes" method="xml" version="1.0" encoding="UTF-8"/>
5
6   <!--
7 ./yaz-xmlquery -p '@and @attr 1=1016 @attr 4=2 @attr 6=3 the @attr 1=4 fish' > test.xml && xmllint -format test.xml && ./yaz-xmlquery -x test1.xml && xsltproc pqf2pqf.xsl test.xml |tee test2.xml && ./yaz-xmlquery -x test2.xml 
8
9 ./yaz-xmlquery -p '@not @attr 1=1016 @attr 4=2 @attr 6=3 @attr 7=1 @attr 8=4 fish @attr 1=4 fish' > test.xml && xmllint -format test.xml && ./yaz-xmlquery -x test.xml && xsltproc pqf2pqf.xsl test.xml |tee test2.xml && ./yaz-xmlquery -x test2.xml 
10   -->
11
12   <!-- disable default templates -->
13   <xsl:template match="text()"/>
14   <xsl:template match="node()"/>
15
16   <!-- identity stylesheet templates -->
17   <!-- these parse pqf-xml input recursively and make identity operations -->
18   <xsl:template match="/query">
19     <query>
20       <xsl:apply-templates/>
21     </query>
22   </xsl:template>
23
24   <xsl:template match="rpn">
25     <rpn>
26       <xsl:attribute name="set">
27         <xsl:value-of  select="@set"/>
28       </xsl:attribute>
29       <xsl:apply-templates/>
30     </rpn>
31   </xsl:template>
32
33   <xsl:template match="operator">
34     <operator>
35       <xsl:attribute name="type">
36         <xsl:value-of  select="@type"/>
37       </xsl:attribute>
38       <xsl:apply-templates/>
39     </operator>
40   </xsl:template>
41
42   <xsl:template match="apt">
43     <apt>
44       <!-- no re-ordering @attr's if you use the following -->
45       <!--
46       <xsl:apply-templates select="attr"/>
47       -->
48       <xsl:apply-templates select="attr[@type=1]"/>
49       <xsl:apply-templates select="attr[@type=2]"/>
50       <xsl:apply-templates select="attr[@type=4]"/>
51       <xsl:apply-templates select="attr[@type=5]"/>
52       <xsl:apply-templates select="attr[@type=6]"/>
53       <xsl:apply-templates select="attr[@type=7]"/>
54       <xsl:apply-templates select="attr[@type=8]"/>
55       <xsl:apply-templates select="attr[@type=9]"/>
56       <xsl:apply-templates select="term"/>
57     </apt>
58   </xsl:template>
59
60   <xsl:template match="attr">
61     <xsl:copy-of select="."/>
62   </xsl:template>
63
64   <xsl:template match="term">
65     <xsl:copy-of select="."/>
66   </xsl:template>
67
68
69   <!-- special rewrite templates
70        these are kicking in when special conditions apply -->
71
72
73   <!-- attribute rewrites --> 
74
75   <!-- remove all @attr 6=3 with bracket syntax -->
76   <!--
77   <xsl:template match="attr[@type=6][@value=3]">
78   </xsl:template>
79   -->
80
81   <!-- remove all @attr 6=4 with and syntax -->
82   <!--
83   <xsl:template match="attr[@type=6 and @value=4]">
84   </xsl:template>
85   -->
86
87   <!-- rewrite all @attr 4=2 to @attr 4=1 -->
88   <!--
89   <xsl:template match="attr[@type=4][@value=2]">
90     <attr type="4" value="1"/>
91   </xsl:template>
92   -->
93
94   <!-- rewrite all @attr 1=1016 to @attr 1=1016 @attr 6=2 -->
95   <!-- this will leave double @attr 6=? nodes, unless you remove all
96        @attr 6=? nodes in some other template -->
97   <!--
98   <xsl:template match="attr[@type=1 and @value=1016]">
99     <attr type="1" value="1016"/>
100     <attr type="6" value="2"/>
101   </xsl:template>
102   -->
103
104
105   <!-- rules depending on multiple attribute combinations -->
106   
107   <!-- whenever there is a <apt> containing an @attr 7 and an @attr 8,
108        rewrite these and drop all @attr 3 .
109        Notice that the selection rules can equally either be written 
110        'attr/@type=7' or 'attr[@type=8]' with no difference -->
111   <!--
112   <xsl:template match="apt[attr/@type=7 and attr/@type=8]">
113     <apt>
114       <xsl:apply-templates select="attr[@type=1]"/>
115       <xsl:apply-templates select="attr[@type=2]"/>
116       <xsl:apply-templates select="attr[@type=4]"/>
117       <xsl:apply-templates select="attr[@type=5]"/>
118       <xsl:apply-templates select="attr[@type=6]"/>
119       <attr type="7" value="2"/>
120       <attr type="8" value="5"/>
121       <xsl:apply-templates select="attr[@type=9]"/>
122       <xsl:apply-templates select="term"/>
123     </apt>
124   </xsl:template>
125   -->
126
127   <!-- whenever there is an apt containing an @attr 7=1, an @attr 8=4, and
128        an @attr 1=? (of any value), let @attr 1=? pass unaltered, drop
129        @attr 3=? totally, and rewrite @attr 7=1 and @attr 8=4 .
130        Notice that this rule can equally be written either with 'and' 
131        connecting the attribute type and value, or with a double '[]'.-->
132   <!--
133   <xsl:template match="apt[attr[@type=7 and @value=1] 
134                        and attr[@type=8][@value=4] 
135                        and attr[@type=1]] ">
136     <apt>
137       <xsl:apply-templates select="attr[@type=1]"/>
138       <xsl:apply-templates select="attr[@type=2]"/>
139       <xsl:apply-templates select="attr[@type=4]"/>
140       <xsl:apply-templates select="attr[@type=5]"/>
141       <xsl:apply-templates select="attr[@type=6]"/>
142       <attr type="7" value="2"/>
143       <attr type="8" value="5"/>
144       <xsl:apply-templates select="attr[@type=9]"/>
145       <xsl:apply-templates select="term"/>
146    </apt>
147   </xsl:template>
148   -->
149
150
151   <!-- term rewrites -->
152
153   <!-- rewrite general term fish to squid -->
154   <!--
155   <xsl:template match="term[@type='general'][text()='fish']">
156     <term type="general">squid</term>
157   </xsl:template>
158   -->
159
160   <!-- operator rewrites -->
161
162   <!-- remove 'not' operator, use first <apt> only -->
163   <!-- 
164   <xsl:template match="operator[@type='not']">
165     <xsl:apply-templates select="apt[1]"/>
166   </xsl:template>
167   -->
168
169   <!-- nasty rewrite 'not' operator to 'and' operator -->
170   <!--
171   <xsl:template match="operator[@type='not']">
172     <operator>
173       <xsl:attribute name="type">
174         <xsl:value-of  select="'and'"/>
175       </xsl:attribute>
176       <xsl:apply-templates/>
177     </operator>
178   </xsl:template>
179   -->
180
181 </xsl:stylesheet>
182
183