dita-utilities.xsl 19 KB

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