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 | |
---|
51 | <xsl:template match="triple"> |
---|
52 | <xsl:message>XCQL: <xsl:copy-of select="." /></xsl:message> |
---|
53 | <xsl:choose> |
---|
54 | <!-- this is rather hacky yet |
---|
55 | currently not used, because collection is a separate parameter and does not show up in the cql |
---|
56 | aim is: special handling of collection-index (as separate parameter) |
---|
57 | |
---|
58 | precondition: collection-index just as rightOperand on top-nesting-level and boolean='and' |
---|
59 | --> |
---|
60 | <xsl:when test="rightOperand/searchClause/index='collection'" > |
---|
61 | <xsl:apply-templates select="leftOperand/*" /> |
---|
62 | <xsl:apply-templates select="rightOperand/*" mode="collection"/> |
---|
63 | </xsl:when> |
---|
64 | <xsl:otherwise> |
---|
65 | <xsl:if test="not(ancestor::triple)" >//CMD/*[</xsl:if> |
---|
66 | <xsl:call-template name="boolean" > |
---|
67 | <xsl:with-param name="op" select="boolean/value" /> |
---|
68 | <xsl:with-param name="left" > |
---|
69 | <xsl:apply-templates select="leftOperand/*" /> |
---|
70 | </xsl:with-param> |
---|
71 | <xsl:with-param name="right" > |
---|
72 | <xsl:apply-templates select="rightOperand/*" /> |
---|
73 | </xsl:with-param> |
---|
74 | </xsl:call-template> |
---|
75 | <xsl:if test="not(ancestor::triple)" >]</xsl:if> |
---|
76 | </xsl:otherwise> |
---|
77 | </xsl:choose> |
---|
78 | |
---|
79 | </xsl:template> |
---|
80 | |
---|
81 | <xsl:template name="boolean"> |
---|
82 | <xsl:param name="op" /> |
---|
83 | <xsl:param name="left" /> |
---|
84 | <xsl:param name="right" /> |
---|
85 | <xsl:message>XCQL2XPath.template-boolean.$op:<xsl:value-of select="$op" /></xsl:message> |
---|
86 | |
---|
87 | |
---|
88 | <xsl:choose> |
---|
89 | <xsl:when test="$op='and'"> |
---|
90 | <xsl:text></xsl:text> |
---|
91 | <xsl:value-of select="$left" /> |
---|
92 | <xsl:text>][</xsl:text><xsl:value-of select="$right" /><xsl:text></xsl:text> |
---|
93 | </xsl:when> |
---|
94 | <xsl:when test="$op='or'"> |
---|
95 | <xsl:value-of select="$left" /> |
---|
96 | <xsl:text>%20or%20</xsl:text><xsl:value-of select="$right" /> |
---|
97 | </xsl:when> |
---|
98 | </xsl:choose> |
---|
99 | |
---|
100 | |
---|
101 | </xsl:template> |
---|
102 | |
---|
103 | <xsl:template match="searchClause"> |
---|
104 | <xsl:apply-templates select="index" /> |
---|
105 | <xsl:apply-templates select="relation"> |
---|
106 | <xsl:with-param name="term" select="term" /> |
---|
107 | </xsl:apply-templates> |
---|
108 | </xsl:template> |
---|
109 | |
---|
110 | |
---|
111 | <!-- aim is: special handling of collection-filter (as separate parameter) --> |
---|
112 | <xsl:template match="searchClause" mode="collection"> |
---|
113 | <xsl:text>&collection=</xsl:text><xsl:value-of select="term"></xsl:value-of> |
---|
114 | </xsl:template> |
---|
115 | |
---|
116 | <xsl:template match="index"> |
---|
117 | |
---|
118 | <!-- reverting the "escaping" of whitespaces in indices with datcat-name --> |
---|
119 | <xsl:variable name="ix_string" select="replace(text(),'_',' ')" /> |
---|
120 | |
---|
121 | <xsl:variable name="ix_resolved" > |
---|
122 | <xsl:choose> |
---|
123 | <xsl:when test="contains(.,':')"> |
---|
124 | <xsl:variable name="prefix" select="substring-before(.,':')" /> |
---|
125 | <xsl:variable name="termset" select="$terms_setup/Termsets/Termset[@id=$prefix]" /> |
---|
126 | |
---|
127 | <xsl:message>XCQL2XPath.index:<xsl:copy-of select="$termset"/> |
---|
128 | ix_string:<xsl:value-of select="$ix_string"/> |
---|
129 | </xsl:message> |
---|
130 | <xsl:choose> |
---|
131 | <!-- magic happening here --> |
---|
132 | <xsl:when test="$termset[@type='dcr' or @type='rr']"> |
---|
133 | <xsl:variable name="expanded_context" select="$terms_flat//context[@path=$ix_string]"></xsl:variable> |
---|
134 | <xsl:variable name="expanded_query" > |
---|
135 | <xsl:text>(</xsl:text> |
---|
136 | <xsl:for-each select="distinct-values($expanded_context//context[@elem]/@path)"> |
---|
137 | <xsl:variable name="prefix" select="substring-before(.,':')" /> |
---|
138 | <xsl:variable name="termset" select="$terms_setup/Termsets/Termset[@id=$prefix]" /> |
---|
139 | <xsl:value-of select="concat($termset/@name,'//',substring-after(.,':'))" /> |
---|
140 | <xsl:if test="position()!=last()"><xsl:text>|</xsl:text></xsl:if> |
---|
141 | </xsl:for-each> |
---|
142 | <xsl:text>)</xsl:text> |
---|
143 | </xsl:variable> |
---|
144 | <xsl:message>expanded-context:<xsl:value-of select="$expanded_query" /></xsl:message> |
---|
145 | <xsl:value-of select="$expanded_query" /> |
---|
146 | </xsl:when> |
---|
147 | <xsl:otherwise> |
---|
148 | <xsl:value-of select="concat($termset/@name,'//',substring-after(.,':'))" /> |
---|
149 | </xsl:otherwise> |
---|
150 | </xsl:choose> |
---|
151 | |
---|
152 | </xsl:when> |
---|
153 | <xsl:otherwise><xsl:value-of select="." /> |
---|
154 | </xsl:otherwise> |
---|
155 | </xsl:choose> |
---|
156 | |
---|
157 | </xsl:variable> |
---|
158 | <!-- <xsl:variable name="ix_xpathed" select="translate($ix_resolved, '.', '/')" ></xsl:variable> --> |
---|
159 | <xsl:variable name="ix_xpathed" select="my:index2xpath(.)" ></xsl:variable> |
---|
160 | <xsl:choose> |
---|
161 | <xsl:when test=".='cql.serverChoice' or .='*'" >//Components</xsl:when> |
---|
162 | <xsl:when test="ancestor::triple" > |
---|
163 | <xsl:value-of select="concat('.//',$ix_xpathed)" /> |
---|
164 | </xsl:when> |
---|
165 | <xsl:otherwise> |
---|
166 | <xsl:value-of select="concat('//',$ix_xpathed)" /> |
---|
167 | </xsl:otherwise> |
---|
168 | </xsl:choose> |
---|
169 | </xsl:template> |
---|
170 | |
---|
171 | <xsl:template match="relation"> |
---|
172 | <xsl:param name="term" /> |
---|
173 | |
---|
174 | <xsl:param name="sanitized_term" select="replace($term,' ','+')" /> |
---|
175 | <xsl:text>[</xsl:text> |
---|
176 | <xsl:choose> |
---|
177 | <xsl:when test="$term='*' or $term='_'" > |
---|
178 | <xsl:text>true()</xsl:text> |
---|
179 | </xsl:when> |
---|
180 | |
---|
181 | <xsl:when test="value='contains'" > |
---|
182 | <xsl:text>contains(.,'</xsl:text><xsl:value-of select="$sanitized_term"/><xsl:text>')</xsl:text> |
---|
183 | </xsl:when> |
---|
184 | <xsl:when test="(preceding-sibling::index='*' or preceding-sibling::index='cql.serverChoice') and contains($term,' ')" > |
---|
185 | <xsl:text>ft:query(.,<phrase></xsl:text><xsl:value-of select="lower-case($sanitized_term)" /><xsl:text></phrase>)</xsl:text> |
---|
186 | </xsl:when> |
---|
187 | <xsl:when test="value='any' or preceding-sibling::index='cql.serverChoice'" > |
---|
188 | <!-- use full-text querying: [ft:query(., "language")] --> |
---|
189 | <xsl:text>ft:query(.,'</xsl:text><xsl:value-of select="lower-case($sanitized_term)"/><xsl:text>')</xsl:text> |
---|
190 | |
---|
191 | </xsl:when> |
---|
192 | <xsl:otherwise> |
---|
193 | <xsl:text>.</xsl:text> <xsl:value-of select="value" /> |
---|
194 | <xsl:text>'</xsl:text><xsl:value-of select="$sanitized_term"/><xsl:text>'</xsl:text> |
---|
195 | </xsl:otherwise> |
---|
196 | </xsl:choose> |
---|
197 | <xsl:text>]</xsl:text> |
---|
198 | |
---|
199 | </xsl:template> |
---|
200 | |
---|
201 | |
---|
202 | </xsl:stylesheet> |
---|