uri-utils.xsl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. This file is part of the DITA Open Toolkit project.
  4. Copyright 2016 Jarno Elovirta
  5. See the accompanying LICENSE file for applicable license.
  6. -->
  7. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  8. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  9. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  10. version="2.0"
  11. exclude-result-prefixes="xs dita-ot">
  12. <xsl:function name="dita-ot:normalize" as="xs:anyURI">
  13. <xsl:param name="uri" as="xs:anyURI"/>
  14. <xsl:variable name="normalized">
  15. <xsl:call-template name="dita-ot:normalize-uri">
  16. <xsl:with-param name="src" select="tokenize($uri, '/')"/>
  17. </xsl:call-template>
  18. </xsl:variable>
  19. <xsl:sequence select="xs:anyURI($normalized)"/>
  20. </xsl:function>
  21. <xsl:template name="dita-ot:normalize-uri" as="xs:string">
  22. <xsl:param name="src" as="xs:string*"/>
  23. <xsl:param name="res" select="()" as="xs:string*"/>
  24. <xsl:choose>
  25. <xsl:when test="empty($src)">
  26. <xsl:value-of select="$res" separator="/"/>
  27. </xsl:when>
  28. <xsl:when test="$src[1] = '.'">
  29. <xsl:call-template name="dita-ot:normalize-uri">
  30. <xsl:with-param name="src" select="$src[position() ne 1]"/>
  31. <xsl:with-param name="res" select="$res"/>
  32. </xsl:call-template>
  33. </xsl:when>
  34. <xsl:when test="$src[1] = '..' and exists($res) and not($res[position() eq last()] = ('..', ''))">
  35. <xsl:call-template name="dita-ot:normalize-uri">
  36. <xsl:with-param name="src" select="$src[position() ne 1]"/>
  37. <xsl:with-param name="res" select="$res[position() ne last()]"/>
  38. </xsl:call-template>
  39. </xsl:when>
  40. <xsl:otherwise>
  41. <xsl:call-template name="dita-ot:normalize-uri">
  42. <xsl:with-param name="src" select="$src[position() ne 1]"/>
  43. <xsl:with-param name="res" select="($res, $src[1])"/>
  44. </xsl:call-template>
  45. </xsl:otherwise>
  46. </xsl:choose>
  47. </xsl:template>
  48. <xsl:function name="dita-ot:relativize" as="xs:anyURI">
  49. <xsl:param name="base" as="xs:anyURI"/>
  50. <xsl:param name="uri" as="xs:anyURI"/>
  51. <xsl:variable name="b-scheme" select="substring-before($base, ':')" as="xs:string"/>
  52. <xsl:variable name="u-scheme" select="substring-before($uri, ':')" as="xs:string"/>
  53. <xsl:choose>
  54. <xsl:when test="$b-scheme ne $u-scheme">
  55. <xsl:sequence select="$uri"/>
  56. </xsl:when>
  57. <xsl:otherwise>
  58. <xsl:variable name="b" select="tokenize(substring-after($base, ':'), '/')" as="xs:string+"/>
  59. <xsl:variable name="u" select="tokenize(substring-after($uri, ':'), '/')" as="xs:string+"/>
  60. <xsl:sequence select="dita-ot:relativize.strip-and-prefix($b, $u)"/>
  61. </xsl:otherwise>
  62. </xsl:choose>
  63. </xsl:function>
  64. <xsl:function name="dita-ot:relativize.strip-and-prefix" as="xs:anyURI">
  65. <xsl:param name="a" as="xs:string+"/>
  66. <xsl:param name="b" as="xs:string+"/>
  67. <xsl:choose>
  68. <xsl:when test="$a[1] = $b[1]">
  69. <xsl:sequence select="dita-ot:relativize.strip-and-prefix($a[position() ne 1], $b[position() ne 1])"/>
  70. </xsl:when>
  71. <xsl:otherwise>
  72. <xsl:variable name="res" as="xs:string+">
  73. <xsl:for-each select="$a[position() ne 1]">../</xsl:for-each>
  74. <xsl:value-of select="$b" separator="/"/>
  75. </xsl:variable>
  76. <xsl:sequence select="xs:anyURI(string-join($res, ''))"/>
  77. </xsl:otherwise>
  78. </xsl:choose>
  79. </xsl:function>
  80. <xsl:function name="dita-ot:strip-fragment" as="xs:string">
  81. <xsl:param name="href" as="xs:string"/>
  82. <xsl:value-of select="if (contains($href, '#')) then substring-before($href, '#') else $href"/>
  83. </xsl:function>
  84. <xsl:function name="dita-ot:resolve" as="xs:anyURI">
  85. <xsl:param name="base" as="xs:anyURI"/>
  86. <xsl:param name="uri" as="xs:anyURI"/>
  87. <xsl:variable name="b" select="tokenize(dita-ot:strip-fragment($base), '/')" as="xs:string+"/>
  88. <xsl:variable name="u" select="tokenize($uri, '/')" as="xs:string+"/>
  89. <xsl:variable name="res" as="xs:string+">
  90. <xsl:call-template name="dita-ot:normalize-uri">
  91. <xsl:with-param name="src" select="($b[position() ne last()], $u)"/>
  92. </xsl:call-template>
  93. </xsl:variable>
  94. <xsl:sequence select="xs:anyURI(string-join($res, '/'))"/>
  95. </xsl:function>
  96. </xsl:stylesheet>