|
1 <?xml version="1.0"?> |
|
2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
|
3 xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt"> |
|
4 <!--Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
5 All rights reserved. |
|
6 This component and the accompanying materials are made available |
|
7 under the terms of the License "Eclipse Public License v1.0" |
|
8 which accompanies this distribution, and is available |
|
9 at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
10 |
|
11 Initial Contributors: |
|
12 Nokia Corporation - initial contribution. |
|
13 Contributors: |
|
14 Description: |
|
15 XSLT module which contains the logic to join a system definition file |
|
16 --> |
|
17 <!-- save SF namespace as a constant to avoid the risk of typos--> |
|
18 <xsl:variable name="defaultns">http://www.symbian.org/system-definition</xsl:variable> |
|
19 |
|
20 <!-- create a stand-alone sysdef from a linked set of fragments --> |
|
21 |
|
22 <xsl:template match="/*" mode="join"> |
|
23 <xsl:message terminate="yes">ERROR: Cannot process this document</xsl:message> |
|
24 </xsl:template> |
|
25 |
|
26 |
|
27 <xsl:template match="/SystemDefinition[@schema='3.0.0' and count(*)=1]" mode="join"> |
|
28 <xsl:param name="origin" select="/.."/> |
|
29 <xsl:param name="root"/> |
|
30 <xsl:param name="filename"/> |
|
31 <xsl:param name="namespaces"/> |
|
32 <xsl:param name="data" select="/.."/> |
|
33 <xsl:choose> |
|
34 <xsl:when test="$origin"> <!-- this sysdef fragment was linked from a parent sysdef --> |
|
35 <xsl:for-each select="*"> <!-- can be only one --> |
|
36 <xsl:variable name="upid"><xsl:apply-templates select="$origin/@id" mode="my-id"/></xsl:variable> <!-- namespaceless ID of this in parent doc --> |
|
37 <xsl:variable name="id"><xsl:apply-templates select="@id" mode="my-id"/></xsl:variable> <!-- namespaceless ID of this here --> |
|
38 <xsl:variable name="upns"><xsl:apply-templates select="$origin/@id" mode="my-namespace"/></xsl:variable> <!-- ID's namespace in parent doc --> |
|
39 <xsl:variable name="ns"><xsl:apply-templates select="@id" mode="my-namespace"/></xsl:variable> <!-- ID's namespace --> |
|
40 <xsl:if test="$id!=$upid or $ns!=$upns"> |
|
41 <xsl:message terminate="yes">ERROR: Linked ID "<xsl:value-of select="$id"/>" (<xsl:value-of select="$ns"/>) must match linking document "<xsl:value-of select="$upid"/>" (<xsl:value-of select="$upns"/>)</xsl:message> |
|
42 </xsl:if> |
|
43 <!-- copy any attributes not already defined (parent doc overrides child doc)--> |
|
44 <xsl:for-each select="@*"> |
|
45 <xsl:variable name="n" select="name()"/> |
|
46 <xsl:choose> |
|
47 <xsl:when test="$n='id'"/> <!-- never copy this, always set --> |
|
48 <xsl:when test="$origin/@*[name()=$n]"> <!-- don't copy if already set --> |
|
49 <xsl:message>Cannot set "<xsl:value-of select="$n"/>", already set</xsl:message> |
|
50 </xsl:when> |
|
51 <xsl:when test="$n='before'"> |
|
52 <!-- ensure ns is correct (if any future attribtues will ever use an ID, process it here too)--> |
|
53 <xsl:apply-templates select="." mode="join"> |
|
54 <xsl:with-param name="namespaces" select="$namespaces"/> |
|
55 </xsl:apply-templates> |
|
56 </xsl:when> |
|
57 <xsl:otherwise><xsl:copy-of select="."/></xsl:otherwise> <!-- just copy anything else --> |
|
58 </xsl:choose> |
|
59 </xsl:for-each> |
|
60 <xsl:copy-of select="../namespace::*[not(.=$namespaces)]"/> <!-- set any namespaces not already set (they should all alreayd be, but some XSLT processors are quirky) --> |
|
61 <xsl:apply-templates select="$data" mode="overlay-attributes"> |
|
62 <xsl:with-param name="item" select="current()"/> |
|
63 </xsl:apply-templates> |
|
64 <xsl:variable name="content"> |
|
65 <xsl:apply-templates select="*|comment()" mode="join"> |
|
66 <xsl:with-param name="root" select="$root"/> |
|
67 <xsl:with-param name="filename" select="$filename"/> |
|
68 <xsl:with-param name="data" select="$data"/> |
|
69 <xsl:with-param name="namespaces" select="$namespaces | ../namespace::*[not(.=$namespaces)]"/> |
|
70 </xsl:apply-templates> |
|
71 </xsl:variable> |
|
72 <xsl:apply-templates select="." mode="is-content-filtered"> <!-- optionally add filtered="yes" if some content has been removed --> |
|
73 <xsl:with-param name="content" select="$content"/> |
|
74 </xsl:apply-templates> |
|
75 <xsl:apply-templates select="$data" mode="overlay-meta"> |
|
76 <xsl:with-param name="item" select="current()"/> |
|
77 </xsl:apply-templates> |
|
78 <xsl:copy-of select="$content"/> |
|
79 </xsl:for-each> |
|
80 </xsl:when> |
|
81 <xsl:when test="function-available('exslt:node-set')"> <!-- this is the root element of a root sysdef --> |
|
82 <!--try to put all namespaces in root element --> |
|
83 <xsl:variable name="nss"> |
|
84 <!-- contains node set of namespaces to add to root element. |
|
85 May panic if there are too many single-letter namespaces and this can't create a new one --> |
|
86 <xsl:call-template name="needed-namespaces"> |
|
87 <xsl:with-param name="foundns"> |
|
88 <xsl:apply-templates select="//*[(self::component or self::collection or self::package or self::layer) and @href]" mode="scan-for-namespaces"/> |
|
89 </xsl:with-param> |
|
90 </xsl:call-template> |
|
91 </xsl:variable> |
|
92 <xsl:variable name="ns" select="@id-namespace | namespace::* | exslt:node-set($nss)/*"/> |
|
93 <xsl:copy><xsl:copy-of select="@*"/> |
|
94 <xsl:apply-templates select="self::*[not(@id-namespace)]" mode="add-id-ns"/> |
|
95 <xsl:for-each select="exslt:node-set($nss)/*"> <!-- add namespace definitions --> |
|
96 <xsl:attribute name="xmlns:{name()}"> |
|
97 <xsl:value-of select="."/> |
|
98 </xsl:attribute> |
|
99 </xsl:for-each> |
|
100 <!-- no need to call is-content-filtered, it never will be from this element --> |
|
101 <xsl:apply-templates select="*|comment()" mode="join"> |
|
102 <xsl:with-param name="namespaces" select="$ns"/> |
|
103 <xsl:with-param name="root" select="$root"/> |
|
104 <xsl:with-param name="data" select="$data"/> |
|
105 <xsl:with-param name="filename" select="$filename"/> |
|
106 </xsl:apply-templates> |
|
107 </xsl:copy> |
|
108 </xsl:when> |
|
109 <xsl:otherwise> <!-- can't handle node-set() so put the namespaces in the document instead of the root element--> |
|
110 <xsl:variable name="ns" select="@id-namespace | namespace::*"/> |
|
111 <xsl:copy><xsl:copy-of select="@*"/> |
|
112 <!-- no need to call is-content-filtered, it never will be from this element --> |
|
113 <xsl:apply-templates select="*|comment()" mode="join"> |
|
114 <xsl:with-param name="namespaces" select="$ns"/> |
|
115 <xsl:with-param name="root" select="$root"/> |
|
116 <xsl:with-param name="data" select="$data"/> |
|
117 <xsl:with-param name="filename" select="$filename"/> |
|
118 </xsl:apply-templates> |
|
119 </xsl:copy> |
|
120 </xsl:otherwise> |
|
121 </xsl:choose> |
|
122 </xsl:template> |
|
123 |
|
124 <xsl:template match="*" mode="scan-for-namespaces"/> <!-- just in case of errors, consider replacing by terminate --> |
|
125 <xsl:template match="*[@href and not(self::meta)]" mode="scan-for-namespaces"> |
|
126 <!-- produce a list of namespace-prefix namespace pairs separated by newlines, in reverse order found in documents |
|
127 reverse order so we can try to use the first namespace prefix defined if it's available--> |
|
128 <xsl:for-each select="document(@href,.)/*"> |
|
129 <xsl:apply-templates select="//*[(self::component or self::collection or self::package or self::layer) and @href]" mode="scan-for-namespaces"/> |
|
130 <xsl:for-each select="//namespace::* | @id-namespace"> |
|
131 <xsl:value-of select="concat(name(),' ',.,'
')"/> |
|
132 </xsl:for-each> |
|
133 </xsl:for-each> |
|
134 </xsl:template> |
|
135 |
|
136 <xsl:template name="needed-namespaces"> |
|
137 <xsl:param name="foundns"/> |
|
138 <xsl:param name="usedpre"/> |
|
139 |
|
140 <xsl:if test="$foundns!=''"> |
|
141 <xsl:variable name="line" select="substring-before($foundns,'
')"/> <!-- always has trailing newline --> |
|
142 <xsl:variable name="name" select="substring-after($line,' ')"/> <!-- namespace prefix --> |
|
143 <xsl:variable name="remainder" select="substring-after($foundns,'
')"/> |
|
144 <xsl:variable name="newprefix"> |
|
145 <xsl:if test="not(contains(concat('
',$remainder),concat('
',$line,'
'))) and |
|
146 not(//namespace::*[.=$name] or @id-namespace[.=$name] or (not(@id-namespace) and $defaultns=$name))"> |
|
147 <xsl:apply-templates select="." mode="ns-prefix"> |
|
148 <xsl:with-param name="ns" select="$name"/> |
|
149 <xsl:with-param name="pre" select="substring-before($line,' ')"/> |
|
150 <xsl:with-param name="dontuse" select="$usedpre"/> |
|
151 </xsl:apply-templates> |
|
152 </xsl:if> |
|
153 </xsl:variable> |
|
154 <xsl:if test="$newprefix!=''"> |
|
155 <!-- can treat this as if it were a namespace node --> |
|
156 <xsl:element name="{$newprefix}"> |
|
157 <xsl:value-of select="$name"/> |
|
158 </xsl:element> |
|
159 </xsl:if> |
|
160 <xsl:if test="$remainder!=''"> |
|
161 <xsl:call-template name="needed-namespaces"> |
|
162 <xsl:with-param name="foundns" select="$remainder"/> |
|
163 <xsl:with-param name="usedpre" select="concat($usedpre,' ',$newprefix,' ')"/> |
|
164 </xsl:call-template> |
|
165 </xsl:if> |
|
166 </xsl:if> |
|
167 </xsl:template> |
|
168 |
|
169 <xsl:template match="/SystemDefinition" mode="ns-prefix"> |
|
170 <!-- should be able to replace this with mechanism that uses the XSLT processor's own ability to generate namespaces --> |
|
171 <xsl:param name="ns"/> |
|
172 <xsl:param name="pre"/> |
|
173 <xsl:param name="dontuse"/> |
|
174 <xsl:param name="chars">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz</xsl:param> |
|
175 <xsl:variable name="name" select="substring(substring-after($ns,'http://www.'),1,1)"/> |
|
176 <xsl:choose> |
|
177 <xsl:when test="$pre!='' and $pre!='id-namespace' and not(//namespace::*[name()=$pre]) and not(contains($dontuse,concat(' ',$pre,' ')))"> |
|
178 <xsl:value-of select="$pre"/> |
|
179 </xsl:when> |
|
180 <xsl:when test="$ns='' and $chars=''"> |
|
181 <xsl:message terminate="yes">ERROR: Cannot create namespace prefix for downstream default namespace in <xsl:value-of select="*/@id"/></xsl:message> |
|
182 </xsl:when> |
|
183 <xsl:when test="$name!='' and not(contains($dontuse,concat(' ',$name,' ')))"><xsl:value-of select="$name"/></xsl:when> |
|
184 <xsl:when test="namespace::*[name()=substring($chars,1,1)] or contains($dontuse,concat(' ',substring($chars,1,1),' '))"> |
|
185 <xsl:apply-templates mode="ns-prefix"> |
|
186 <xsl:with-param name="chars" select="substring($chars,2)"/> |
|
187 </xsl:apply-templates> |
|
188 </xsl:when> |
|
189 <xsl:otherwise> |
|
190 <xsl:value-of select="substring($chars,1,1)"/> |
|
191 </xsl:otherwise> |
|
192 </xsl:choose> |
|
193 </xsl:template> |
|
194 |
|
195 |
|
196 <xsl:template match="unit" mode="join"> <xsl:param name="root"/><xsl:param name="filename"/><xsl:param name="data"/> |
|
197 <xsl:variable name="display"> |
|
198 <xsl:apply-templates select="$data" mode="filter"> |
|
199 <xsl:with-param name="item" select="current()"/> |
|
200 </xsl:apply-templates> |
|
201 </xsl:variable> |
|
202 |
|
203 <xsl:if test="$display != 'hide' "> <!-- if hide, remove completely from the output--> |
|
204 <xsl:element name="{name()}"> |
|
205 <xsl:apply-templates select="@*" mode="join"> |
|
206 <xsl:with-param name="root" select="$root"/> |
|
207 <xsl:with-param name="filename" select="$filename"/> |
|
208 </xsl:apply-templates> |
|
209 </xsl:element> |
|
210 </xsl:if> |
|
211 </xsl:template> |
|
212 |
|
213 <!-- override mode="meta" to translate metadata sections. By default, include --> |
|
214 <xsl:template match="meta" priority="2" mode="join"><xsl:param name="data"/> |
|
215 <xsl:variable name="display"> |
|
216 <xsl:apply-templates select="$data" mode="filter"> |
|
217 <xsl:with-param name="item" select="current()"/> |
|
218 </xsl:apply-templates> |
|
219 </xsl:variable> |
|
220 |
|
221 <xsl:if test="$display != 'hide' "> <!-- if hide, remove completely from the output--> |
|
222 <xsl:apply-templates select="." mode="meta"> |
|
223 <xsl:with-param name="display" select="$display"/> |
|
224 <xsl:with-param name="data" select="$data"/> |
|
225 </xsl:apply-templates> |
|
226 </xsl:if> |
|
227 </xsl:template> |
|
228 |
|
229 <xsl:template match="meta[@rel='link-mapping']" priority="3" mode="join"/> <!--these are only used in the joining process, so remove from output --> |
|
230 |
|
231 |
|
232 <xsl:template match="*" mode="join"> |
|
233 <xsl:param name="root"/><xsl:param name="filename"/><xsl:param name="namespaces"/><xsl:param name="data"/> |
|
234 <!-- get attribtues from overlay --> |
|
235 <!-- test for presence, if filtered out, just return --> |
|
236 <!-- test for children, if it has some, but they're filtered out, either return or leave as empty, dependening on filter rule |
|
237 if had items and now has none, options: |
|
238 still has meta: keep / delete |
|
239 still has comments: keep / delete |
|
240 --> |
|
241 <xsl:variable name="display"> |
|
242 <xsl:apply-templates select="$data" mode="filter"> |
|
243 <xsl:with-param name="item" select="current()"/> |
|
244 </xsl:apply-templates> |
|
245 </xsl:variable> |
|
246 |
|
247 <xsl:if test="$display != 'hide' "> <!-- if hide, remove completely from the output--> |
|
248 <xsl:variable name="href"> |
|
249 <xsl:apply-templates select="." mode="link"> |
|
250 <xsl:with-param name="data" select="$data"/> |
|
251 </xsl:apply-templates> |
|
252 </xsl:variable> |
|
253 |
|
254 <xsl:element name="{name()}"> <!-- use this instead of <copy> so xalan doesn't add extra wrong namespaces --> |
|
255 <xsl:apply-templates select="@*" mode="join"> |
|
256 <xsl:with-param name="namespaces" select="$namespaces"/> |
|
257 </xsl:apply-templates> |
|
258 <xsl:if test="$display != '' "> |
|
259 <!-- custom attribute to indicate how this is to be represented. Blank indicates normal, hide removes from the output (see above), anything else is put in the attribute --> |
|
260 <xsl:attribute name="display"><xsl:value-of select="$display"/></xsl:attribute> |
|
261 </xsl:if> |
|
262 <xsl:apply-templates select="$data" mode="overlay-attributes"> |
|
263 <xsl:with-param name="item" select="current()"/> |
|
264 </xsl:apply-templates> |
|
265 <xsl:choose> |
|
266 <xsl:when test="$href !='' "> |
|
267 <xsl:variable name="prefixmap" select="ancestor::SystemDefinition/*/meta[@rel='link-mapping']/map-prefix[starts-with($href,@link)]"/> |
|
268 <xsl:variable name="origin" select="."/> |
|
269 <xsl:apply-templates select="document($href,.)/*" mode="join"> |
|
270 <xsl:with-param name="origin" select="$origin"/> |
|
271 <xsl:with-param name="data" select="$data"/> |
|
272 <xsl:with-param name="namespaces" select="$namespaces"/> |
|
273 <xsl:with-param name="filename"> |
|
274 <xsl:call-template name="joinpath"> |
|
275 <xsl:with-param name="file" select="$filename"/> |
|
276 <xsl:with-param name="rel"> |
|
277 <xsl:choose> |
|
278 <xsl:when test="$prefixmap"> |
|
279 <xsl:value-of select="$prefixmap/@to"/> |
|
280 <xsl:value-of select="substring-after($href,$prefixmap/@link)"/> |
|
281 </xsl:when> |
|
282 <xsl:otherwise> |
|
283 <xsl:value-of select="$href"/> |
|
284 </xsl:otherwise> |
|
285 </xsl:choose> |
|
286 </xsl:with-param> |
|
287 </xsl:call-template> |
|
288 </xsl:with-param> |
|
289 <xsl:with-param name="root"> |
|
290 <xsl:value-of select="$root"/>/<xsl:call-template name="lastbefore"> |
|
291 <xsl:with-param name="string" select="$href"/> |
|
292 </xsl:call-template> |
|
293 </xsl:with-param> |
|
294 </xsl:apply-templates> |
|
295 </xsl:when> |
|
296 <xsl:otherwise> |
|
297 <xsl:variable name="content"> |
|
298 <xsl:apply-templates select="*|comment()" mode="join"> |
|
299 <xsl:with-param name="root" select="$root"/> |
|
300 <xsl:with-param name="filename" select="$filename"/> |
|
301 <xsl:with-param name="namespaces" select="$namespaces"/> |
|
302 <xsl:with-param name="data" select="$data"/> |
|
303 </xsl:apply-templates> |
|
304 </xsl:variable> |
|
305 <xsl:apply-templates select="." mode="is-content-filtered"> <!-- add filtered="yes" if some content has been removed --> |
|
306 <xsl:with-param name="content" select="$content"/> |
|
307 </xsl:apply-templates> |
|
308 <xsl:apply-templates select="$data" mode="overlay-meta"> |
|
309 <xsl:with-param name="item" select="current()"/> |
|
310 </xsl:apply-templates> |
|
311 <xsl:copy-of select="$content"/> |
|
312 </xsl:otherwise> |
|
313 </xsl:choose> |
|
314 </xsl:element> |
|
315 </xsl:if> |
|
316 </xsl:template> |
|
317 |
|
318 <!-- By default, do nothing. Can override template to add filtered="yes" if need to track what's a placeholder and what's been filtered |
|
319 implement with param name="content" |
|
320 --> |
|
321 <xsl:template mode="is-content-filtered" match="*" priority="-2"/> |
|
322 |
|
323 |
|
324 <xsl:template match="@mrp[starts-with(.,'/')] | @bldFile[starts-with(.,'/')] | @base[starts-with(.,'/')]" mode="join"> |
|
325 <xsl:copy-of select="."/> |
|
326 </xsl:template> |
|
327 |
|
328 |
|
329 <xsl:template match="@mrp|@bldFile|@base" mode="join"> <xsl:param name="root"/><xsl:param name="filename"/> |
|
330 <xsl:attribute name="{name()}"> |
|
331 <xsl:call-template name="joinpath"> |
|
332 <xsl:with-param name="file" select="$filename"/> |
|
333 <xsl:with-param name="rel" select="."/> |
|
334 </xsl:call-template> |
|
335 </xsl:attribute> |
|
336 </xsl:template> |
|
337 |
|
338 |
|
339 <xsl:template match="@href" mode="join"/> <!--never copy this into the generated doc, that's the whole point of this module --> |
|
340 |
|
341 <xsl:template match="@*" mode="my-namespace"> <!-- the namespace of an ID --> |
|
342 <xsl:choose> |
|
343 <xsl:when test="contains(.,':')"> |
|
344 <xsl:value-of select="ancestor::*/namespace::*[name()=substring-before(current(),':')]"/> |
|
345 </xsl:when> |
|
346 <xsl:when test="/SystemDefinition/@id-namespace"> |
|
347 <xsl:value-of select="/SystemDefinition/@id-namespace"/> |
|
348 </xsl:when> |
|
349 <xsl:otherwise> |
|
350 <xsl:value-of select="$defaultns"/> |
|
351 </xsl:otherwise> |
|
352 </xsl:choose> |
|
353 </xsl:template> |
|
354 |
|
355 |
|
356 <xsl:template match="@*" mode="my-id"> <!-- the ID with namespace prefix removed --> |
|
357 <xsl:choose> |
|
358 <xsl:when test="contains(.,':')"> |
|
359 <xsl:value-of select="substring-after(.,':')"/> |
|
360 </xsl:when> |
|
361 <xsl:otherwise> |
|
362 <xsl:value-of select="."/> |
|
363 </xsl:otherwise> |
|
364 </xsl:choose> |
|
365 </xsl:template> |
|
366 |
|
367 <xsl:template match="*" mode="err-path"> |
|
368 <xsl:if test="../@id"> |
|
369 <xsl:apply-templates select=".." mode="err-path"/>/</xsl:if> |
|
370 <xsl:value-of select="@id"/> |
|
371 </xsl:template> |
|
372 |
|
373 |
|
374 <xsl:template match="@id|@before" mode="join"> |
|
375 <xsl:param name="namespaces"/> |
|
376 <!-- this will change the namespace prefixes for all IDs to match the root document --> |
|
377 <xsl:variable name="ns"> |
|
378 <xsl:apply-templates select="." mode="my-namespace"/> |
|
379 </xsl:variable> |
|
380 <xsl:if test="$ns=''"> |
|
381 <xsl:message terminate="yes">ERROR: Could not find namespace for <xsl:value-of select="name()"/> "<xsl:value-of select="."/>" in <xsl:apply-templates select="../.." mode="err-path"/> |
|
382 <xsl:text>
</xsl:text> |
|
383 </xsl:message> |
|
384 </xsl:if> |
|
385 <xsl:variable name="prefix" select="name($namespaces[.=$ns])"/> |
|
386 <xsl:attribute name="{name()}"> |
|
387 <xsl:choose> |
|
388 <xsl:when test="$prefix = 'id-namespace' or (not($namespaces[name()='id-prefix']) and $ns=$defaultns)"/> <!-- it's the default namespace, no prefix --> |
|
389 <xsl:when test="$prefix='' and contains(.,':')"> |
|
390 <!-- complex: copy id and copy namespace (namespace should be copied already)--> |
|
391 <xsl:value-of select="."/> |
|
392 </xsl:when> |
|
393 <xsl:when test="$prefix='' and $ns=$defaultns"/> <!-- no prefix and it's the default --> |
|
394 <xsl:when test="$prefix!=''"> <!-- just change the prefix --> |
|
395 <xsl:value-of select="concat($prefix,':')"/> |
|
396 </xsl:when> |
|
397 <xsl:otherwise> |
|
398 <xsl:message terminate="yes">ERROR: Joining error in resolving namespace for <xsl:value-of select="name()"/> "<xsl:value-of select="."/>" in <xsl:apply-templates select="../.." mode="err-path"/> |
|
399 <xsl:text>
</xsl:text></xsl:message> |
|
400 </xsl:otherwise> |
|
401 </xsl:choose> |
|
402 <xsl:apply-templates select="." mode="my-id"/> |
|
403 </xsl:attribute> |
|
404 </xsl:template> |
|
405 |
|
406 |
|
407 |
|
408 <xsl:template match="@*|comment()" mode="join"><xsl:copy-of select="."/></xsl:template> |
|
409 |
|
410 |
|
411 <!-- path handling follows --> |
|
412 |
|
413 <xsl:template name="lastbefore"><xsl:param name="string"/><xsl:param name="substr" select="'/'"/> |
|
414 <xsl:if test="contains($string,$substr)"> |
|
415 <xsl:value-of select="substring-before($string,$substr)"/> |
|
416 <xsl:if test="contains(substring-after($string,$substr),$substr)"> |
|
417 <xsl:value-of select="$substr"/> |
|
418 </xsl:if> |
|
419 <xsl:call-template name="lastbefore"> |
|
420 <xsl:with-param name="string" select="substring-after($string,$substr)"/> |
|
421 <xsl:with-param name="substr" select="$substr"/> |
|
422 </xsl:call-template> |
|
423 </xsl:if> |
|
424 </xsl:template> |
|
425 |
|
426 <xsl:template name="joinpath"><xsl:param name="file"/><xsl:param name="rel"/> |
|
427 <xsl:choose> |
|
428 <xsl:when test="(contains($rel,'://') and not(contains(substring-before($rel,'://'),'/'))) or starts-with($rel,'/')"> <!-- absolute URI or absolute path--> |
|
429 <xsl:value-of select="$rel"/> |
|
430 </xsl:when> |
|
431 <xsl:otherwise> <!-- relative link --> |
|
432 <xsl:call-template name="reducepath"> |
|
433 <xsl:with-param name="file"> |
|
434 <xsl:call-template name="lastbefore"> |
|
435 <xsl:with-param name="string" select="$file"/> |
|
436 </xsl:call-template> |
|
437 <xsl:text>/</xsl:text> |
|
438 <xsl:value-of select="$rel"/> |
|
439 </xsl:with-param> |
|
440 </xsl:call-template> |
|
441 </xsl:otherwise> |
|
442 </xsl:choose> |
|
443 </xsl:template> |
|
444 |
|
445 <xsl:template name="reducepath"><xsl:param name="file"/> |
|
446 <xsl:call-template name="reducedotdotpath"> |
|
447 <xsl:with-param name="file"> |
|
448 <xsl:call-template name="reducedotpath"> |
|
449 <xsl:with-param name="file" select="$file"/> |
|
450 </xsl:call-template> |
|
451 </xsl:with-param> |
|
452 </xsl:call-template> |
|
453 </xsl:template> |
|
454 |
|
455 <xsl:template name="reducedotdotpath"><xsl:param name="file"/> |
|
456 <xsl:choose> |
|
457 <xsl:when test="starts-with($file,'../')"> |
|
458 <xsl:text>../</xsl:text> |
|
459 <xsl:call-template name="reducedotdotpath"> |
|
460 <xsl:with-param name="file" select="substring($file,4)"/> |
|
461 </xsl:call-template> |
|
462 </xsl:when> |
|
463 <xsl:when test="contains($file,'/../')"> |
|
464 <xsl:call-template name="reducepath"> |
|
465 <xsl:with-param name="file"> |
|
466 <xsl:call-template name="lastbefore"> |
|
467 <xsl:with-param name="string" select="substring-before($file,'/../')"/> |
|
468 </xsl:call-template> |
|
469 <xsl:text>/</xsl:text> |
|
470 <xsl:value-of select="substring-after($file,'/../')"/> |
|
471 </xsl:with-param> |
|
472 </xsl:call-template> |
|
473 </xsl:when> |
|
474 <xsl:otherwise><xsl:value-of select="$file"/></xsl:otherwise> |
|
475 </xsl:choose> |
|
476 </xsl:template> |
|
477 |
|
478 <xsl:template name="reducedotpath"><xsl:param name="file"/> |
|
479 <xsl:choose> |
|
480 <xsl:when test="starts-with($file,'./')"> |
|
481 <xsl:call-template name="reducedotpath"> |
|
482 <xsl:with-param name="file" select="substring($file,3)"/> |
|
483 </xsl:call-template> |
|
484 </xsl:when> |
|
485 |
|
486 <xsl:when test="contains($file,'/./')"> |
|
487 <xsl:call-template name="reducepath"> |
|
488 <xsl:with-param name="file"> |
|
489 <xsl:value-of select="substring-before($file,'/./')"/> |
|
490 <xsl:text>/</xsl:text> |
|
491 <xsl:value-of select="substring-after($file,'/./')"/> |
|
492 </xsl:with-param> |
|
493 </xsl:call-template> |
|
494 </xsl:when> |
|
495 <xsl:when test="substring($file,string-length($file) - 1) = '/.'"> |
|
496 <xsl:value-of select="substring($file,1,string-length($file) - 2)"/> |
|
497 </xsl:when> |
|
498 <xsl:otherwise><xsl:value-of select="$file"/></xsl:otherwise> |
|
499 </xsl:choose> |
|
500 </xsl:template> |
|
501 |
|
502 <!-- overridable templates follow --> |
|
503 |
|
504 |
|
505 <xsl:template match="*" mode="filter" priority="-9"/> <!-- by default show --> |
|
506 <xsl:template match="*" mode="overlay-attributes" priority="-9"/> <!-- by default do nothing --> |
|
507 <xsl:template match="*" mode="overlay-meta" priority="-9"/> <!-- by default do nothing --> |
|
508 <xsl:template match="/SystemDefinition" mode="add-id-ns" priority="-9"/> <!-- some tools may have an easier job if this were always present, but, by default, assume it can just stay implied --> |
|
509 |
|
510 <xsl:template match="*" mode="link" priority="-1"> <!-- can be overriden to allow custom changes to href values --> |
|
511 <xsl:value-of select="@href"/> |
|
512 </xsl:template> |
|
513 |
|
514 |
|
515 <xsl:template match="*" mode="meta" priority="-9"><xsl:param name="data"/><xsl:param name="display"/> |
|
516 <xsl:element name="{name()}"> |
|
517 <xsl:copy-of select="@*[name()!='href']"/> <!-- copy all attributes as is, always drop href --> |
|
518 <xsl:choose> |
|
519 <xsl:when test="$display='local' and @href and contains(@href,':') and not(starts-with(@href,'file:'))"> |
|
520 <!-- non-local URL: only want local URLs, so keep href as is--> |
|
521 <xsl:copy-of select="@href"/> |
|
522 </xsl:when> |
|
523 <xsl:when test="@href"> |
|
524 <xsl:copy-of select="document(@href,.)/*"/> |
|
525 </xsl:when> |
|
526 <xsl:otherwise> |
|
527 <xsl:copy-of select="*|comment()"/> |
|
528 </xsl:otherwise> |
|
529 </xsl:choose> |
|
530 </xsl:element> |
|
531 </xsl:template> |
|
532 |
|
533 |
|
534 </xsl:stylesheet> |