]>
Commit | Line | Data |
---|---|---|
c005fb28 MW |
1 | <?xml version="1.0" encoding="utf-8"?> |
2 | ||
3 | <!-- | |
4 | Name: embedded.xsl | |
5 | Purpose: Embedded XSLT | |
6 | Author: Mike Wetherell | |
7 | RCS-ID: $Id$ | |
8 | Copyright: (c) 2007 Mike Wetherell | |
9 | Licence: wxWidgets licence | |
10 | --> | |
11 | ||
12 | <transform xmlns="http://www.w3.org/1999/XSL/Transform" | |
13 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
14 | xmlns:XSL="XSL" | |
15 | version="1.0"> | |
16 | ||
17 | <namespace-alias stylesheet-prefix="XSL" result-prefix="xsl"/> | |
18 | ||
19 | <output indent="yes"/> | |
20 | ||
21 | <variable | |
22 | name="top-level-base" | |
23 | select="/*/xsl:import | | |
24 | /*/xsl:include | | |
25 | /*/xsl:attribute-set | | |
26 | /*/xsl:character-map | | |
27 | /*/xsl:decimal-format | | |
28 | /*/xsl:function | | |
29 | /*/xsl:import-schema | | |
30 | /*/xsl:key | | |
31 | /*/xsl:namespace-alias | | |
32 | /*/xsl:output | | |
33 | /*/xsl:param | | |
34 | /*/xsl:preserve-space | | |
35 | /*/xsl:strip-space | | |
36 | /*/xsl:variable"/> | |
37 | ||
38 | <variable | |
39 | name="top-level" | |
40 | select="$top-level-base | | |
41 | /*/xsl:template"/> | |
42 | ||
43 | <variable | |
44 | name="top-level-copy" | |
45 | select="$top-level-base | | |
46 | /*/xsl:template | |
47 | [not(following-sibling::xsl:template/@name = @name)]"/> | |
48 | ||
49 | <template match="/"> | |
50 | <XSL:transform> | |
51 | <copy-of select="/*/namespace::*"/> | |
52 | ||
53 | <for-each select="/*/@xsl:*"> | |
54 | <attribute name="{local-name()}"> | |
55 | <value-of select="."/> | |
56 | </attribute> | |
57 | </for-each> | |
58 | ||
59 | <apply-templates mode="copy-xsl" select="$top-level-copy"/> | |
60 | <apply-templates select="*"/> | |
61 | </XSL:transform> | |
62 | </template> | |
63 | ||
64 | <template match="*"> | |
65 | <variable name="pattern"> | |
66 | <call-template name="coord-pattern"/> | |
67 | </variable> | |
68 | ||
69 | <XSL:template match="*[generate-id() = generate-id(document('', /){$pattern})]"> | |
70 | <copy-of select="namespace::*"/> | |
71 | <choose> | |
72 | <when test="/*/xsl:template[@name = name(current())]"> | |
73 | <call-template name="expand"/> | |
74 | </when> | |
75 | <when test="count($top-level | .) != count($top-level)"> | |
76 | <call-template name="create-context"/> | |
77 | </when> | |
78 | </choose> | |
79 | </XSL:template> | |
80 | ||
81 | <if test="not(self::xsl:*)"> | |
82 | <apply-templates select="node()"/> | |
83 | </if> | |
84 | </template> | |
85 | ||
86 | <template match="text()"/> | |
87 | ||
88 | <template match="@*"> | |
89 | <copy/> | |
90 | </template> | |
91 | ||
92 | <template mode="copy-xsl" match="*[/*/xsl:template/@name = name()]"> | |
93 | <choose> | |
94 | <when test="ancestor::xsl:template[@name = name(current())]"> | |
95 | <XSL:choose> | |
96 | <XSL:when test="name() = '{name()}'"> | |
97 | <XSL:copy> | |
98 | <call-template name="copy-xsl-children"/> | |
99 | </XSL:copy> | |
100 | </XSL:when> | |
101 | <XSL:otherwise> | |
102 | <copy> | |
103 | <call-template name="copy-xsl-children"/> | |
104 | </copy> | |
105 | </XSL:otherwise> | |
106 | </XSL:choose> | |
107 | </when> | |
108 | <otherwise> | |
109 | <call-template name="expand"/> | |
110 | </otherwise> | |
111 | </choose> | |
112 | </template> | |
113 | ||
114 | <template mode="copy-xsl" match="@*|node()"> | |
115 | <copy> | |
116 | <call-template name="copy-xsl-children"/> | |
117 | </copy> | |
118 | </template> | |
119 | ||
120 | <template name="copy-xsl-children"> | |
121 | <copy-of select="namespace::*"/> | |
122 | <apply-templates mode="copy-xsl" select="@*|node()"/> | |
123 | </template> | |
124 | ||
125 | <template name="coord-pattern"> | |
126 | <param name="element" select="."/> | |
127 | ||
128 | <for-each select="$element/ancestor-or-self::*"> | |
129 | <text>/*[</text> | |
130 | <value-of select="count(preceding-sibling::*) + 1"/> | |
131 | <text>]</text> | |
132 | </for-each> | |
133 | </template> | |
134 | ||
135 | <template name="expand"> | |
136 | <variable name="params" select="/*/xsl:template[@name = name(current())]/xsl:param"/> | |
137 | ||
138 | <XSL:call-template name="{name()}"> | |
139 | <if test="$params"> | |
140 | <XSL:with-param name="{$params[1]/@name}"> | |
141 | <choose> | |
142 | <when test="ancestor-or-self::xsl:*"> | |
143 | <apply-templates mode="copy-xsl" select="node()"/> | |
144 | </when> | |
145 | <otherwise> | |
146 | <variable name="pattern"> | |
147 | <call-template name="coord-pattern"/> | |
148 | </variable> | |
149 | <XSL:apply-templates select="{$pattern}/node()"/> | |
150 | </otherwise> | |
151 | </choose> | |
152 | </XSL:with-param> | |
153 | </if> | |
154 | ||
155 | <for-each select="@*"> | |
156 | <XSL:with-param name="{name()}"> | |
157 | <call-template name="avt"> | |
158 | <with-param name="string" select="."/> | |
159 | </call-template> | |
160 | </XSL:with-param> | |
161 | </for-each> | |
162 | </XSL:call-template> | |
163 | </template> | |
164 | ||
165 | <template name="create-context"> | |
166 | <param name="elements" select="ancestor-or-self::* | |
167 | [last() - 1 > position()] | |
168 | /preceding-sibling::xsl:variable | ."/> | |
169 | ||
170 | <variable name="pattern"> | |
171 | <call-template name="coord-pattern"> | |
172 | <with-param name="element" select="$elements[1]"/> | |
173 | </call-template> | |
174 | </variable> | |
175 | ||
176 | <variable name="parent-pattern"> | |
177 | <call-template name="coord-pattern"> | |
178 | <with-param name="element" select="$elements[1]/.."/> | |
179 | </call-template> | |
180 | </variable> | |
181 | ||
182 | <XSL:for-each select="{$parent-pattern}/*"> | |
183 | <XSL:if test="count({$pattern} | .) = 1"> | |
184 | <choose> | |
185 | <when test="$elements[1]/self::xsl:*"> | |
186 | <apply-templates mode="copy-xsl" select="$elements[1]"/> | |
187 | <if test="$elements[2]"> | |
188 | <call-template name="create-context"> | |
189 | <with-param name="elements" select="$elements[position() > 1]"/> | |
190 | </call-template> | |
191 | </if> | |
192 | </when> | |
193 | <otherwise> | |
194 | <copy> | |
195 | <apply-templates select="@*"/> | |
196 | <if test="node()"> | |
197 | <XSL:apply-templates select="node()"/> | |
198 | </if> | |
199 | </copy> | |
200 | </otherwise> | |
201 | </choose> | |
202 | </XSL:if> | |
203 | </XSL:for-each> | |
204 | </template> | |
205 | ||
206 | <template name="before"> | |
207 | <param name="string"/> | |
208 | <param name="target"/> | |
209 | ||
210 | <variable name="apos">'</variable> | |
211 | <variable name="quot">"</variable> | |
212 | ||
213 | <variable name="posapos" select="string-length(substring-before( | |
214 | concat($string, $apos), $apos))"/> | |
215 | <variable name="posquot" select="string-length(substring-before( | |
216 | concat($string, $quot), $quot))"/> | |
217 | <variable name="postarg" select="string-length(substring-before( | |
218 | concat($string, $target), $target))"/> | |
219 | ||
220 | <choose> | |
221 | <when test="$posapos = $postarg and $posquot = $postarg"> | |
222 | <value-of select="$string"/> | |
223 | </when> | |
224 | <when test="$posapos > $postarg and $posquot > $postarg"> | |
225 | <value-of select="substring($string, 1, $postarg)"/> | |
226 | </when> | |
227 | <otherwise> | |
228 | <variable name="delim"> | |
229 | <choose> | |
230 | <when test="$posquot > $posapos">'</when> | |
231 | <otherwise>"</otherwise> | |
232 | </choose> | |
233 | </variable> | |
234 | ||
235 | <value-of select="substring-before($string, $delim)"/> | |
236 | <value-of select="$delim"/> | |
237 | ||
238 | <variable name="mid" select="substring-after($string, $delim)"/> | |
239 | ||
240 | <choose> | |
241 | <when test="contains($mid, $delim)"> | |
242 | <value-of select="substring-before($mid, $delim)"/> | |
243 | <value-of select="$delim"/> | |
244 | ||
245 | <call-template name="before"> | |
246 | <with-param name="string" | |
247 | select="substring-after($mid, $delim)"/> | |
248 | <with-param name="target" | |
249 | select="$target"/> | |
250 | </call-template> | |
251 | </when> | |
252 | <otherwise> | |
253 | <value-of select="$mid"/> | |
254 | </otherwise> | |
255 | </choose> | |
256 | </otherwise> | |
257 | </choose> | |
258 | </template> | |
259 | ||
260 | <template name="avt"> | |
261 | <param name="string"/> | |
262 | ||
263 | <variable name="before1" select="substring-before(concat($string, '{'), '{')"/> | |
264 | <variable name="len1" select="string-length($before1)"/> | |
265 | ||
266 | <variable name="before2" select="substring-before(concat($string, '}}'), '}}')"/> | |
267 | <variable name="len2" select="string-length($before2)"/> | |
268 | ||
269 | <choose> | |
270 | <when test="$before1 = $string and $before2 = $string"> | |
271 | <value-of select="$string"/> | |
272 | </when> | |
273 | <when test="$len2 < $len1"> | |
274 | <value-of select="$before2"/> | |
275 | <text>}</text> | |
276 | <call-template name="avt"> | |
277 | <with-param name="string" select="substring($string, $len2 + 3)"/> | |
278 | </call-template> | |
279 | </when> | |
280 | <when test="substring($string, $len1 + 2, 1) = '{'"> | |
281 | <value-of select="$before1"/> | |
282 | <text>{</text> | |
283 | <call-template name="avt"> | |
284 | <with-param name="string" select="substring($string, $len1 + 3)"/> | |
285 | </call-template> | |
286 | </when> | |
287 | <otherwise> | |
288 | <variable name="mid" select="substring($string, $len1 + 2)"/> | |
289 | <variable name="expr"> | |
290 | <call-template name="before"> | |
291 | <with-param name="string" select="$mid"/> | |
292 | <with-param name="target">}</with-param> | |
293 | </call-template> | |
294 | </variable> | |
295 | <value-of select="$before1"/> | |
296 | <XSL:copy-of select="{$expr}"/> | |
297 | <call-template name="avt"> | |
298 | <with-param name="string" select="substring($mid, string-length($expr) + 2)"/> | |
299 | </call-template> | |
300 | </otherwise> | |
301 | </choose> | |
302 | </template> | |
303 | ||
304 | </transform> |