dita-utilities.xsl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. <!-- Common utilities that can be used by DITA transforms -->
  8. <xsl:stylesheet version="2.0"
  9. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  10. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  11. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  12. exclude-result-prefixes="xs dita-ot">
  13. <xsl:include href="functions.xsl"/>
  14. <xsl:param name="defaultLanguage" select="'en'" as="xs:string"/>
  15. <xsl:param name="DEFAULTLANG" select="if (/*/@xml:lang) then /*/@xml:lang else $defaultLanguage" as="xs:string"/>
  16. <xsl:param name="variableFiles.url" select="'plugin:org.dita.base:xsl/common/strings.xml'"/>
  17. <xsl:variable name="pixels-per-inch" select="number(96)"/>
  18. <xsl:key name="id" match="*[@id]" use="@id"/>
  19. <!-- Function to determine the current language, and return it in lower case -->
  20. <xsl:template name="getLowerCaseLang">
  21. <xsl:value-of select="dita-ot:get-current-language(.)"/>
  22. </xsl:template>
  23. <xsl:template match="*" mode="get-first-topic-lang">
  24. <xsl:sequence select="dita-ot:get-first-topic-language(.)"/>
  25. </xsl:template>
  26. <xsl:template match="*" mode="get-render-direction">
  27. <xsl:param name="lang">
  28. <xsl:apply-templates select="/*" mode="get-first-topic-lang"/>
  29. </xsl:param>
  30. <xsl:variable name="l" select="tokenize($lang, '-')[1]" as="xs:string"/>
  31. <xsl:choose>
  32. <xsl:when test="$l = 'ar'">rtl</xsl:when>
  33. <xsl:when test="$l = 'arc'">rtl</xsl:when>
  34. <xsl:when test="$l = 'bcc'">rtl</xsl:when>
  35. <xsl:when test="$l = 'bqi'">rtl</xsl:when>
  36. <xsl:when test="$l = 'ckb'">rtl</xsl:when>
  37. <xsl:when test="$l = 'dv'">rtl</xsl:when>
  38. <xsl:when test="$l = 'fa'">rtl</xsl:when>
  39. <xsl:when test="$l = 'glk'">rtl</xsl:when>
  40. <xsl:when test="$l = 'he'">rtl</xsl:when>
  41. <xsl:when test="$l = 'lrc'">rtl</xsl:when>
  42. <xsl:when test="$l = 'mzn'">rtl</xsl:when>
  43. <xsl:when test="$l = 'pnb'">rtl</xsl:when>
  44. <xsl:when test="$l = 'ps'">rtl</xsl:when>
  45. <xsl:when test="$l = 'sd'">rtl</xsl:when>
  46. <xsl:when test="$l = 'ug'">rtl</xsl:when>
  47. <xsl:when test="$l = 'ur'">rtl</xsl:when>
  48. <xsl:when test="$l = 'yi'">rtl</xsl:when>
  49. <xsl:otherwise>ltr</xsl:otherwise>
  50. </xsl:choose>
  51. </xsl:template>
  52. <xsl:variable name="variableFiles" select="document($variableFiles.url)/langlist/lang" as="element(lang)*"/>
  53. <!-- Deprecated. Use getVariable template instead. -->
  54. <xsl:template name="getString">
  55. <xsl:param name="stringName"/>
  56. <xsl:call-template name="output-message">
  57. <xsl:with-param name="id" select="'DOTX066W'"/>
  58. <xsl:with-param name="msgparams">%1=getString</xsl:with-param>
  59. </xsl:call-template>
  60. <xsl:call-template name="getVariable">
  61. <xsl:with-param name="id" select="string($stringName)"/>
  62. </xsl:call-template>
  63. </xsl:template>
  64. <xsl:template name="getVariable">
  65. <xsl:param name="id" as="xs:string"/>
  66. <xsl:param name="params" as="node()*"/>
  67. <xsl:param name="ctx" as="node()" select="."/>
  68. <xsl:sequence select="dita-ot:get-variable($ctx, $id, $params)"/>
  69. </xsl:template>
  70. <xsl:template name="findString">
  71. <xsl:param name="id" as="xs:string"/>
  72. <xsl:param name="params" as="node()*"/>
  73. <xsl:param name="ancestorlang" as="xs:string*"/>
  74. <xsl:param name="defaultlang" as="xs:string*"/>
  75. <xsl:param name="originallang" as="xs:string*" select="$ancestorlang[1]"/>
  76. <xsl:variable name="l" select="($ancestorlang, $defaultlang)[1]" as="xs:string?"/>
  77. <xsl:choose>
  78. <xsl:when test="exists($l)">
  79. <xsl:variable name="variablefile" select="$variableFiles[lower-case(@xml:lang) = lower-case($l)]/@filename" as="xs:string*"/>
  80. <xsl:variable name="variable" as="element()*">
  81. <xsl:for-each select="$variablefile">
  82. <xsl:sequence select="document(., $variableFiles[1])/*/*[@name = $id or @id = $id]"/><!-- strings/str/@name opentopic-vars:vars/opentopic-vars:variable/@id -->
  83. </xsl:for-each>
  84. </xsl:variable>
  85. <xsl:choose>
  86. <xsl:when test="exists($variable)">
  87. <xsl:apply-templates select="$variable[last()]" mode="processVariableBody">
  88. <xsl:with-param name="params" select="$params"/>
  89. </xsl:apply-templates>
  90. <xsl:if test="empty($ancestorlang)">
  91. <xsl:call-template name="output-message">
  92. <xsl:with-param name="id" select="'DOTX001W'"/>
  93. <xsl:with-param name="msgparams">%1=<xsl:value-of select="$id"/>;%2=<xsl:value-of select="$originallang"/>;%3=<xsl:value-of select="$DEFAULTLANG"/></xsl:with-param>
  94. </xsl:call-template>
  95. </xsl:if>
  96. </xsl:when>
  97. <xsl:otherwise>
  98. <xsl:call-template name="findString">
  99. <xsl:with-param name="id" select="$id"/>
  100. <xsl:with-param name="params" select="$params"/>
  101. <xsl:with-param name="ancestorlang" select="$ancestorlang[position() gt 1]"/>
  102. <xsl:with-param name="defaultlang" select="if (exists($ancestorlang)) then $defaultlang else $defaultlang[position() gt 1]"/>
  103. <xsl:with-param name="originallang" select="$originallang"/>
  104. </xsl:call-template>
  105. </xsl:otherwise>
  106. </xsl:choose>
  107. </xsl:when>
  108. <xsl:otherwise>
  109. <xsl:variable name="variablefile" select="$variableFiles[@xml:lang='']/@filename" as="xs:string*"/>
  110. <xsl:variable name="variable" as="element()*">
  111. <xsl:for-each select="$variablefile">
  112. <xsl:sequence select="document(., $variableFiles[1])/*/*[@name = $id or @id = $id]"/>
  113. </xsl:for-each>
  114. </xsl:variable>
  115. <xsl:choose>
  116. <xsl:when test="exists($variable)">
  117. <xsl:apply-templates select="$variable[last()]" mode="processVariableBody">
  118. <xsl:with-param name="params" select="$params"/>
  119. </xsl:apply-templates>
  120. </xsl:when>
  121. <xsl:otherwise>
  122. <xsl:value-of select="$id"/>
  123. <xsl:call-template name="output-message">
  124. <xsl:with-param name="id" select="'DOTX052W'"/>
  125. <xsl:with-param name="msgparams">%1=<xsl:value-of select="$id"/></xsl:with-param>
  126. </xsl:call-template>
  127. </xsl:otherwise>
  128. </xsl:choose>
  129. </xsl:otherwise>
  130. </xsl:choose>
  131. </xsl:template>
  132. <!-- Support legacy variable syntax -->
  133. <xsl:template match="str" mode="processVariableBody">
  134. <xsl:param name="params"/>
  135. <xsl:copy-of select="node()"/>
  136. </xsl:template>
  137. <xsl:template match="variable" mode="processVariableBody">
  138. <xsl:param name="params" as="node()*"/>
  139. <xsl:for-each select="node()">
  140. <xsl:choose>
  141. <xsl:when test="self::param">
  142. <xsl:variable name="param-name" select="@ref-name" as="xs:string"/>
  143. <xsl:copy-of select="$params/descendant-or-self::*[name() = $param-name]/node()"/>
  144. </xsl:when>
  145. <xsl:when test="self::variableref">
  146. <xsl:call-template name="getVariable">
  147. <xsl:with-param name="id" select="@refid"/>
  148. <xsl:with-param name="params" select="$params"/>
  149. </xsl:call-template>
  150. </xsl:when>
  151. <xsl:otherwise>
  152. <xsl:copy-of select="."/>
  153. </xsl:otherwise>
  154. </xsl:choose>
  155. </xsl:for-each>
  156. </xsl:template>
  157. <xsl:template name="length-to-pixels">
  158. <xsl:param name="dimen"/>
  159. <!-- We handle units of cm, mm, in, pt, pc, px. We also accept em,
  160. but just treat 1em=1pc. An omitted unit is taken as px. -->
  161. <xsl:variable name="units" select="substring($dimen, string-length($dimen) - 1)"/>
  162. <xsl:variable name="numeric-value" select="number(substring($dimen, 1, string-length($dimen) - 2))"/>
  163. <xsl:choose>
  164. <xsl:when test="string(number($dimen)) != 'NaN'">
  165. <!-- Since $units is a number, the input was unitless, so we default
  166. the unit to pixels and just return the input value -->
  167. <xsl:value-of select="round(number($dimen))"/>
  168. </xsl:when>
  169. <xsl:when test="string($numeric-value) = 'NaN'">
  170. <!-- If the input isn't valid, just return 100% -->
  171. <xsl:value-of select="'100%'"/>
  172. </xsl:when>
  173. <xsl:when test="$units='cm'">
  174. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 2.54)"/>
  175. </xsl:when>
  176. <xsl:when test="$units='mm'">
  177. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 25.4)"/>
  178. </xsl:when>
  179. <xsl:when test="$units='in'">
  180. <xsl:value-of select="round($numeric-value * $pixels-per-inch)"/>
  181. </xsl:when>
  182. <xsl:when test="$units='pt'">
  183. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 72)"/>
  184. </xsl:when>
  185. <xsl:when test="$units='pc'">
  186. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 6)"/>
  187. </xsl:when>
  188. <xsl:when test="$units='px'">
  189. <xsl:value-of select="round($numeric-value)"/>
  190. </xsl:when>
  191. <xsl:when test="$units='em'">
  192. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 6)"/>
  193. </xsl:when>
  194. <xsl:otherwise>
  195. <!-- If the input isn't valid, just return 100% -->
  196. <xsl:value-of select="'100%'"/>
  197. </xsl:otherwise>
  198. </xsl:choose>
  199. </xsl:template>
  200. <xsl:template name="replace">
  201. <xsl:param name="text" as="xs:string?"/>
  202. <xsl:param name="from" as="xs:string"/>
  203. <xsl:param name="to"/>
  204. <xsl:choose>
  205. <xsl:when test="contains($text, $from)">
  206. <xsl:sequence select="substring-before($text, $from)[string-length(.) gt 0]"/>
  207. <xsl:copy-of select="$to"/>
  208. <xsl:call-template name="replace">
  209. <xsl:with-param name="text" select="substring-after($text, $from)[string-length(.) gt 0]"/>
  210. <xsl:with-param name="from" select="$from"/>
  211. <xsl:with-param name="to" select="$to"/>
  212. </xsl:call-template>
  213. </xsl:when>
  214. <xsl:otherwise>
  215. <xsl:sequence select="$text"/>
  216. </xsl:otherwise>
  217. </xsl:choose>
  218. </xsl:template>
  219. <!-- replace all the blank in file name or directory with %20 -->
  220. <xsl:template name="replace-blank">
  221. <xsl:param name="file-origin"></xsl:param>
  222. <xsl:choose>
  223. <xsl:when test="contains($file-origin,' ')">
  224. <xsl:call-template name="replace-blank">
  225. <xsl:with-param name="file-origin">
  226. <xsl:value-of select="substring-before($file-origin,' ')"/>%20<xsl:value-of select="substring-after($file-origin,' ')"/>
  227. </xsl:with-param>
  228. </xsl:call-template>
  229. </xsl:when>
  230. <xsl:otherwise>
  231. <xsl:value-of select="$file-origin"/>
  232. </xsl:otherwise>
  233. </xsl:choose>
  234. </xsl:template>
  235. <!-- Return the portion of an HREF value up to the file's extension. This assumes
  236. that the file has an extension, and that the topic and/or element ID does not
  237. contain a period. Written to allow references such as com.example.dita.files/file.dita#topic -->
  238. <!-- Deprecated: use replace-extension instead -->
  239. <xsl:template match="*" mode="parseHrefUptoExtension">
  240. <xsl:param name="href" select="@href"/>
  241. <xsl:call-template name="output-message">
  242. <xsl:with-param name="id" select="'DOTX069W'"/>
  243. <xsl:with-param name="msgparams">%1=parseHrefUptoExtension</xsl:with-param>
  244. </xsl:call-template>
  245. <xsl:variable name="uptoDot" select="substring-before($href,'.')" as="xs:string"/>
  246. <xsl:variable name="afterDot" select="substring-after($href,'.')" as="xs:string"/>
  247. <xsl:value-of select="$uptoDot"/>
  248. <xsl:choose>
  249. <!-- No more periods, so this is at the extension -->
  250. <xsl:when test="not(contains($afterDot,'.'))"/>
  251. <!-- Multiple slashes; at least one must be a directory, so it's before the extension -->
  252. <xsl:when test="contains(substring-after($afterDot,'/'),'/')">
  253. <xsl:text>.</xsl:text>
  254. <xsl:value-of select="substring-before($afterDot,'/')"/>
  255. <xsl:text>/</xsl:text>
  256. <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="substring-after($afterDot,'/')"/></xsl:apply-templates>
  257. </xsl:when>
  258. <!-- Multiple periods, no slashes, no topic or element ID, so the file name contains more periods -->
  259. <xsl:when test="not(contains($afterDot,'#'))">
  260. <xsl:text>.</xsl:text>
  261. <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="$afterDot"/></xsl:apply-templates>
  262. </xsl:when>
  263. <!-- Multiple periods, no slashes, with #. Move to next period. Needs additional work to support
  264. IDs containing periods. -->
  265. <xsl:otherwise>
  266. <xsl:text>.</xsl:text>
  267. <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="$afterDot"/></xsl:apply-templates>
  268. </xsl:otherwise>
  269. </xsl:choose>
  270. </xsl:template>
  271. <!-- Get filename base -->
  272. <xsl:template name="getFileName">
  273. <xsl:param name="filename"/>
  274. <xsl:param name="extension"/>
  275. <xsl:choose>
  276. <xsl:when test="contains($filename, $extension)">
  277. <xsl:call-template name="substring-before-last">
  278. <xsl:with-param name="text" select="$filename"/>
  279. <xsl:with-param name="delim" select="$extension"/>
  280. </xsl:call-template>
  281. </xsl:when>
  282. <xsl:otherwise>
  283. <xsl:value-of select="$filename"/>
  284. </xsl:otherwise>
  285. </xsl:choose>
  286. </xsl:template>
  287. <!-- Replace file extension in a URI -->
  288. <xsl:template name="replace-extension" as="xs:string">
  289. <xsl:param name="filename" as="xs:string"/>
  290. <xsl:param name="extension" as="xs:string"/>
  291. <xsl:param name="ignore-fragment" select="false()"/>
  292. <xsl:variable name="f" as="xs:string">
  293. <xsl:value-of>
  294. <xsl:call-template name="substring-before-last">
  295. <xsl:with-param name="text">
  296. <xsl:choose>
  297. <xsl:when test="contains($filename, '#')">
  298. <xsl:value-of select="substring-before($filename, '#')"/>
  299. </xsl:when>
  300. <xsl:otherwise>
  301. <xsl:value-of select="$filename"/>
  302. </xsl:otherwise>
  303. </xsl:choose>
  304. </xsl:with-param>
  305. <xsl:with-param name="delim" select="'.'"/>
  306. </xsl:call-template>
  307. </xsl:value-of>
  308. </xsl:variable>
  309. <xsl:value-of>
  310. <xsl:if test="string($f)">
  311. <xsl:value-of select="concat($f, $extension)"/>
  312. </xsl:if>
  313. <xsl:if test="not($ignore-fragment) and contains($filename, '#')">
  314. <xsl:value-of select="concat('#', substring-after($filename, '#'))"/>
  315. </xsl:if>
  316. </xsl:value-of>
  317. </xsl:template>
  318. <xsl:function name="dita-ot:substring-before-last" as="xs:string?">
  319. <xsl:param name="text" as="xs:string"/>
  320. <xsl:param name="delim" as="xs:string"/>
  321. <xsl:call-template name="substring-before-last">
  322. <xsl:with-param name="text" select="$text"/>
  323. <xsl:with-param name="delim" select="$delim" />
  324. </xsl:call-template>
  325. </xsl:function>
  326. <xsl:template name="substring-before-last" as="xs:string?">
  327. <xsl:param name="text" as="xs:string"/>
  328. <xsl:param name="delim" as="xs:string"/>
  329. <xsl:param name="acc" as="xs:string?"/>
  330. <xsl:choose>
  331. <xsl:when test="string($text) and string($delim)">
  332. <xsl:variable name="tail" select="substring-after($text, $delim)" />
  333. <xsl:choose>
  334. <xsl:when test="contains($tail, $delim)">
  335. <xsl:call-template name="substring-before-last">
  336. <xsl:with-param name="text" select="$tail" />
  337. <xsl:with-param name="delim" select="$delim" />
  338. <xsl:with-param name="acc" select="concat($acc, substring-before($text, $delim), $delim)"/>
  339. </xsl:call-template>
  340. </xsl:when>
  341. <xsl:otherwise>
  342. <xsl:value-of select="concat($acc, substring-before($text, $delim))"/>
  343. </xsl:otherwise>
  344. </xsl:choose>
  345. </xsl:when>
  346. <xsl:otherwise>
  347. <xsl:sequence select="$acc"/>
  348. </xsl:otherwise>
  349. </xsl:choose>
  350. </xsl:template>
  351. <xsl:template match="processing-instruction('workdir-uri')" mode="get-work-dir">
  352. <xsl:value-of select="."/>
  353. </xsl:template>
  354. <xsl:template match="processing-instruction('path2project-uri')" mode="get-path2project">
  355. <xsl:choose>
  356. <!-- Backwards compatibility with path2project that is empty when current directory is the root directory -->
  357. <xsl:when test=". = './'"/>
  358. <xsl:otherwise>
  359. <xsl:value-of select="."/>
  360. </xsl:otherwise>
  361. </xsl:choose>
  362. </xsl:template>
  363. <xsl:template match="processing-instruction('path2project')" mode="get-path2project">
  364. <xsl:call-template name="get-path2project">
  365. <xsl:with-param name="s" select="."/>
  366. </xsl:call-template>
  367. </xsl:template>
  368. <xsl:template name="get-path2project">
  369. <!-- Deal with being handed a Windows backslashed path by accident. -->
  370. <!-- This code only changes \ to / and doesn't handle the many other situations
  371. where a URI differs from a file path. Hopefully they don't occur in path2proj anyway. -->
  372. <xsl:param name="s"/>
  373. <xsl:choose>
  374. <xsl:when test="contains($s, '\')">
  375. <xsl:value-of select="substring-before($s, '\')"/>
  376. <xsl:text>/</xsl:text>
  377. <xsl:call-template name="get-path2project">
  378. <xsl:with-param name="s" select="substring-after($s, '\')"/>
  379. </xsl:call-template>
  380. </xsl:when>
  381. <xsl:otherwise>
  382. <xsl:value-of select="$s"/>
  383. </xsl:otherwise>
  384. </xsl:choose>
  385. </xsl:template>
  386. <xsl:function name="dita-ot:get-closest-topic" as="element()">
  387. <xsl:param name="n" as="node()"/>
  388. <xsl:sequence
  389. select="$n/ancestor-or-self::*[contains(@class, ' topic/topic ')][1]"/>
  390. </xsl:function>
  391. <xsl:include href="uri-utils.xsl"/>
  392. </xsl:stylesheet>