functions.xsl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. This file is part of the DITA Open Toolkit project.
  4. Copyright 2004, 2005 IBM Corporation
  5. See the accompanying LICENSE file for applicable license.
  6. -->
  7. <xsl:stylesheet version="2.0"
  8. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  9. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  10. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  11. exclude-result-prefixes="xs dita-ot">
  12. <!-- href -->
  13. <xsl:function name="dita-ot:resolve-href-path" as="xs:anyURI">
  14. <xsl:param name="href" as="attribute(href)"/>
  15. <xsl:variable name="source" as="xs:anyURI" select="base-uri($href)"/>
  16. <xsl:sequence select="
  17. if (starts-with($href, '#'))
  18. then $source
  19. else resolve-uri(tokenize($href, '#')[1], $source)
  20. "/>
  21. </xsl:function>
  22. <xsl:function name="dita-ot:retrieve-href-target" as="node()?">
  23. <xsl:param name="href" as="attribute(href)"/>
  24. <xsl:variable name="doc" as="document-node()"
  25. select="doc(dita-ot:resolve-href-path($href))"/>
  26. <xsl:sequence select="
  27. if (dita-ot:has-element-id($href))
  28. then $doc/key('id', dita-ot:get-element-id($href))
  29. [dita-ot:get-closest-topic(.)/@id eq dita-ot:get-topic-id($href)]
  30. else if (dita-ot:has-topic-id($href) and not(dita-ot:has-element-id($href)))
  31. then $doc/key('id', dita-ot:get-topic-id($href))
  32. else $doc
  33. "/>
  34. </xsl:function>
  35. <!-- ID -->
  36. <xsl:function name="dita-ot:has-topic-id" as="xs:boolean">
  37. <xsl:param name="href"/>
  38. <xsl:sequence select="contains($href, '#')"/>
  39. </xsl:function>
  40. <xsl:function name="dita-ot:get-element-id" as="xs:string?">
  41. <xsl:param name="href"/>
  42. <xsl:variable name="fragment" select="substring-after($href, '#')" as="xs:string"/>
  43. <xsl:if test="contains($fragment, '/')">
  44. <xsl:value-of select="substring-after($fragment, '/')"/>
  45. </xsl:if>
  46. </xsl:function>
  47. <xsl:function name="dita-ot:has-element-id" as="xs:boolean">
  48. <xsl:param name="href"/>
  49. <xsl:sequence select="contains(substring-after($href, '#'), '/')"/>
  50. </xsl:function>
  51. <xsl:function name="dita-ot:get-topic-id" as="xs:string?">
  52. <xsl:param name="href"/>
  53. <xsl:variable name="fragment" select="substring-after($href, '#')" as="xs:string"/>
  54. <xsl:if test="string-length($fragment) gt 0">
  55. <xsl:choose>
  56. <xsl:when test="contains($fragment, '/')">
  57. <xsl:value-of select="substring-before($fragment, '/')"/>
  58. </xsl:when>
  59. <xsl:otherwise>
  60. <xsl:value-of select="$fragment"/>
  61. </xsl:otherwise>
  62. </xsl:choose>
  63. </xsl:if>
  64. </xsl:function>
  65. <!-- language -->
  66. <xsl:function name="dita-ot:get-current-language" as="xs:string">
  67. <xsl:param name="ctx" as="node()"/>
  68. <xsl:sequence select="
  69. lower-case(($ctx/ancestor-or-self::*[@xml:lang][1]/@xml:lang, $DEFAULTLANG)[1])
  70. "/>
  71. </xsl:function>
  72. <xsl:function name="dita-ot:get-iso-language-code" as="xs:string">
  73. <xsl:param name="lang" as="xs:string"/>
  74. <xsl:sequence select="tokenize($lang, '-')[1]"/>
  75. </xsl:function>
  76. <xsl:function name="dita-ot:get-language-codes" as="xs:string*">
  77. <xsl:param name="lang" as="xs:string"/>
  78. <xsl:sequence select="$lang, dita-ot:get-iso-language-code($lang)"/>
  79. </xsl:function>
  80. <xsl:function name="dita-ot:get-first-topic-language" as="xs:string">
  81. <!-- $ctx should contain the root element.
  82. If toot element is <dita>, check first topic. Otherwise, root element. Otherwise, default. -->
  83. <xsl:param name="ctx" as="node()"/>
  84. <xsl:sequence select="
  85. lower-case(($ctx/self::dita/*[1]/@xml:lang, $ctx/@xml:lang, $DEFAULTLANG)[1])
  86. "/>
  87. </xsl:function>
  88. <!-- URI -->
  89. <xsl:function name="dita-ot:normalize-uri" as="xs:string">
  90. <xsl:param name="uri" as="xs:string"/>
  91. <xsl:call-template name="dita-ot:normalize-uri">
  92. <xsl:with-param name="src" select="tokenize($uri, '/')"/>
  93. </xsl:call-template>
  94. </xsl:function>
  95. <xsl:function name="dita-ot:get-variable" as="node()*">
  96. <xsl:param name="ctx" as="node()"/>
  97. <xsl:param name="id" as="xs:string"/>
  98. <xsl:param name="params" as="node()*"/>
  99. <xsl:call-template name="findString">
  100. <xsl:with-param name="ctx" select="$ctx" tunnel="yes"/>
  101. <xsl:with-param name="id" select="$id"/>
  102. <xsl:with-param name="params" select="$params"/>
  103. <xsl:with-param name="ancestorlang"
  104. select="dita-ot:get-language-codes(dita-ot:get-current-language($ctx))"/>
  105. <xsl:with-param name="defaultlang" select="dita-ot:get-language-codes($DEFAULTLANG)"/>
  106. </xsl:call-template>
  107. </xsl:function>
  108. <xsl:function name="dita-ot:get-variable" as="node()*">
  109. <xsl:param name="ctx" as="node()"/>
  110. <xsl:param name="id" as="xs:string"/>
  111. <xsl:sequence select="dita-ot:get-variable($ctx, $id, ())"/>
  112. </xsl:function>
  113. </xsl:stylesheet>