1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
<?xml version="1.0"?>
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
PathUtils.xsl
A set of XSLT templates useful for parsing URI paths:
dirname: return the directory part of a path
filename: return the file part of a path
ext: return the last extension of the filename in a path
filename-noext: return the file part of a path without its last extension
@author Jeff Turner <jefft@apache.org>
$Id$
-->
<!-- Returns the directory part of a path. Equivalent to Unix 'dirname'.
Examples:
'' -> ''
'foo/index.html' -> 'foo/'
-->
<xsl:template name="dirname">
<xsl:param name="path" />
<xsl:if test="contains($path, '/')">
<xsl:value-of select="concat(substring-before($path, '/'), '/')" />
<xsl:call-template name="dirname">
<xsl:with-param name="path"
select="substring-after($path, '/')" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Normalized (..'s eliminated) version of 'dirname' -->
<xsl:template name="dirname-nz">
<xsl:param name="path" />
<xsl:call-template name="normalize">
<xsl:with-param name="path">
<xsl:call-template name="dirname">
<xsl:with-param name="path" select="$path" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<!-- Returns the filename part of a path. Equivalent to Unix 'basename'
Examples:
'index.html' -> 'index.html'
'foo/bar/' -> ''
'foo/bar/index.html' -> 'index.html'
-->
<xsl:template name="filename">
<xsl:param name="path"/>
<xsl:choose>
<xsl:when test="contains($path, '/')">
<xsl:call-template name="filename">
<xsl:with-param name="path" select="substring-after($path, '/')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$path"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Returns the last extension of a filename in a path.
Examples:
'index.html' -> '.html'
'index.dtdx.html' -> '.html'
'foo/bar/' -> ''
'foo/bar/index.html' -> '.html'
'foo/bar/index' -> ''
-->
<xsl:template name="ext">
<xsl:param name="path"/>
<xsl:param name="subflag"/> <!-- Outermost call? -->
<xsl:choose>
<xsl:when test="contains($path, '.')">
<xsl:call-template name="ext">
<xsl:with-param name="path" select="substring-after($path, '.')"/>
<xsl:with-param name="subflag" select="'sub'"/>
</xsl:call-template>
</xsl:when>
<!-- Handle extension-less filenames by returning '' -->
<xsl:when test="not($subflag) and not(contains($path, '.'))">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('.', $path)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Returns a filename of a path stripped of its last extension.
Examples:
'foo/bar/index.dtdx.html' -> 'index.dtdx'
-->
<xsl:template name="filename-noext">
<xsl:param name="path"/>
<xsl:variable name="filename">
<xsl:call-template name="filename">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="ext">
<xsl:call-template name="ext">
<xsl:with-param name="path" select="$filename"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="substring($filename, 1, string-length($filename) - string-length($ext))"/>
</xsl:template>
<!-- Returns a path with the filename stripped of its last extension.
Examples:
'foo/bar/index.dtdx.html' -> 'foo/bar/index.dtdx'
-->
<xsl:template name="path-noext">
<xsl:param name="path"/>
<xsl:variable name="ext">
<xsl:call-template name="ext">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="substring($path, 1, string-length($path) - string-length($ext))"/>
</xsl:template>
<!-- Normalized (..'s eliminated) version of 'path-noext' -->
<xsl:template name="path-noext-nz">
<xsl:param name="path" />
<xsl:call-template name="normalize">
<xsl:with-param name="path">
<xsl:call-template name="path-noext">
<xsl:with-param name="path" select="$path" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<!-- Returns a path with any fragment identifier ('#...') stripped off
Examples:
'foo/bar/index.dtdx.html#blah' -> 'foo/bar/index.dtdx.html'
-->
<xsl:template name="path-nofrag">
<xsl:param name="path"/>
<xsl:if test="not(contains($path, '#'))">
<xsl:value-of select="$path"/>
</xsl:if>
<xsl:if test="contains($path, '#')">
<xsl:value-of select="substring-before($path, '#')"/>
</xsl:if>
</xsl:template>
<!-- Normalizes a path, converting '/' to '\' and eliminating ..'s
Examples:
'foo/bar/../baz/index.html' -> foo/baz/index.html'
-->
<xsl:template name="normalize">
<xsl:param name="path"/>
<xsl:variable name="path-" select="translate($path, '\', '/')"/>
<xsl:choose>
<xsl:when test="contains($path-, '/../')">
<xsl:variable name="pa" select="substring-before($path-, '/../')"/>
<xsl:variable name="th" select="substring-after($path-, '/../')"/>
<xsl:variable name="pa-">
<xsl:call-template name="dirname">
<xsl:with-param name="path" select="$pa"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="pa-th" select="concat($pa-, $th)"/>
<xsl:call-template name="normalize">
<xsl:with-param name="path" select="$pa-th"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$path-"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
Uncomment this to test.
Usage: saxon pathutils.xsl pathutils.xsl path=foo/bar
<xsl:param name="path" select="'/foo/bar/../baz/index.html'"/>
<xsl:template match="/">
<xsl:message>
path = <xsl:value-of select="$path"/>
normalize = <xsl:call-template name="normalize">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
dirname = <xsl:call-template name="dirname">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
dirname-nz = <xsl:call-template name="dirname-nz">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
filename = <xsl:call-template name="filename">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
ext = <xsl:call-template name="ext">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
filename-noext = <xsl:call-template name="filename-noext">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
path-noext = <xsl:call-template name="path-noext">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
path-noext-nz = <xsl:call-template name="path-noext-nz">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
path-nofrag = <xsl:call-template name="path-nofrag">
<xsl:with-param name="path" select="$path"/>
</xsl:call-template>
</xsl:message>
</xsl:template>
-->
</xsl:stylesheet>
|