lib.xsl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- ********************************************************************
  3. $Id$
  4. ********************************************************************
  5. This file is part of the XSL DocBook Stylesheet distribution.
  6. See ../README or http://nwalsh.com/docbook/xsl/ for copyright
  7. and other information.
  8. This module implements DTD-independent functions
  9. ******************************************************************** -->
  10. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:src="http://nwalsh.com/xmlns/litprog/fragment" xmlns:dyn="http://exslt.org/dynamic" xmlns:saxon="http://icl.com/saxon" exclude-result-prefixes="src" version="1.0">
  11. <xsl:template name="dot.count">
  12. <!-- Returns the number of "." characters in a string -->
  13. <xsl:param name="string"/>
  14. <xsl:param name="count" select="0"/>
  15. <xsl:choose>
  16. <xsl:when test="contains($string, '.')">
  17. <xsl:call-template name="dot.count">
  18. <xsl:with-param name="string" select="substring-after($string, '.')"/>
  19. <xsl:with-param name="count" select="$count+1"/>
  20. </xsl:call-template>
  21. </xsl:when>
  22. <xsl:otherwise>
  23. <xsl:value-of select="$count"/>
  24. </xsl:otherwise>
  25. </xsl:choose>
  26. </xsl:template>
  27. <xsl:template name="copy-string">
  28. <!-- returns 'count' copies of 'string' -->
  29. <xsl:param name="string"/>
  30. <xsl:param name="count" select="0"/>
  31. <xsl:param name="result"/>
  32. <xsl:choose>
  33. <xsl:when test="$count&gt;0">
  34. <xsl:call-template name="copy-string">
  35. <xsl:with-param name="string" select="$string"/>
  36. <xsl:with-param name="count" select="$count - 1"/>
  37. <xsl:with-param name="result">
  38. <xsl:value-of select="$result"/>
  39. <xsl:value-of select="$string"/>
  40. </xsl:with-param>
  41. </xsl:call-template>
  42. </xsl:when>
  43. <xsl:otherwise>
  44. <xsl:value-of select="$result"/>
  45. </xsl:otherwise>
  46. </xsl:choose>
  47. </xsl:template>
  48. <xsl:template name="string.subst">
  49. <xsl:param name="string"/>
  50. <xsl:param name="target"/>
  51. <xsl:param name="replacement"/>
  52. <xsl:choose>
  53. <xsl:when test="contains($string, $target)">
  54. <xsl:variable name="rest">
  55. <xsl:call-template name="string.subst">
  56. <xsl:with-param name="string" select="substring-after($string, $target)"/>
  57. <xsl:with-param name="target" select="$target"/>
  58. <xsl:with-param name="replacement" select="$replacement"/>
  59. </xsl:call-template>
  60. </xsl:variable>
  61. <xsl:value-of select="concat(substring-before($string, $target), $replacement, $rest)"/>
  62. </xsl:when>
  63. <xsl:otherwise>
  64. <xsl:value-of select="$string"/>
  65. </xsl:otherwise>
  66. </xsl:choose>
  67. </xsl:template>
  68. <xsl:template name="xpointer.idref">
  69. <xsl:param name="xpointer">http://...</xsl:param>
  70. <xsl:choose>
  71. <xsl:when test="starts-with($xpointer, '#xpointer(id(')">
  72. <xsl:variable name="rest" select="substring-after($xpointer, '#xpointer(id(')"/>
  73. <xsl:variable name="quote" select="substring($rest, 1, 1)"/>
  74. <xsl:value-of select="substring-before(substring-after($xpointer, $quote), $quote)"/>
  75. </xsl:when>
  76. <xsl:when test="starts-with($xpointer, '#')">
  77. <xsl:value-of select="substring-after($xpointer, '#')"/>
  78. </xsl:when>
  79. <!-- otherwise it's a pointer to some other document -->
  80. </xsl:choose>
  81. </xsl:template>
  82. <xsl:template name="length-magnitude">
  83. <xsl:param name="length" select="'0pt'"/>
  84. <xsl:choose>
  85. <xsl:when test="string-length($length) = 0"/>
  86. <xsl:when test="substring($length,1,1) = '0' or substring($length,1,1) = '1' or substring($length,1,1) = '2' or substring($length,1,1) = '3' or substring($length,1,1) = '4' or substring($length,1,1) = '5' or substring($length,1,1) = '6' or substring($length,1,1) = '7' or substring($length,1,1) = '8' or substring($length,1,1) = '9' or substring($length,1,1) = '.'">
  87. <xsl:value-of select="substring($length,1,1)"/>
  88. <xsl:call-template name="length-magnitude">
  89. <xsl:with-param name="length" select="substring($length,2)"/>
  90. </xsl:call-template>
  91. </xsl:when>
  92. </xsl:choose>
  93. </xsl:template>
  94. <xsl:template name="length-units">
  95. <xsl:param name="length" select="'0pt'"/>
  96. <xsl:param name="default.units" select="'px'"/>
  97. <xsl:variable name="magnitude">
  98. <xsl:call-template name="length-magnitude">
  99. <xsl:with-param name="length" select="$length"/>
  100. </xsl:call-template>
  101. </xsl:variable>
  102. <xsl:variable name="units">
  103. <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
  104. </xsl:variable>
  105. <xsl:choose>
  106. <xsl:when test="$units = ''">
  107. <xsl:value-of select="$default.units"/>
  108. </xsl:when>
  109. <xsl:otherwise>
  110. <xsl:value-of select="$units"/>
  111. </xsl:otherwise>
  112. </xsl:choose>
  113. </xsl:template>
  114. <xsl:template name="length-spec">
  115. <xsl:param name="length" select="'0pt'"/>
  116. <xsl:param name="default.units" select="'px'"/>
  117. <xsl:variable name="magnitude">
  118. <xsl:call-template name="length-magnitude">
  119. <xsl:with-param name="length" select="$length"/>
  120. </xsl:call-template>
  121. </xsl:variable>
  122. <xsl:variable name="units">
  123. <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
  124. </xsl:variable>
  125. <xsl:value-of select="$magnitude"/>
  126. <xsl:choose>
  127. <xsl:when test="$units='cm' or $units='mm' or $units='in' or $units='pt' or $units='pc' or $units='px' or $units='em'">
  128. <xsl:value-of select="$units"/>
  129. </xsl:when>
  130. <xsl:when test="$units = ''">
  131. <xsl:value-of select="$default.units"/>
  132. </xsl:when>
  133. <xsl:otherwise>
  134. <xsl:message>
  135. <xsl:text>Unrecognized unit of measure: </xsl:text>
  136. <xsl:value-of select="$units"/>
  137. <xsl:text>.</xsl:text>
  138. </xsl:message>
  139. </xsl:otherwise>
  140. </xsl:choose>
  141. </xsl:template>
  142. <xsl:template name="length-in-points">
  143. <xsl:param name="length" select="'0pt'"/>
  144. <xsl:param name="em.size" select="10"/>
  145. <xsl:param name="pixels.per.inch" select="90"/>
  146. <xsl:variable name="magnitude">
  147. <xsl:call-template name="length-magnitude">
  148. <xsl:with-param name="length" select="$length"/>
  149. </xsl:call-template>
  150. </xsl:variable>
  151. <xsl:variable name="units">
  152. <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
  153. </xsl:variable>
  154. <xsl:choose>
  155. <xsl:when test="$units = 'pt'">
  156. <xsl:value-of select="$magnitude"/>
  157. </xsl:when>
  158. <xsl:when test="$units = 'cm'">
  159. <xsl:value-of select="$magnitude div 2.54 * 72.0"/>
  160. </xsl:when>
  161. <xsl:when test="$units = 'mm'">
  162. <xsl:value-of select="$magnitude div 25.4 * 72.0"/>
  163. </xsl:when>
  164. <xsl:when test="$units = 'in'">
  165. <xsl:value-of select="$magnitude * 72.0"/>
  166. </xsl:when>
  167. <xsl:when test="$units = 'pc'">
  168. <xsl:value-of select="$magnitude * 12.0"/>
  169. </xsl:when>
  170. <xsl:when test="$units = 'px'">
  171. <xsl:value-of select="$magnitude div $pixels.per.inch * 72.0"/>
  172. </xsl:when>
  173. <xsl:when test="$units = 'em'">
  174. <xsl:value-of select="$magnitude * $em.size"/>
  175. </xsl:when>
  176. <xsl:otherwise>
  177. <xsl:message>
  178. <xsl:text>Unrecognized unit of measure: </xsl:text>
  179. <xsl:value-of select="$units"/>
  180. <xsl:text>.</xsl:text>
  181. </xsl:message>
  182. </xsl:otherwise>
  183. </xsl:choose>
  184. </xsl:template>
  185. <xsl:template name="pi-attribute">
  186. <xsl:param name="pis" select="processing-instruction('BOGUS_PI')"/>
  187. <xsl:param name="attribute">filename</xsl:param>
  188. <xsl:param name="count">1</xsl:param>
  189. <xsl:choose>
  190. <xsl:when test="$count&gt;count($pis)">
  191. <!-- not found -->
  192. </xsl:when>
  193. <xsl:otherwise>
  194. <xsl:variable name="pi">
  195. <xsl:value-of select="$pis[$count]"/>
  196. </xsl:variable>
  197. <xsl:variable name="pivalue">
  198. <xsl:value-of select="concat(' ', normalize-space($pi))"/>
  199. </xsl:variable>
  200. <xsl:choose>
  201. <xsl:when test="contains($pivalue,concat(' ', $attribute, '='))">
  202. <xsl:variable name="rest" select="substring-after($pivalue,concat(' ', $attribute,'='))"/>
  203. <xsl:variable name="quote" select="substring($rest,1,1)"/>
  204. <xsl:value-of select="substring-before(substring($rest,2),$quote)"/>
  205. </xsl:when>
  206. <xsl:otherwise>
  207. <xsl:call-template name="pi-attribute">
  208. <xsl:with-param name="pis" select="$pis"/>
  209. <xsl:with-param name="attribute" select="$attribute"/>
  210. <xsl:with-param name="count" select="$count + 1"/>
  211. </xsl:call-template>
  212. </xsl:otherwise>
  213. </xsl:choose>
  214. </xsl:otherwise>
  215. </xsl:choose>
  216. </xsl:template>
  217. <xsl:template name="lookup.key">
  218. <xsl:param name="key" select="''"/>
  219. <xsl:param name="table" select="''"/>
  220. <xsl:if test="contains($table, ' ')">
  221. <xsl:choose>
  222. <xsl:when test="substring-before($table, ' ') = $key">
  223. <xsl:variable name="rest" select="substring-after($table, ' ')"/>
  224. <xsl:choose>
  225. <xsl:when test="contains($rest, ' ')">
  226. <xsl:value-of select="substring-before($rest, ' ')"/>
  227. </xsl:when>
  228. <xsl:otherwise>
  229. <xsl:value-of select="$rest"/>
  230. </xsl:otherwise>
  231. </xsl:choose>
  232. </xsl:when>
  233. <xsl:otherwise>
  234. <xsl:call-template name="lookup.key">
  235. <xsl:with-param name="key" select="$key"/>
  236. <xsl:with-param name="table" select="substring-after(substring-after($table,' '), ' ')"/>
  237. </xsl:call-template>
  238. </xsl:otherwise>
  239. </xsl:choose>
  240. </xsl:if>
  241. </xsl:template>
  242. <xsl:template name="xpath.location">
  243. <xsl:param name="node" select="."/>
  244. <xsl:param name="path" select="''"/>
  245. <xsl:variable name="next.path">
  246. <xsl:value-of select="local-name($node)"/>
  247. <xsl:if test="$path != ''">/</xsl:if>
  248. <xsl:value-of select="$path"/>
  249. </xsl:variable>
  250. <xsl:choose>
  251. <xsl:when test="$node/parent::*">
  252. <xsl:call-template name="xpath.location">
  253. <xsl:with-param name="node" select="$node/parent::*"/>
  254. <xsl:with-param name="path" select="$next.path"/>
  255. </xsl:call-template>
  256. </xsl:when>
  257. <xsl:otherwise>
  258. <xsl:text>/</xsl:text>
  259. <xsl:value-of select="$next.path"/>
  260. </xsl:otherwise>
  261. </xsl:choose>
  262. </xsl:template>
  263. <xsl:template name="comment-escape-string">
  264. <xsl:param name="string" select="''"/>
  265. <xsl:if test="starts-with($string, '-')">
  266. <xsl:text> </xsl:text>
  267. </xsl:if>
  268. <xsl:call-template name="comment-escape-string.recursive">
  269. <xsl:with-param name="string" select="$string"/>
  270. </xsl:call-template>
  271. <xsl:if test="substring($string, string-length($string), 1) = '-'">
  272. <xsl:text> </xsl:text>
  273. </xsl:if>
  274. </xsl:template>
  275. <xsl:template name="comment-escape-string.recursive">
  276. <xsl:param name="string" select="''"/>
  277. <xsl:choose>
  278. <xsl:when test="contains($string, '--')">
  279. <xsl:value-of select="substring-before($string, '--')"/>
  280. <xsl:value-of select="'- -'"/>
  281. <xsl:call-template name="comment-escape-string.recursive">
  282. <xsl:with-param name="string" select="substring-after($string, '--')"/>
  283. </xsl:call-template>
  284. </xsl:when>
  285. <xsl:otherwise>
  286. <xsl:value-of select="$string"/>
  287. </xsl:otherwise>
  288. </xsl:choose>
  289. </xsl:template>
  290. <xsl:template name="pad-string">
  291. <!-- * recursive template to right/left pad the value with -->
  292. <!-- * whatever padChar is passed in -->
  293. <xsl:param name="padChar" select="' '"/>
  294. <xsl:param name="leftRight">left</xsl:param>
  295. <xsl:param name="padVar"/>
  296. <xsl:param name="length"/>
  297. <xsl:choose>
  298. <xsl:when test="string-length($padVar) &lt; $length">
  299. <xsl:call-template name="pad-string">
  300. <xsl:with-param name="padChar" select="$padChar"/>
  301. <xsl:with-param name="leftRight" select="$leftRight"/>
  302. <xsl:with-param name="padVar">
  303. <xsl:choose>
  304. <!-- * determine whether string should be -->
  305. <!-- * right- or left-padded -->
  306. <xsl:when test="$leftRight = 'left'">
  307. <!-- * pad it to left -->
  308. <xsl:value-of select="concat($padChar,$padVar)"/>
  309. </xsl:when>
  310. <xsl:otherwise>
  311. <!-- * otherwise, right-pad the string -->
  312. <xsl:value-of select="concat($padVar,$padChar)"/>
  313. </xsl:otherwise>
  314. </xsl:choose>
  315. </xsl:with-param>
  316. <xsl:with-param name="length" select="$length"/>
  317. </xsl:call-template>
  318. </xsl:when>
  319. <xsl:otherwise>
  320. <xsl:value-of select="substring($padVar,string-length($padVar) - $length + 1)"/>
  321. </xsl:otherwise>
  322. </xsl:choose>
  323. </xsl:template>
  324. <xsl:template name="str.tokenize.keep.delimiters">
  325. <xsl:param name="string" select="''"/>
  326. <xsl:param name="delimiters" select="' '"/>
  327. <xsl:choose>
  328. <xsl:when test="not($string)"/>
  329. <xsl:when test="not($delimiters)">
  330. <xsl:call-template name="str.tokenize.keep.delimiters-characters">
  331. <xsl:with-param name="string" select="$string"/>
  332. </xsl:call-template>
  333. </xsl:when>
  334. <xsl:otherwise>
  335. <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
  336. <xsl:with-param name="string" select="$string"/>
  337. <xsl:with-param name="delimiters" select="$delimiters"/>
  338. </xsl:call-template>
  339. </xsl:otherwise>
  340. </xsl:choose>
  341. </xsl:template>
  342. <xsl:template name="str.tokenize.keep.delimiters-characters">
  343. <xsl:param name="string"/>
  344. <xsl:if test="$string">
  345. <token><xsl:value-of select="substring($string, 1, 1)"/></token>
  346. <xsl:call-template name="str.tokenize.keep.delimiters-characters">
  347. <xsl:with-param name="string" select="substring($string, 2)"/>
  348. </xsl:call-template>
  349. </xsl:if>
  350. </xsl:template>
  351. <xsl:template name="str.tokenize.keep.delimiters-delimiters">
  352. <xsl:param name="string"/>
  353. <xsl:param name="delimiters"/>
  354. <xsl:variable name="delimiter" select="substring($delimiters, 1, 1)"/>
  355. <xsl:choose>
  356. <xsl:when test="not($delimiter)">
  357. <token><xsl:value-of select="$string"/></token>
  358. </xsl:when>
  359. <xsl:when test="contains($string, $delimiter)">
  360. <xsl:if test="not(starts-with($string, $delimiter))">
  361. <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
  362. <xsl:with-param name="string" select="substring-before($string, $delimiter)"/>
  363. <xsl:with-param name="delimiters" select="substring($delimiters, 2)"/>
  364. </xsl:call-template>
  365. </xsl:if>
  366. <!-- output each delimiter -->
  367. <xsl:value-of select="$delimiter"/>
  368. <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
  369. <xsl:with-param name="string" select="substring-after($string, $delimiter)"/>
  370. <xsl:with-param name="delimiters" select="$delimiters"/>
  371. </xsl:call-template>
  372. </xsl:when>
  373. <xsl:otherwise>
  374. <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
  375. <xsl:with-param name="string" select="$string"/>
  376. <xsl:with-param name="delimiters" select="substring($delimiters, 2)"/>
  377. </xsl:call-template>
  378. </xsl:otherwise>
  379. </xsl:choose>
  380. </xsl:template>
  381. <xsl:template name="apply-string-subst-map">
  382. <xsl:param name="content"/>
  383. <xsl:param name="map.contents"/>
  384. <xsl:variable name="replaced_text">
  385. <xsl:call-template name="string.subst">
  386. <xsl:with-param name="string" select="$content"/>
  387. <xsl:with-param name="target" select="$map.contents[1]/@oldstring"/>
  388. <xsl:with-param name="replacement" select="$map.contents[1]/@newstring"/>
  389. </xsl:call-template>
  390. </xsl:variable>
  391. <xsl:choose>
  392. <xsl:when test="$map.contents[2]">
  393. <xsl:call-template name="apply-string-subst-map">
  394. <xsl:with-param name="content" select="$replaced_text"/>
  395. <xsl:with-param name="map.contents" select="$map.contents[position() &gt; 1]"/>
  396. </xsl:call-template>
  397. </xsl:when>
  398. <xsl:otherwise>
  399. <xsl:value-of select="$replaced_text"/>
  400. </xsl:otherwise>
  401. </xsl:choose>
  402. </xsl:template>
  403. <xsl:template name="apply-character-map">
  404. <xsl:param name="content"/>
  405. <xsl:param name="map.contents"/>
  406. <xsl:variable name="replaced_text">
  407. <xsl:call-template name="string.subst">
  408. <xsl:with-param name="string" select="$content"/>
  409. <xsl:with-param name="target" select="$map.contents[1]/@character"/>
  410. <xsl:with-param name="replacement" select="$map.contents[1]/@string"/>
  411. </xsl:call-template>
  412. </xsl:variable>
  413. <xsl:choose>
  414. <xsl:when test="$map.contents[2]">
  415. <xsl:call-template name="apply-character-map">
  416. <xsl:with-param name="content" select="$replaced_text"/>
  417. <xsl:with-param name="map.contents" select="$map.contents[position() &gt; 1]"/>
  418. </xsl:call-template>
  419. </xsl:when>
  420. <xsl:otherwise>
  421. <xsl:value-of select="$replaced_text"/>
  422. </xsl:otherwise>
  423. </xsl:choose>
  424. </xsl:template>
  425. <xsl:template name="read-character-map">
  426. <xsl:param name="use.subset"/>
  427. <xsl:param name="subset.profile"/>
  428. <xsl:param name="uri"/>
  429. <xsl:choose>
  430. <xsl:when test="$use.subset != 0">
  431. <!-- use a subset of the character map instead of the full map -->
  432. <xsl:choose>
  433. <!-- xsltproc and Xalan both support dyn:evaluate() -->
  434. <xsl:when test="function-available('dyn:evaluate')">
  435. <xsl:copy-of select="document($uri)//*[local-name()='output-character'] [dyn:evaluate($subset.profile)]"/>
  436. </xsl:when>
  437. <!-- Saxon has its own evaluate() & doesn't support dyn:evaluate() -->
  438. <xsl:when test="function-available('saxon:evaluate')">
  439. <xsl:copy-of select="document($uri)//*[local-name()='output-character'] [saxon:evaluate($subset.profile)]"/>
  440. </xsl:when>
  441. <xsl:otherwise>
  442. <xsl:message terminate="yes">
  443. Error: To process character-map subsets, you must use an XSLT engine
  444. that supports the evaluate() XSLT extension function. Your XSLT engine
  445. does not support it.
  446. </xsl:message>
  447. </xsl:otherwise>
  448. </xsl:choose>
  449. </xsl:when>
  450. <xsl:otherwise>
  451. <!-- value of $use.subset is non-zero, so use the full map -->
  452. <xsl:copy-of select="document($uri)//*[local-name()='output-character']"/>
  453. </xsl:otherwise>
  454. </xsl:choose>
  455. </xsl:template>
  456. <xsl:template name="count.uri.path.depth">
  457. <xsl:param name="filename" select="''"/>
  458. <xsl:param name="count" select="0"/>
  459. <xsl:choose>
  460. <xsl:when test="contains($filename, '/')">
  461. <xsl:call-template name="count.uri.path.depth">
  462. <xsl:with-param name="filename" select="substring-after($filename, '/')"/>
  463. <xsl:with-param name="count" select="$count + 1"/>
  464. </xsl:call-template>
  465. </xsl:when>
  466. <xsl:otherwise>
  467. <xsl:value-of select="$count"/>
  468. </xsl:otherwise>
  469. </xsl:choose>
  470. </xsl:template>
  471. <xsl:template name="trim.common.uri.paths">
  472. <xsl:param name="uriA" select="''"/>
  473. <xsl:param name="uriB" select="''"/>
  474. <xsl:param name="return" select="'A'"/>
  475. <xsl:choose>
  476. <xsl:when test="contains($uriA, '/') and contains($uriB, '/') and substring-before($uriA, '/') = substring-before($uriB, '/')">
  477. <xsl:call-template name="trim.common.uri.paths">
  478. <xsl:with-param name="uriA" select="substring-after($uriA, '/')"/>
  479. <xsl:with-param name="uriB" select="substring-after($uriB, '/')"/>
  480. <xsl:with-param name="return" select="$return"/>
  481. </xsl:call-template>
  482. </xsl:when>
  483. <xsl:otherwise>
  484. <xsl:choose>
  485. <xsl:when test="$return = 'A'">
  486. <xsl:value-of select="$uriA"/>
  487. </xsl:when>
  488. <xsl:otherwise>
  489. <xsl:value-of select="$uriB"/>
  490. </xsl:otherwise>
  491. </xsl:choose>
  492. </xsl:otherwise>
  493. </xsl:choose>
  494. </xsl:template>
  495. <xsl:template name="trim.text">
  496. <xsl:param name="contents" select="."/>
  497. <xsl:variable name="contents-left-trimmed">
  498. <xsl:call-template name="trim-left">
  499. <xsl:with-param name="contents" select="$contents"/>
  500. </xsl:call-template>
  501. </xsl:variable>
  502. <xsl:variable name="contents-trimmed">
  503. <xsl:call-template name="trim-right">
  504. <xsl:with-param name="contents" select="$contents-left-trimmed"/>
  505. </xsl:call-template>
  506. </xsl:variable>
  507. <xsl:value-of select="$contents-trimmed"/>
  508. </xsl:template>
  509. <xsl:template name="trim-left">
  510. <xsl:param name="contents"/>
  511. <xsl:choose>
  512. <xsl:when test="starts-with($contents,'&#xA;') or starts-with($contents,'&#xD;') or starts-with($contents,' ') or starts-with($contents,'&#x9;')">
  513. <xsl:call-template name="trim-left">
  514. <xsl:with-param name="contents" select="substring($contents, 2)"/>
  515. </xsl:call-template>
  516. </xsl:when>
  517. <xsl:otherwise>
  518. <xsl:value-of select="$contents"/>
  519. </xsl:otherwise>
  520. </xsl:choose>
  521. </xsl:template>
  522. <xsl:template name="trim-right">
  523. <xsl:param name="contents"/>
  524. <xsl:variable name="last-char">
  525. <xsl:value-of select="substring($contents, string-length($contents), 1)"/>
  526. </xsl:variable>
  527. <xsl:choose>
  528. <xsl:when test="($last-char = '&#xA;') or ($last-char = '&#xD;') or ($last-char = ' ') or ($last-char = '&#x9;')">
  529. <xsl:call-template name="trim-right">
  530. <xsl:with-param name="contents" select="substring($contents, 1, string-length($contents) - 1)"/>
  531. </xsl:call-template>
  532. </xsl:when>
  533. <xsl:otherwise>
  534. <xsl:value-of select="$contents"/>
  535. </xsl:otherwise>
  536. </xsl:choose>
  537. </xsl:template>
  538. </xsl:stylesheet>