functions.xsl 4.8 KB

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