1 | <?xml version="1.0" encoding="utf-8"?> |
---|
2 | <xsl:stylesheet |
---|
3 | version="2.0" |
---|
4 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="myFunctions"> |
---|
5 | |
---|
6 | <!-- |
---|
7 | <purpose>transform XCQL to xpath |
---|
8 | ! WARNING: not pure Xpath! actually it adapts the query on the query-interface of MD-Repository |
---|
9 | in particular there is special handling for index "collection" |
---|
10 | </purpose> |
---|
11 | <history> |
---|
12 | <change on="2010-01-28" type="created" by="vr"></change> |
---|
13 | <change on="2010-07-01" type="changed" by="vr">handling of collection-filter</change> |
---|
14 | </history> |
---|
15 | <sample> |
---|
16 | <triple> |
---|
17 | <boolean> |
---|
18 | <value>and</value> |
---|
19 | </boolean> |
---|
20 | <leftOperand> |
---|
21 | <searchClause> |
---|
22 | <index>bath.author</index> |
---|
23 | <relation> |
---|
24 | <value>any</value> |
---|
25 | </relation> |
---|
26 | <term>fish</term> |
---|
27 | </searchClause> |
---|
28 | </leftOperand> |
---|
29 | <rightOperand> |
---|
30 | <searchClause> |
---|
31 | <index>dc.title</index> |
---|
32 | <relation> |
---|
33 | <value>all</value> |
---|
34 | </relation> |
---|
35 | <term>cat dog</term> |
---|
36 | </searchClause> |
---|
37 | </rightOperand> |
---|
38 | </triple> |
---|
39 | </sample> |
---|
40 | --> |
---|
41 | |
---|
42 | |
---|
43 | <xsl:include href="cmd_functions.xsl"/> |
---|
44 | <!--<xsl:include href="params.xsl"/> |
---|
45 | <xsl:variable name="terms_setup" select="document($terms_setup_uri)" /> |
---|
46 | <xsl:variable name="terms_flat" select="document($terms_flat_uri)" /> |
---|
47 | --> |
---|
48 | <xsl:output method="text" /> |
---|
49 | |
---|
50 | <xsl:template match="/"> |
---|
51 | <xsl:message>XCQL: <xsl:copy-of select="." /></xsl:message> |
---|
52 | <xsl:apply-templates/> |
---|
53 | </xsl:template> |
---|
54 | |
---|
55 | <xsl:template match="triple"> |
---|
56 | |
---|
57 | <xsl:choose> |
---|
58 | <!-- this is rather hacky yet |
---|
59 | currently not used, because collection is a separate parameter and does not show up in the cql |
---|
60 | aim is: special handling of collection-index (as separate parameter) |
---|
61 | |
---|
62 | precondition: collection-index just as rightOperand on top-nesting-level and boolean='and' |
---|
63 | --> |
---|
64 | <xsl:when test="rightOperand/searchClause/index='collection'" > |
---|
65 | <xsl:apply-templates select="leftOperand/*" /> |
---|
66 | <xsl:apply-templates select="rightOperand/*" mode="collection"/> |
---|
67 | </xsl:when> |
---|
68 | <xsl:otherwise> |
---|
69 | <xsl:if test="not(ancestor::triple)" >//Components[</xsl:if> |
---|
70 | <xsl:call-template name="boolean" > |
---|
71 | <xsl:with-param name="op" select="boolean/value" /> |
---|
72 | <xsl:with-param name="left" > |
---|
73 | <xsl:apply-templates select="leftOperand/*" /> |
---|
74 | </xsl:with-param> |
---|
75 | <xsl:with-param name="right" > |
---|
76 | <xsl:apply-templates select="rightOperand/*" /> |
---|
77 | </xsl:with-param> |
---|
78 | </xsl:call-template> |
---|
79 | <xsl:if test="not(ancestor::triple)" >]</xsl:if> |
---|
80 | </xsl:otherwise> |
---|
81 | </xsl:choose> |
---|
82 | |
---|
83 | </xsl:template> |
---|
84 | |
---|
85 | <xsl:template name="boolean"> |
---|
86 | <xsl:param name="op" /> |
---|
87 | <xsl:param name="left" /> |
---|
88 | <xsl:param name="right" /> |
---|
89 | <xsl:message>XCQL2XPath.template-boolean.$op:<xsl:value-of select="$op" /></xsl:message> |
---|
90 | |
---|
91 | |
---|
92 | <xsl:choose> |
---|
93 | <xsl:when test="$op='and'"> |
---|
94 | <xsl:text></xsl:text> |
---|
95 | <xsl:value-of select="$left" /> |
---|
96 | <xsl:text>][</xsl:text><xsl:value-of select="$right" /><xsl:text></xsl:text> |
---|
97 | </xsl:when> |
---|
98 | <xsl:when test="$op='or'"> |
---|
99 | <xsl:value-of select="$left" /> |
---|
100 | <xsl:text>%20or%20</xsl:text><xsl:value-of select="$right" /> |
---|
101 | </xsl:when> |
---|
102 | </xsl:choose> |
---|
103 | |
---|
104 | |
---|
105 | </xsl:template> |
---|
106 | |
---|
107 | <xsl:template match="searchClause"> |
---|
108 | <xsl:apply-templates select="index" /> |
---|
109 | <xsl:apply-templates select="relation"> |
---|
110 | <xsl:with-param name="term" select="term" /> |
---|
111 | </xsl:apply-templates> |
---|
112 | </xsl:template> |
---|
113 | |
---|
114 | |
---|
115 | <!-- aim is: special handling of collection-filter (as separate parameter) --> |
---|
116 | <xsl:template match="searchClause" mode="collection"> |
---|
117 | <xsl:text>&collection=</xsl:text><xsl:value-of select="term"></xsl:value-of> |
---|
118 | </xsl:template> |
---|
119 | |
---|
120 | <xsl:template match="index"> |
---|
121 | |
---|
122 | <!-- reverting the "escaping" of whitespaces in indices with datcat-name --> |
---|
123 | <xsl:variable name="ix_string" select="replace(text(),'_',' ')" /> |
---|
124 | |
---|
125 | <xsl:variable name="ix_resolved" > |
---|
126 | <xsl:choose> |
---|
127 | <xsl:when test="contains(.,':')"> |
---|
128 | <xsl:variable name="prefix" select="substring-before(.,':')" /> |
---|
129 | <xsl:variable name="termset" select="$terms_setup/Termsets/Termset[@id=$prefix]" /> |
---|
130 | |
---|
131 | <xsl:message>XCQL2XPath.index:<xsl:copy-of select="$termset"/> |
---|
132 | ix_string:<xsl:value-of select="$ix_string"/> |
---|
133 | </xsl:message> |
---|
134 | <xsl:choose> |
---|
135 | <!-- magic happening here --> |
---|
136 | <xsl:when test="$termset[@type='dcr' or @type='rr']"> |
---|
137 | <xsl:variable name="expanded_context" select="$terms_flat//context[@path=$ix_string]"></xsl:variable> |
---|
138 | <xsl:variable name="expanded_query" > |
---|
139 | <xsl:text>(</xsl:text> |
---|
140 | <xsl:for-each select="distinct-values($expanded_context//context[@elem]/@path)"> |
---|
141 | <xsl:variable name="prefix" select="substring-before(.,':')" /> |
---|
142 | <xsl:variable name="termset" select="$terms_setup/Termsets/Termset[@id=$prefix]" /> |
---|
143 | <xsl:value-of select="concat($termset/@name,'//',substring-after(.,':'))" /> |
---|
144 | <xsl:if test="position()!=last()"><xsl:text>|</xsl:text></xsl:if> |
---|
145 | </xsl:for-each> |
---|
146 | <xsl:text>)</xsl:text> |
---|
147 | </xsl:variable> |
---|
148 | <xsl:message>expanded-context:<xsl:value-of select="$expanded_query" /></xsl:message> |
---|
149 | <xsl:value-of select="$expanded_query" /> |
---|
150 | </xsl:when> |
---|
151 | <xsl:otherwise> |
---|
152 | <xsl:value-of select="concat($termset/@name,'//',substring-after(.,':'))" /> |
---|
153 | </xsl:otherwise> |
---|
154 | </xsl:choose> |
---|
155 | |
---|
156 | </xsl:when> |
---|
157 | <xsl:otherwise><xsl:value-of select="." /> |
---|
158 | </xsl:otherwise> |
---|
159 | </xsl:choose> |
---|
160 | |
---|
161 | </xsl:variable> |
---|
162 | <!-- <xsl:variable name="ix_xpathed" select="translate($ix_resolved, '.', '/')" ></xsl:variable> --> |
---|
163 | <xsl:variable name="ix_xpathed" select="my:index2xpath(.)" ></xsl:variable> |
---|
164 | <xsl:choose> |
---|
165 | <xsl:when test="(.='cql.serverChoice' or .='*') and not(ancestor::triple)" >//Components</xsl:when> |
---|
166 | <xsl:when test=".='cql.serverChoice' or .='*'" >.</xsl:when> |
---|
167 | <xsl:when test="ancestor::triple" > |
---|
168 | <xsl:value-of select="concat('.//',$ix_xpathed)" /> |
---|
169 | </xsl:when> |
---|
170 | <xsl:otherwise> |
---|
171 | <xsl:value-of select="concat('//',$ix_xpathed)" /> |
---|
172 | </xsl:otherwise> |
---|
173 | </xsl:choose> |
---|
174 | </xsl:template> |
---|
175 | |
---|
176 | <xsl:template match="relation"> |
---|
177 | <xsl:param name="term" /> |
---|
178 | |
---|
179 | <xsl:param name="sanitized_term" select="replace($term,' ','+')" /> |
---|
180 | <xsl:text>[</xsl:text> |
---|
181 | <xsl:choose> |
---|
182 | <xsl:when test="$term='*' or $term='_'" > |
---|
183 | <xsl:text>true()</xsl:text> |
---|
184 | </xsl:when> |
---|
185 | |
---|
186 | <xsl:when test="value='contains'" > |
---|
187 | <xsl:text>contains(.,'</xsl:text><xsl:value-of select="$sanitized_term"/><xsl:text>')</xsl:text> |
---|
188 | </xsl:when> |
---|
189 | <xsl:when test="(value='any' or preceding-sibling::index='*' or preceding-sibling::index='cql.serverChoice')" > |
---|
190 | <!-- use full-text querying: [ft:query(., "language")] --> |
---|
191 | <xsl:choose> |
---|
192 | <xsl:when test="starts-with($term,'%22')" > |
---|
193 | <xsl:text>ft:query(.,<phrase></xsl:text><xsl:value-of select="replace(lower-case($sanitized_term),'%22','')" /><xsl:text></phrase>)</xsl:text> |
---|
194 | </xsl:when> |
---|
195 | <xsl:when test="contains($term,'%7C')" > |
---|
196 | <xsl:text>ft:query(.,'</xsl:text><xsl:value-of select="replace(lower-case($sanitized_term),'%7C','')"/><xsl:text>')</xsl:text> |
---|
197 | </xsl:when> |
---|
198 | <xsl:when test="contains($term,'%20') or contains($sanitized_term,'+')" > <!-- AND-combined full-text search--> |
---|
199 | <xsl:for-each select="tokenize(replace($sanitized_term,'\+','%20'),'%20')" > |
---|
200 | <xsl:if test=".!=''" > |
---|
201 | <xsl:text>ft:query(.,'</xsl:text><xsl:value-of select="."/><xsl:text>')</xsl:text> |
---|
202 | <xsl:message><xsl:value-of select="concat(position(),':', .)" ></xsl:value-of></xsl:message> |
---|
203 | <xsl:if test="position()!=last()" ><xsl:text>][</xsl:text></xsl:if> |
---|
204 | </xsl:if> |
---|
205 | </xsl:for-each> |
---|
206 | </xsl:when> |
---|
207 | <xsl:otherwise> |
---|
208 | <xsl:text>ft:query(.,'</xsl:text><xsl:value-of select="lower-case($sanitized_term)"/><xsl:text>')</xsl:text> |
---|
209 | </xsl:otherwise> |
---|
210 | </xsl:choose> |
---|
211 | </xsl:when> |
---|
212 | <xsl:when test="value='='" > |
---|
213 | <xsl:text>.%20eq%20</xsl:text> <xsl:text>'</xsl:text><xsl:value-of select="$sanitized_term"/><xsl:text>'</xsl:text> |
---|
214 | </xsl:when> |
---|
215 | <xsl:otherwise> |
---|
216 | <xsl:text>.</xsl:text> <xsl:value-of select="value" /> |
---|
217 | <xsl:text>'</xsl:text><xsl:value-of select="$sanitized_term"/><xsl:text>'</xsl:text> |
---|
218 | </xsl:otherwise> |
---|
219 | </xsl:choose> |
---|
220 | <xsl:text>]</xsl:text> |
---|
221 | |
---|
222 | </xsl:template> |
---|
223 | |
---|
224 | |
---|
225 | </xsl:stylesheet> |
---|