5 extension-element-prefixes="doc"
6 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
7 xmlns:doc="http://xsltsl.org/xsl/documentation/1.0"
8 xmlns:uri="http://xsltsl.org/uri"
11 <doc:reference xmlns="">
13 <releaseinfo role="meta">
14 $Id: uri.xsl 3991 2004-11-10 06:51:55Z balls $
17 <surname>Diamond</surname>
18 <firstname>Jason</firstname>
22 <holder>Jason Diamond</holder>
26 <title>URI (Uniform Resource Identifier) Processing</title>
30 <title>Introduction</title>
31 <para>This module provides templates for processing URIs (Uniform Resource Identifers).</para>
37 <doc:template name="uri:is-absolute-uri" xmlns="">
38 <refpurpose>Determines if a URI is absolute or relative.</refpurpose>
41 <para>Absolute URIs start with a scheme (like "http:" or "mailto:").</para>
49 <para>An absolute or relative URI.</para>
56 <para>Returns 'true' if the URI is absolute or '' if it's not.</para>
60 <xsl:template name="uri:is-absolute-uri">
61 <xsl:param name="uri"/>
63 <xsl:if test="contains($uri, ':')">
64 <xsl:value-of select="true()"/>
69 <doc:template name="uri:get-uri-scheme" xmlns="">
70 <refpurpose>Gets the scheme part of a URI.</refpurpose>
73 <para>The ':' is not part of the scheme.</para>
81 <para>An absolute or relative URI.</para>
88 <para>Returns the scheme (without the ':') or '' if the URI is relative.</para>
92 <xsl:template name="uri:get-uri-scheme">
93 <xsl:param name="uri"/>
94 <xsl:if test="contains($uri, ':')">
95 <xsl:value-of select="substring-before($uri, ':')"/>
99 <doc:template name="uri:get-uri-authority" xmlns="">
100 <refpurpose>Gets the authority part of a URI.</refpurpose>
103 <para>The authority usually specifies the host machine for a resource. It always follows '//' in a typical URI.</para>
111 <para>An absolute or relative URI.</para>
118 <para>Returns the authority (without the '//') or '' if the URI has no authority.</para>
122 <xsl:template name="uri:get-uri-authority">
123 <xsl:param name="uri"/>
125 <xsl:variable name="a">
127 <xsl:when test="contains($uri, ':')">
128 <xsl:if test="substring(substring-after($uri, ':'), 1, 2) = '//'">
129 <xsl:value-of select="substring(substring-after($uri, ':'), 3)"/>
133 <xsl:if test="substring($uri, 1, 2) = '//'">
134 <xsl:value-of select="substring($uri, 3)"/>
141 <xsl:when test="contains($a, '/')">
142 <xsl:value-of select="substring-before($a, '/')" />
144 <xsl:when test="contains($a, '?')">
145 <xsl:value-of select="substring-before($a, '?')" />
147 <xsl:when test="contains($a, '#')">
148 <xsl:value-of select="substring-before($a, '#')" />
151 <xsl:value-of select="$a" />
157 <doc:template name="uri:get-uri-path" xmlns="">
158 <refpurpose>Gets the path part of a URI.</refpurpose>
161 <para>The path usually comes after the '/' in a URI.</para>
169 <para>An absolute or relative URI.</para>
176 <para>Returns the path (with any leading '/') or '' if the URI has no path.</para>
180 <xsl:template name="uri:get-uri-path">
181 <xsl:param name="uri"/>
183 <xsl:variable name="p">
185 <xsl:when test="contains($uri, '//')">
186 <xsl:if test="contains(substring-after($uri, '//'), '/')">
187 <xsl:value-of select="concat('/', substring-after(substring-after($uri, '//'), '/'))"/>
192 <xsl:when test="contains($uri, ':')">
193 <xsl:value-of select="substring-after($uri, ':')"/>
196 <xsl:value-of select="$uri"/>
204 <xsl:when test="contains($p, '?')">
205 <xsl:value-of select="substring-before($p, '?')" />
207 <xsl:when test="contains($p, '#')">
208 <xsl:value-of select="substring-before($p, '#')" />
211 <xsl:value-of select="$p" />
217 <doc:template name="uri:get-uri-query" xmlns="">
218 <refpurpose>Gets the query part of a URI.</refpurpose>
221 <para>The query comes after the '?' in a URI.</para>
229 <para>An absolute or relative URI.</para>
236 <para>Returns the query (without the '?') or '' if the URI has no query.</para>
240 <xsl:template name="uri:get-uri-query">
241 <xsl:param name="uri"/>
243 <xsl:variable name="q" select="substring-after($uri, '?')"/>
246 <xsl:when test="contains($q, '#')">
247 <xsl:value-of select="substring-before($q, '#')"/>
249 <xsl:otherwise><xsl:value-of select="$q"/></xsl:otherwise>
254 <doc:template name="uri:get-uri-fragment" xmlns="">
255 <refpurpose>Gets the fragment part of a URI.</refpurpose>
258 <para>The fragment comes after the '#' in a URI.</para>
266 <para>An absolute or relative URI.</para>
273 <para>Returns the fragment (without the '#') or '' if the URI has no fragment.</para>
277 <xsl:template name="uri:get-uri-fragment">
278 <xsl:param name="uri"/>
280 <xsl:value-of select="substring-after($uri, '#')"/>
284 <doc:template name="uri:resolve-uri" xmlns="">
285 <refpurpose>Resolves a URI reference against a base URI.</refpurpose>
288 <para>This template follows the guidelines specified by <ulink url="ftp://ftp.isi.edu/in-notes/rfc2396.txt">RFC 2396</ulink>.</para>
294 <term>reference</term>
296 <para>A (potentially relative) URI reference.</para>
302 <para>The base URI.</para>
306 <term>document</term>
308 <para>The URI of the current document. This defaults to the value of the base URI if not specified.</para>
315 <para>The "combined" URI.</para>
319 <xsl:template name="uri:resolve-uri">
320 <xsl:param name="reference"/>
321 <xsl:param name="base"/>
322 <xsl:param name="document" select="$base"/>
324 <xsl:variable name="reference-scheme">
325 <xsl:call-template name="uri:get-uri-scheme">
326 <xsl:with-param name="uri" select="$reference"/>
330 <xsl:variable name="reference-authority">
331 <xsl:call-template name="uri:get-uri-authority">
332 <xsl:with-param name="uri" select="$reference"/>
336 <xsl:variable name="reference-path">
337 <xsl:call-template name="uri:get-uri-path">
338 <xsl:with-param name="uri" select="$reference"/>
342 <xsl:variable name="reference-query">
343 <xsl:call-template name="uri:get-uri-query">
344 <xsl:with-param name="uri" select="$reference"/>
348 <xsl:variable name="reference-fragment">
349 <xsl:call-template name="uri:get-uri-fragment">
350 <xsl:with-param name="uri" select="$reference"/>
357 not(string-length($reference-scheme)) and
358 not(string-length($reference-authority)) and
359 not(string-length($reference-path)) and
360 not(string-length($reference-query))"
364 <xsl:when test="contains($document, '?')">
365 <xsl:value-of select="substring-before($document, '?')"/>
367 <xsl:when test="contains($document, '#')">
368 <xsl:value-of select="substring-before($document, '#')"/>
371 <xsl:value-of select="$document"/>
375 <xsl:if test="string-length($reference-fragment)">
376 <xsl:value-of select="concat('#', $reference-fragment)"/>
381 <xsl:when test="string-length($reference-scheme)">
383 <xsl:value-of select="$reference"/>
389 <xsl:variable name="base-scheme">
390 <xsl:call-template name="uri:get-uri-scheme">
391 <xsl:with-param name="uri" select="$base"/>
395 <xsl:variable name="base-authority">
396 <xsl:call-template name="uri:get-uri-authority">
397 <xsl:with-param name="uri" select="$base"/>
401 <xsl:variable name="base-path">
402 <xsl:call-template name="uri:get-uri-path">
403 <xsl:with-param name="uri" select="$base"/>
407 <xsl:variable name="base-query">
408 <xsl:call-template name="uri:get-uri-query">
409 <xsl:with-param name="uri" select="$base"/>
413 <xsl:variable name="base-fragment">
414 <xsl:call-template name="uri:get-uri-fragment">
415 <xsl:with-param name="uri" select="$base"/>
419 <xsl:variable name="result-authority">
421 <xsl:when test="string-length($reference-authority)">
422 <xsl:value-of select="$reference-authority"/>
425 <xsl:value-of select="$base-authority"/>
430 <xsl:variable name="result-path">
432 <!-- don't normalize absolute paths -->
433 <xsl:when test="starts-with($reference-path, '/')">
434 <xsl:value-of select="$reference-path" />
437 <xsl:call-template name="uri:normalize-path">
438 <xsl:with-param name="path">
439 <xsl:if test="string-length($reference-authority) = 0 and substring($reference-path, 1, 1) != '/'">
440 <xsl:call-template name="uri:get-path-without-file">
441 <xsl:with-param name="path-with-file" select="$base-path"/>
443 <xsl:value-of select="'/'"/>
445 <xsl:value-of select="$reference-path"/>
452 <xsl:value-of select="concat($base-scheme, '://', $result-authority, $result-path)"/>
454 <xsl:if test="string-length($reference-query)">
455 <xsl:value-of select="concat('?', $reference-query)"/>
458 <xsl:if test="string-length($reference-fragment)">
459 <xsl:value-of select="concat('#', $reference-fragment)"/>
467 <xsl:template name="uri:get-path-without-file">
468 <xsl:param name="path-with-file" />
469 <xsl:param name="path-without-file" />
472 <xsl:when test="contains($path-with-file, '/')">
473 <xsl:call-template name="uri:get-path-without-file">
474 <xsl:with-param name="path-with-file" select="substring-after($path-with-file, '/')" />
475 <xsl:with-param name="path-without-file">
477 <xsl:when test="$path-without-file">
478 <xsl:value-of select="concat($path-without-file, '/', substring-before($path-with-file, '/'))" />
481 <xsl:value-of select="substring-before($path-with-file, '/')" />
488 <xsl:value-of select="$path-without-file" />
494 <xsl:template name="uri:normalize-path">
495 <xsl:param name="path"/>
496 <xsl:param name="result" select="''"/>
499 <xsl:when test="string-length($path)">
501 <xsl:when test="$path = '/'">
502 <xsl:value-of select="concat($result, '/')"/>
504 <xsl:when test="$path = '.'">
505 <xsl:value-of select="concat($result, '/')"/>
507 <xsl:when test="$path = '..'">
508 <xsl:call-template name="uri:get-path-without-file">
509 <xsl:with-param name="path-with-file" select="$result"/>
511 <xsl:value-of select="'/'"/>
513 <xsl:when test="contains($path, '/')">
514 <!-- the current segment -->
515 <xsl:variable name="s" select="substring-before($path, '/')"/>
516 <!-- the remaining path -->
517 <xsl:variable name="p">
519 <xsl:when test="substring-after($path, '/') = ''">
520 <xsl:value-of select="'/'"/>
523 <xsl:value-of select="substring-after($path, '/')"/>
528 <xsl:when test="$s = ''">
529 <xsl:call-template name="uri:normalize-path">
530 <xsl:with-param name="path" select="$p"/>
531 <xsl:with-param name="result" select="$result"/>
534 <xsl:when test="$s = '.'">
535 <xsl:call-template name="uri:normalize-path">
536 <xsl:with-param name="path" select="$p"/>
537 <xsl:with-param name="result" select="$result"/>
540 <xsl:when test="$s = '..'">
542 <xsl:when test="string-length($result) and (substring($result, string-length($result) - 2) != '/..')">
543 <xsl:call-template name="uri:normalize-path">
544 <xsl:with-param name="path" select="$p"/>
545 <xsl:with-param name="result">
546 <xsl:call-template name="uri:get-path-without-file">
547 <xsl:with-param name="path-with-file" select="$result"/>
553 <xsl:call-template name="uri:normalize-path">
554 <xsl:with-param name="path" select="$p"/>
555 <xsl:with-param name="result" select="concat($result, '/..')"/>
561 <xsl:call-template name="uri:normalize-path">
562 <xsl:with-param name="path" select="$p"/>
563 <xsl:with-param name="result" select="concat($result, '/', $s)"/>
569 <xsl:value-of select="concat($result, '/', $path)"/>
574 <xsl:value-of select="$result"/>