added some examples of XMLPQF to XMLPQF query rewrites
[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 && xsltproc identity.xsl 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 && xsltproc identity.xsl 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   <xsl:template match="/query">
18     <query>
19       <xsl:apply-templates/>
20     </query>
21   </xsl:template>
22
23   <xsl:template match="rpn">
24     <rpn>
25       <xsl:attribute name="set">
26         <xsl:value-of  select="@set"/>
27       </xsl:attribute>
28       <xsl:apply-templates/>
29     </rpn>
30   </xsl:template>
31
32
33   <xsl:template match="apt">
34     <apt>
35       <xsl:apply-templates select="attr"/>
36       <xsl:apply-templates select="term"/>
37       <!-- re-ordering @attr's if you use the following -->
38       <!--
39       <xsl:apply-templates select="attr[@type=1]"/>
40       <xsl:apply-templates select="attr[@type=2]"/>
41       <xsl:apply-templates select="attr[@type=4]"/>
42       <xsl:apply-templates select="attr[@type=5]"/>
43       <xsl:apply-templates select="attr[@type=6]"/>
44       <xsl:apply-templates select="attr[@type=7]"/>
45       <xsl:apply-templates select="attr[@type=8]"/>
46       <xsl:apply-templates select="attr[@type=9]"/>
47       <xsl:apply-templates select="term"/>
48       -->
49     </apt>
50   </xsl:template>
51
52   <xsl:template match="operator">
53     <operator>
54       <xsl:attribute name="type">
55         <xsl:value-of  select="@type"/>
56       </xsl:attribute>
57       <xsl:apply-templates/>
58     </operator>
59   </xsl:template>
60
61   <xsl:template match="attr">
62     <xsl:copy-of select="."/>
63   </xsl:template>
64
65   <xsl:template match="term">
66     <xsl:copy-of select="."/>
67   </xsl:template>
68
69
70   <!-- special rewrite templates -->
71
72   <!-- attribute rewrites --> 
73
74   <!-- remove all @attr 6=3 with bracket syntax -->
75   <xsl:template match="attr[@type=6][@value=3]">
76   </xsl:template>
77
78   <!-- remove all @attr 6=4 with and syntax -->
79   <xsl:template match="attr[@type=6 and @value=4]">
80   </xsl:template>
81
82   <!-- rewrite all @attr 4=2 to @attr 4=1 -->
83   <xsl:template match="attr[@type=4][@value=2]">
84     <attr type="4" value="1"/>
85   </xsl:template>
86
87   <!-- rewrite all @attr 1=1016 to @attr 1=1016 @attr 6=2 -->
88   <xsl:template match="attr[@type=1 and @value=1016]">
89     <attr type="1" value="1016"/>
90     <attr type="6" value="2"/>
91   </xsl:template>
92
93
94
95   <!-- term rewrites -->
96
97   <!-- rewrite general term fish to squid -->
98   <xsl:template match="term[@type='general'][text()='fish']">
99     <term type="general">squid</term>
100   </xsl:template>
101
102
103   <!-- operator rewrites -->
104   <!-- nasty rewrite 'not' operator to 'and' operator -->
105   <!--
106   <xsl:template match="operator[@type='not']">
107     <operator>
108       <xsl:attribute name="type">
109         <xsl:value-of  select="'and'"/>
110       </xsl:attribute>
111       <xsl:apply-templates/>
112     </operator>
113   </xsl:template>
114   -->
115
116   <!-- remove 'not' operator, use first <apt> only -->
117   <xsl:template match="operator[@type='not']">
118     <xsl:apply-templates select="apt[1]"/>
119   </xsl:template>
120   
121
122
123   <!-- more nasty things for special occasions 
124        depending on multiple attribute combinations -->
125
126   <!-- whenever there is a <apt> containing a @attr 7 and a @attr 8,
127        rewrite these and drop all @attr 3, 
128        finally sort all other @attrs according to type 
129        and call all terms, still rewriting 'fish' to 'squid' -->
130  
131  <xsl:template match="apt[attr/@type=7 and attr/@type=8]">
132    <apt>
133       <xsl:apply-templates select="attr[@type=1]"/>
134       <xsl:apply-templates select="attr[@type=2]"/>
135       <xsl:apply-templates select="attr[@type=4]"/>
136       <xsl:apply-templates select="attr[@type=5]"/>
137       <xsl:apply-templates select="attr[@type=6]"/>
138       <attr type="7" value="2"/>
139       <attr type="8" value="5"/>
140       <xsl:apply-templates select="attr[@type=9]"/>
141       <xsl:apply-templates select="term"/>
142    </apt>
143   </xsl:template>
144
145
146
147
148 </xsl:stylesheet>
149
150