You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dotdots.xsl 2.7KB

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