home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / dotdots.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  2.7 KB  |  79 lines

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3.   Copyright 1999-2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.   Unless required by applicable law or agreed to in writing, software
  12.   distributed under the License is distributed on an "AS IS" BASIS,
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   See the License for the specific language governing permissions and
  15.   limitations under the License.
  16. -->
  17.  
  18. <!--
  19. Contains the 'dotdots' template, which, given a path, will output a set of
  20. directory traversals to get back to the source directory. Handles both '/' and
  21. '\' directory separators.
  22.  
  23. Examples:
  24.   Input                           Output 
  25.     index.html                    ""
  26.     dir/index.html                "../"
  27.     dir/subdir/index.html         "../../"
  28.     dir//index.html              "../"
  29.     dir/                          "../"
  30.     dir//                         "../"
  31.     \some\windows\path            "../../"
  32.     \some\windows\path\           "../../../"
  33.     \Program Files\mydir          "../"
  34.  
  35. Cannot handle ..'s in the path, so don't expect 'dir/subdir/../index.html' to
  36. work.
  37.  
  38. jefft@apache.org
  39. -->
  40.  
  41. <xsl:stylesheet
  42.   version="1.0"
  43.   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  44.  
  45.   <xsl:template name="dotdots">
  46.     <xsl:param name="path"/>
  47.     <xsl:variable name="dirs" select="normalize-space(translate(concat($path, 'x'), ' /\', '_  '))"/>
  48.     <!-- The above does the following:
  49.        o Adds a trailing character to the path. This prevents us having to deal
  50.          with the special case of ending with '/'
  51.        o Translates all directory separators to ' ', and normalize spaces,
  52.          cunningly eliminating duplicate '//'s. We also translate any real
  53.          spaces into _ to preserve them.
  54.     -->
  55.     <xsl:variable name="remainder" select="substring-after($dirs, ' ')"/>
  56.     <xsl:if test="$remainder">
  57.       <xsl:text>../</xsl:text>
  58.       <xsl:call-template name="dotdots">
  59.         <xsl:with-param name="path" select="translate($remainder, ' ', '/')"/>
  60.         <!-- Translate back to /'s because that's what the template expects. -->
  61.       </xsl:call-template>
  62.     </xsl:if>
  63.   </xsl:template>
  64.  
  65. <!--
  66.   Uncomment to test.
  67.   Usage: saxon dotdots.xsl dotdots.xsl path='/my/test/path'
  68.  
  69.   <xsl:param name="path"/>
  70.   <xsl:template match="/">
  71.     <xsl:message>Path: <xsl:value-of select="$path"/></xsl:message>
  72.     <xsl:call-template name="dotdots">
  73.       <xsl:with-param name="path" select="$path"/>
  74.     </xsl:call-template>
  75.   </xsl:template>
  76.  -->
  77.  
  78. </xsl:stylesheet>
  79.