formular.xsl 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. * This file is part of the LibreOffice project.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * This file incorporates work covered by the following license notice:
  10. *
  11. * Licensed to the Apache Software Foundation (ASF) under one or more
  12. * contributor license agreements. See the NOTICE file distributed
  13. * with this work for additional information regarding copyright
  14. * ownership. The ASF licenses this file to you under the Apache
  15. * License, Version 2.0 (the "License"); you may not use this file
  16. * except in compliance with the License. You may obtain a copy of
  17. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  18. -->
  19. <xsl:stylesheet version="1.0"
  20. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  21. xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
  22. xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0"
  23. xmlns:dc="http://purl.org/dc/elements/1.1/"
  24. xmlns:dom="http://www.w3.org/2001/xml-events"
  25. xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
  26. xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
  27. xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
  28. xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
  29. xmlns:math="http://www.w3.org/1998/Math/MathML"
  30. xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
  31. xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
  32. xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
  33. xmlns:ooo="http://openoffice.org/2004/office"
  34. xmlns:oooc="http://openoffice.org/2004/calc"
  35. xmlns:ooow="http://openoffice.org/2004/writer"
  36. xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
  37. xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
  38. xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
  39. xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
  40. xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
  41. xmlns:xlink="http://www.w3.org/1999/xlink"
  42. xmlns:xt="http://www.jclark.com/xt"
  43. xmlns:common="http://exslt.org/common"
  44. xmlns:xalan="http://xml.apache.org/xalan"
  45. xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46. exclude-result-prefixes="chart config dc dom dr3d draw fo form math meta number office ooo oooc ooow script style svg table text xlink xt common xalan">
  47. <!-- Mapping @table:formula to @ss:Formula translating the expression syntax -->
  48. <xsl:template match="@table:formula">
  49. <xsl:param name="calculatedCellPosition" />
  50. <xsl:param name="calculatedRowPosition" />
  51. <xsl:attribute name="ss:Formula">
  52. <xsl:call-template name="translate-formular-expression">
  53. <xsl:with-param name="rowPos" select="$calculatedRowPosition" />
  54. <xsl:with-param name="columnPos" select="$calculatedCellPosition" />
  55. <xsl:with-param name="expression" select="." />
  56. </xsl:call-template>
  57. </xsl:attribute>
  58. </xsl:template>
  59. <!-- Translate OOOC formula expressions of table cells to spreadsheetml expression
  60. For example:
  61. "oooc:=ROUNDDOWN(123.321;2)"
  62. to "=ROUNDDOWN(123.321,2)"
  63. "oooc:=([.B2]-[.C2])"
  64. to "=(RC[-2]-RC[-1])"
  65. "oooc:=DCOUNTA([.E14:.F21];[.F14];[.H14:.I15])"
  66. to "=DCOUNTA(R[-17]C[3]:R[-10]C[4],R[-17]C[4],R[-17]C[6]:R[-16]C[7])" -->
  67. <xsl:template name="translate-formular-expression">
  68. <!-- return position or range for formula or other -->
  69. <xsl:param name="rowPos" /> <!-- the position in row (vertical) of cell -->
  70. <xsl:param name="columnPos" /> <!-- the position in column (horizontal of cell) -->
  71. <xsl:param name="expression" /> <!-- the expression string to be converted -->
  72. <xsl:choose>
  73. <xsl:when test="$expression != ''">
  74. <xsl:choose>
  75. <!-- OASIS Open Document XML formular expressions -->
  76. <xsl:when test="starts-with($expression,'oooc:')">
  77. <!-- giving out the '=', which will be removed with 'oooc:=' to enable recursive string parsing -->
  78. <xsl:text>=</xsl:text>
  79. <xsl:call-template name="function-parameter-mapping">
  80. <xsl:with-param name="rowPos" select="$rowPos" />
  81. <xsl:with-param name="columnPos" select="$columnPos" />
  82. <!-- 1) remove 'oooc:=' prefix and exchange ';' with ',' -->
  83. <xsl:with-param name="expression" select="translate(substring($expression,7),';',',')"/>
  84. </xsl:call-template>
  85. </xsl:when>
  86. <xsl:otherwise>
  87. <xsl:value-of select="$expression" />
  88. </xsl:otherwise>
  89. </xsl:choose>
  90. </xsl:when>
  91. <xsl:otherwise>
  92. <xsl:value-of select="$expression" />
  93. </xsl:otherwise>
  94. </xsl:choose>
  95. </xsl:template>
  96. <!-- As the function API of our Office and MS Office show differences in the argumentlists,
  97. - sometimes the last parameter have to be neglected
  98. - sometimes a default have to be added
  99. these exchanges have to be done as well -->
  100. <xsl:template name="function-parameter-mapping">
  101. <xsl:param name="rowPos" /> <!-- the position in row (vertical of cell) -->
  102. <xsl:param name="columnPos" /> <!-- the position in column (horizontal of cell) -->
  103. <xsl:param name="expression" /> <!-- expression to be exchanged -->
  104. <!-- Choose if the expression contains one of the function, which might need changes -->
  105. <xsl:choose>
  106. <!-- if not contain one of the functions, which need parameter mapping -->
  107. <xsl:when test="not(contains($expression, 'ADDRESS(') or
  108. contains($expression, 'CEILING(') or
  109. contains($expression, 'FLOOR(') or
  110. contains($expression, 'IF(') or
  111. contains($expression, 'ROUND('))">
  112. <!-- simply translate possibly existing column & row references -->
  113. <xsl:call-template name="translate-oooc-expression">
  114. <xsl:with-param name="rowPos" select="$rowPos" />
  115. <xsl:with-param name="columnPos" select="$columnPos" />
  116. <xsl:with-param name="expression" select="$expression"/>
  117. </xsl:call-template>
  118. </xsl:when>
  119. <!-- functions to be mapped -->
  120. <xsl:otherwise>
  121. <xsl:variable name="functionPrefix" select="substring-before($expression, '(')" />
  122. <xsl:variable name="expressionSuffix" select="substring-after($expression, '(')" />
  123. <!-- translate in case the expression contains row/cell references aside of the function name -->
  124. <xsl:call-template name="translate-oooc-expression">
  125. <xsl:with-param name="rowPos" select="$rowPos" />
  126. <xsl:with-param name="columnPos" select="$columnPos" />
  127. <xsl:with-param name="expression" select="$functionPrefix"/>
  128. </xsl:call-template>
  129. <!-- Prefix do not include the bracket -->
  130. <xsl:text>(</xsl:text>
  131. <xsl:choose>
  132. <xsl:when test="not(contains($functionPrefix, 'ADDRESS') or
  133. contains($functionPrefix, 'CEILING') or
  134. contains($functionPrefix, 'FLOOR') or
  135. (contains($functionPrefix, 'IF') and not(
  136. contains($functionPrefix, 'COUNTIF') or
  137. contains($functionPrefix, 'SUMIF'))) or
  138. contains($functionPrefix, 'ROUND'))">
  139. <xsl:call-template name="function-parameter-mapping">
  140. <xsl:with-param name="rowPos" select="$rowPos" />
  141. <xsl:with-param name="columnPos" select="$columnPos" />
  142. <xsl:with-param name="expression" select="$expressionSuffix"/>
  143. </xsl:call-template>
  144. </xsl:when>
  145. <xsl:otherwise>
  146. <xsl:choose>
  147. <xsl:when test="contains($functionPrefix, 'ADDRESS')">
  148. <xsl:call-template name="find-parameters">
  149. <xsl:with-param name="rowPos" select="$rowPos" />
  150. <xsl:with-param name="columnPos" select="$columnPos" />
  151. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  152. <xsl:with-param name="parameterRemoval" select="4" />
  153. </xsl:call-template>
  154. </xsl:when>
  155. <xsl:when test="contains($functionPrefix, 'CEILING') or
  156. contains($functionPrefix, 'FLOOR')">
  157. <xsl:call-template name="find-parameters">
  158. <xsl:with-param name="rowPos" select="$rowPos" />
  159. <xsl:with-param name="columnPos" select="$columnPos" />
  160. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  161. <xsl:with-param name="parameterRemoval" select="3" />
  162. </xsl:call-template>
  163. </xsl:when>
  164. <xsl:when test="contains($functionPrefix, 'IF')">
  165. <xsl:if test="not(contains($functionPrefix, 'COUNTIF') or
  166. contains($functionPrefix, 'SUMIF'))">
  167. <xsl:call-template name="find-parameters">
  168. <xsl:with-param name="rowPos" select="$rowPos" />
  169. <xsl:with-param name="columnPos" select="$columnPos" />
  170. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  171. <xsl:with-param name="parameterAddition" select="'true'" />
  172. <xsl:with-param name="additonAfterLastParameter" select="2" />
  173. </xsl:call-template>
  174. </xsl:if>
  175. </xsl:when>
  176. <xsl:when test="contains($functionPrefix, 'ROUND')">
  177. <xsl:call-template name="find-parameters">
  178. <xsl:with-param name="rowPos" select="$rowPos" />
  179. <xsl:with-param name="columnPos" select="$columnPos" />
  180. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  181. <xsl:with-param name="parameterAddition" select="'null'" />
  182. <xsl:with-param name="additonAfterLastParameter" select="1" />
  183. </xsl:call-template>
  184. </xsl:when>
  185. </xsl:choose>
  186. </xsl:otherwise>
  187. </xsl:choose>
  188. </xsl:otherwise>
  189. </xsl:choose>
  190. </xsl:template>
  191. <!-- Each parameter of the argumentlist have to be determined.
  192. Due to the low level string functionality in XSLT it becomes a clumsy task -->
  193. <xsl:template name="find-parameters">
  194. <!-- used for mapping of row/column reference -->
  195. <xsl:param name="rowPos" /> <!-- the position in row (vertical of cell) -->
  196. <xsl:param name="columnPos" /> <!-- the position in column (horizontal of cell) -->
  197. <!-- used for mapping of parameter -->
  198. <xsl:param name="parameterRemoval" />
  199. <xsl:param name="parameterAddition" />
  200. <xsl:param name="additonAfterLastParameter" />
  201. <!-- used as helper to find a parameter -->
  202. <xsl:param name="expressionSuffix" />
  203. <xsl:param name="parameterNumber" select="1" />
  204. <xsl:variable name="parameter">
  205. <xsl:call-template name="getParameter">
  206. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  207. </xsl:call-template>
  208. </xsl:variable>
  209. <xsl:choose>
  210. <!-- if it is not the last parameter -->
  211. <xsl:when test="starts-with(substring-after($expressionSuffix, $parameter), ',')">
  212. <!-- searches the argument for functions to be mapped -->
  213. <xsl:if test="not($parameterRemoval = $parameterNumber)">
  214. <xsl:call-template name="function-parameter-mapping">
  215. <xsl:with-param name="rowPos" select="$rowPos" />
  216. <xsl:with-param name="columnPos" select="$columnPos" />
  217. <xsl:with-param name="expression">
  218. <xsl:choose>
  219. <!-- in case a character will be removed the preceding won't make a comma -->
  220. <xsl:when test="$parameterRemoval = ($parameterNumber + 1)">
  221. <xsl:value-of select="$parameter" />
  222. </xsl:when>
  223. <xsl:otherwise>
  224. <xsl:value-of select="concat($parameter, ',')" />
  225. </xsl:otherwise>
  226. </xsl:choose>
  227. </xsl:with-param>
  228. </xsl:call-template>
  229. </xsl:if>
  230. <!-- searches for the next parameter -->
  231. <xsl:call-template name="find-parameters">
  232. <xsl:with-param name="rowPos" select="$rowPos" />
  233. <xsl:with-param name="columnPos" select="$columnPos" />
  234. <xsl:with-param name="expressionSuffix" select="substring-after(substring-after($expressionSuffix, $parameter),',')"/>
  235. <xsl:with-param name="parameterAddition" select="$parameterAddition" />
  236. <xsl:with-param name="parameterRemoval" select="$parameterRemoval" />
  237. <xsl:with-param name="additonAfterLastParameter" select="$additonAfterLastParameter" />
  238. <xsl:with-param name="parameterNumber" select="$parameterNumber + 1" />
  239. </xsl:call-template>
  240. </xsl:when>
  241. <xsl:otherwise>
  242. <!-- the last parameter -->
  243. <xsl:choose>
  244. <xsl:when test="$parameterRemoval = $parameterNumber">
  245. <!-- searches the rest of the expression for functions to be mapped -->
  246. <xsl:call-template name="function-parameter-mapping">
  247. <xsl:with-param name="rowPos" select="$rowPos" />
  248. <xsl:with-param name="columnPos" select="$columnPos" />
  249. <xsl:with-param name="expression" select="substring-after($expressionSuffix, $parameter)"/>
  250. </xsl:call-template>
  251. </xsl:when>
  252. <xsl:when test="$parameterAddition and ($parameterNumber = $additonAfterLastParameter)">
  253. <!-- searches the rest of the expression for functions to be mapped -->
  254. <xsl:call-template name="function-parameter-mapping">
  255. <xsl:with-param name="rowPos" select="$rowPos" />
  256. <xsl:with-param name="columnPos" select="$columnPos" />
  257. <xsl:with-param name="expression" select="$parameter" />
  258. </xsl:call-template>
  259. <!-- searches last parameter and additional parameters for functions to be mapped -->
  260. <xsl:call-template name="function-parameter-mapping">
  261. <xsl:with-param name="rowPos" select="$rowPos" />
  262. <xsl:with-param name="columnPos" select="$columnPos" />
  263. <!-- for the final parameter the latter substring is the ')' -->
  264. <xsl:with-param name="expression" select="concat(',', $parameterAddition, substring-after($expressionSuffix, $parameter))"/>
  265. </xsl:call-template>
  266. </xsl:when>
  267. <xsl:otherwise>
  268. <!-- searches the argument for functions to be mapped -->
  269. <xsl:call-template name="function-parameter-mapping">
  270. <xsl:with-param name="rowPos" select="$rowPos" />
  271. <xsl:with-param name="columnPos" select="$columnPos" />
  272. <xsl:with-param name="expression" select="$parameter" />
  273. </xsl:call-template>
  274. <!-- searches the rest of the expression for functions to be mapped -->
  275. <xsl:call-template name="function-parameter-mapping">
  276. <xsl:with-param name="rowPos" select="$rowPos" />
  277. <xsl:with-param name="columnPos" select="$columnPos" />
  278. <xsl:with-param name="expression" select="substring-after($expressionSuffix, $parameter)"/>
  279. </xsl:call-template>
  280. </xsl:otherwise>
  281. </xsl:choose>
  282. </xsl:otherwise>
  283. </xsl:choose>
  284. </xsl:template>
  285. <xsl:template name="getParameter">
  286. <xsl:param name="closingBracketCount" select="0" />
  287. <xsl:param name="openingBracketCount" select="0" />
  288. <xsl:param name="expressionSuffix" />
  289. <xsl:param name="parameterCandidate">
  290. <xsl:choose>
  291. <!-- if there are multiple parameter -->
  292. <xsl:when test="contains(substring-before($expressionSuffix, ')'), ',')">
  293. <xsl:value-of select="substring-before($expressionSuffix, ',')"/>
  294. </xsl:when>
  295. <xsl:otherwise>
  296. <xsl:value-of select="substring-before($expressionSuffix, ')')"/>
  297. </xsl:otherwise>
  298. </xsl:choose>
  299. </xsl:param>
  300. <xsl:param name="earlierCandidate" select="$parameterCandidate" />
  301. <xsl:choose>
  302. <xsl:when test="contains($parameterCandidate, '(') or contains($parameterCandidate, ')')">
  303. <xsl:choose>
  304. <!-- contains only closing bracket(s) -->
  305. <xsl:when test="contains($parameterCandidate, '(') and not(contains($parameterCandidate, ')'))">
  306. <xsl:call-template name="getParameter">
  307. <xsl:with-param name="openingBracketCount" select="$openingBracketCount + 1" />
  308. <xsl:with-param name="closingBracketCount" select="$closingBracketCount" />
  309. <xsl:with-param name="parameterCandidate" select="substring-after($parameterCandidate, '(')" />
  310. <xsl:with-param name="earlierCandidate" select="$earlierCandidate" />
  311. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  312. </xsl:call-template>
  313. </xsl:when>
  314. <!-- contains only opening bracket(s) -->
  315. <xsl:when test="not(contains($parameterCandidate, '(')) and contains($parameterCandidate, ')')">
  316. <xsl:call-template name="getParameter">
  317. <xsl:with-param name="openingBracketCount" select="$openingBracketCount" />
  318. <xsl:with-param name="closingBracketCount" select="$closingBracketCount + 1" />
  319. <xsl:with-param name="parameterCandidate" select="substring-after($parameterCandidate, ')')" />
  320. <xsl:with-param name="earlierCandidate" select="$earlierCandidate" />
  321. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  322. </xsl:call-template>
  323. </xsl:when>
  324. <xsl:otherwise>
  325. <xsl:choose>
  326. <xsl:when test="string-length(substring-before($parameterCandidate, '(')) &lt;
  327. string-length(substring-before($parameterCandidate, ')'))">
  328. <xsl:call-template name="getParameter">
  329. <xsl:with-param name="openingBracketCount" select="$openingBracketCount + 1" />
  330. <xsl:with-param name="closingBracketCount" select="$closingBracketCount" />
  331. <xsl:with-param name="parameterCandidate" select="substring-after($parameterCandidate, '(')" />
  332. <xsl:with-param name="earlierCandidate" select="$earlierCandidate" />
  333. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  334. </xsl:call-template>
  335. </xsl:when>
  336. <xsl:otherwise>
  337. <xsl:call-template name="getParameter">
  338. <xsl:with-param name="openingBracketCount" select="$openingBracketCount" />
  339. <xsl:with-param name="closingBracketCount" select="$closingBracketCount + 1" />
  340. <xsl:with-param name="parameterCandidate" select="substring-after($parameterCandidate, ')')" />
  341. <xsl:with-param name="earlierCandidate" select="$earlierCandidate" />
  342. <xsl:with-param name="expressionSuffix" select="$expressionSuffix"/>
  343. </xsl:call-template>
  344. </xsl:otherwise>
  345. </xsl:choose>
  346. </xsl:otherwise>
  347. </xsl:choose>
  348. </xsl:when>
  349. <xsl:otherwise>
  350. <xsl:choose>
  351. <xsl:when test="$openingBracketCount = $closingBracketCount">
  352. <xsl:value-of select="$earlierCandidate" />
  353. </xsl:when>
  354. <xsl:otherwise>
  355. <xsl:value-of select="$earlierCandidate" />
  356. <xsl:variable name="parameterCandidate2">
  357. <xsl:variable name="formularAfterCandidate" select="substring-after($expressionSuffix, $earlierCandidate)" />
  358. <xsl:variable name="parameterTillBracket" select="concat(substring-before($formularAfterCandidate,')'),')')" />
  359. <xsl:variable name="parameterTillComma" select="substring-before(substring-after($expressionSuffix, $parameterTillBracket),',')" />
  360. <xsl:choose>
  361. <xsl:when test="string-length($parameterTillComma) &gt; 0 and
  362. not(contains($parameterTillComma, '('))">
  363. <xsl:choose>
  364. <xsl:when test="starts-with($formularAfterCandidate, ',')">
  365. <xsl:value-of select="concat(',',substring-before(substring-after($formularAfterCandidate,','),','))"/>
  366. </xsl:when>
  367. <xsl:otherwise>
  368. <xsl:value-of select="substring-before($formularAfterCandidate,',')"/>
  369. </xsl:otherwise>
  370. </xsl:choose>
  371. </xsl:when>
  372. <xsl:otherwise>
  373. <xsl:value-of select="$parameterTillBracket"/>
  374. </xsl:otherwise>
  375. </xsl:choose>
  376. </xsl:variable>
  377. <xsl:call-template name="getParameter">
  378. <xsl:with-param name="closingBracketCount" select="$closingBracketCount" />
  379. <xsl:with-param name="openingBracketCount" select="$openingBracketCount" />
  380. <xsl:with-param name="parameterCandidate" select="$parameterCandidate2" />
  381. <xsl:with-param name="earlierCandidate" select="$parameterCandidate2" />
  382. <xsl:with-param name="expressionSuffix" select="$expressionSuffix" />
  383. </xsl:call-template>
  384. </xsl:otherwise>
  385. </xsl:choose>
  386. </xsl:otherwise>
  387. </xsl:choose>
  388. </xsl:template>
  389. <!-- Mapping table-cell definitions by exchangomg all table cell definitions:
  390. a) a pair of cells e.g. "[.E14:.F21]" to "R[-17]C[3]:R[-10]C[4]"
  391. b) a single cell e.g. "[.F14]" to "R[-17]"-->
  392. <xsl:template name="translate-oooc-expression">
  393. <xsl:param name="rowPos" /> <!-- the position in row (vertical of cell) -->
  394. <xsl:param name="columnPos" /> <!-- the position in column (horizontal of cell) -->
  395. <xsl:param name="expression" /> <!-- expression to be exchanged -->
  396. <xsl:choose>
  397. <xsl:when test="contains($expression, '[')">
  398. <!-- Giving out the part before '[.' -->
  399. <xsl:value-of select="substring-before($expression, '[')" />
  400. <!-- Mapping cell definitions
  401. 1) a pair of cells e.g. "[.E14:.F21]" to "R[-17]C[3]:R[-10]C[4]"
  402. 2) a single cell e.g. "[.F14]" to "R[-17]"-->
  403. <xsl:variable name="remainingExpression" select="substring-after($expression, '[')"/>
  404. <xsl:choose>
  405. <xsl:when test="contains(substring-before($remainingExpression, ']'), ':')">
  406. <xsl:call-template name="translate-cell-expression">
  407. <xsl:with-param name="rowPos" select="$rowPos" />
  408. <xsl:with-param name="columnPos" select="$columnPos" />
  409. <xsl:with-param name="expression" select="substring-before($remainingExpression, ':')" />
  410. </xsl:call-template>
  411. <xsl:value-of select="':'" />
  412. <xsl:call-template name="translate-cell-expression">
  413. <xsl:with-param name="rowPos" select="$rowPos" />
  414. <xsl:with-param name="columnPos" select="$columnPos" />
  415. <xsl:with-param name="expression" select="substring-after(substring-before($remainingExpression, ']'), ':')" />
  416. </xsl:call-template>
  417. </xsl:when>
  418. <xsl:otherwise>
  419. <xsl:call-template name="translate-cell-expression">
  420. <xsl:with-param name="rowPos" select="$rowPos" />
  421. <xsl:with-param name="columnPos" select="$columnPos" />
  422. <xsl:with-param name="expression" select="substring-before($remainingExpression, ']')" />
  423. </xsl:call-template>
  424. </xsl:otherwise>
  425. </xsl:choose>
  426. <xsl:call-template name="translate-oooc-expression">
  427. <xsl:with-param name="rowPos" select="$rowPos" />
  428. <xsl:with-param name="columnPos" select="$columnPos" />
  429. <xsl:with-param name="expression" select="substring-after($remainingExpression,']')"/>
  430. </xsl:call-template>
  431. </xsl:when>
  432. <xsl:otherwise>
  433. <!-- Giving out the remaining part -->
  434. <xsl:value-of select="$expression" />
  435. </xsl:otherwise>
  436. </xsl:choose>
  437. </xsl:template>
  438. <!-- A cell expression has usually starts with a '.' otherwise it references to a sheet -->
  439. <xsl:template name="translate-cell-expression">
  440. <xsl:param name="rowPos" /> <!-- the vertical position of the current cell -->
  441. <xsl:param name="columnPos" /> <!-- the horizontal position of the current cell -->
  442. <xsl:param name="targetRowPos" select="0"/> <!-- the vertical position of the target cell -->
  443. <xsl:param name="targetColumnPos" select="0"/> <!-- the horizontal position of the target cell -->
  444. <xsl:param name="charPos" select="0"/> <!-- current column position (needed for multiplying) -->
  445. <xsl:param name="digitPos" select="0"/> <!-- current row position (needed for multiplying) -->
  446. <xsl:param name="expression" /> <!-- expression to be parsed by character -->
  447. <xsl:param name="isRow" select="true()"/> <!-- the string (e.g. $D39 is parsed character per character from the back,
  448. first the row, later the column is parsed -->
  449. <xsl:choose>
  450. <xsl:when test="starts-with($expression, '.')">
  451. <xsl:variable name="expLength" select="string-length($expression)" />
  452. <xsl:choose>
  453. <!-- parsing from the end, till only the '.' remains -->
  454. <xsl:when test="$expLength != 1">
  455. <xsl:variable name="token" select="substring($expression, $expLength)" />
  456. <xsl:choose>
  457. <xsl:when test="$token='0' or $token='1' or $token='2' or $token='3' or $token='4' or $token='5' or $token='6' or $token='7' or $token='8' or $token='9'">
  458. <xsl:variable name="multiplier">
  459. <xsl:call-template name="calculate-square-numbers">
  460. <xsl:with-param name="base" select="10" />
  461. <xsl:with-param name="exponent" select="$digitPos"/>
  462. </xsl:call-template>
  463. </xsl:variable>
  464. <xsl:call-template name="translate-cell-expression">
  465. <xsl:with-param name="columnPos" select="$columnPos" />
  466. <xsl:with-param name="rowPos" select="$rowPos" />
  467. <xsl:with-param name="targetColumnPos" select="$targetColumnPos" />
  468. <xsl:with-param name="targetRowPos" select="$targetRowPos + $multiplier * $token" />
  469. <xsl:with-param name="digitPos" select="$digitPos + 1" />
  470. <xsl:with-param name="charPos" select="$charPos" />
  471. <!-- removing the last character-->
  472. <xsl:with-param name="expression" select="substring($expression, 1, $expLength - 1)" />
  473. <xsl:with-param name="isRow" select="true()" />
  474. </xsl:call-template>
  475. </xsl:when>
  476. <xsl:when test="$token = '$'">
  477. <xsl:choose>
  478. <!-- if this is the first '$' after '.' (column-->
  479. <xsl:when test="$expLength = 2">
  480. <xsl:text>C</xsl:text><xsl:value-of select="$targetColumnPos"/>
  481. </xsl:when>
  482. <xsl:otherwise>
  483. <xsl:text>R</xsl:text><xsl:value-of select="$targetRowPos"/>
  484. <xsl:call-template name="translate-cell-expression">
  485. <xsl:with-param name="columnPos" select="$columnPos" />
  486. <xsl:with-param name="rowPos" select="$rowPos" />
  487. <xsl:with-param name="targetColumnPos" select="$targetColumnPos" />
  488. <xsl:with-param name="targetRowPos" select="$targetRowPos" />
  489. <xsl:with-param name="charPos" select="$charPos" />
  490. <!-- removing the last character-->
  491. <xsl:with-param name="expression" select="substring($expression, 1, $expLength - 1)" />
  492. <xsl:with-param name="isRow" select="false()" />
  493. </xsl:call-template>
  494. </xsl:otherwise>
  495. </xsl:choose>
  496. </xsl:when>
  497. <!-- in case of a letter -->
  498. <xsl:otherwise>
  499. <xsl:if test="$isRow">
  500. <xsl:text>R</xsl:text>
  501. <xsl:if test="$targetRowPos != $rowPos">
  502. <xsl:text>[</xsl:text><xsl:value-of select="$targetRowPos - $rowPos"/><xsl:text>]</xsl:text>
  503. </xsl:if>
  504. </xsl:if>
  505. <xsl:variable name="multiplier">
  506. <xsl:call-template name="calculate-square-numbers">
  507. <xsl:with-param name="base" select="26" />
  508. <xsl:with-param name="exponent" select="$charPos"/>
  509. </xsl:call-template>
  510. </xsl:variable>
  511. <xsl:variable name="tokenNumber">
  512. <xsl:call-template name="character-to-number">
  513. <xsl:with-param name="character" select="$token" />
  514. </xsl:call-template>
  515. </xsl:variable>
  516. <xsl:call-template name="translate-cell-expression">
  517. <xsl:with-param name="columnPos" select="$columnPos" />
  518. <xsl:with-param name="rowPos" select="$rowPos" />
  519. <xsl:with-param name="targetColumnPos" select="$targetColumnPos + $multiplier * $tokenNumber" />
  520. <xsl:with-param name="targetRowPos" select="$targetRowPos" />
  521. <xsl:with-param name="digitPos" select="$digitPos" />
  522. <xsl:with-param name="charPos" select="$charPos + 1" />
  523. <!-- removing the last character-->
  524. <xsl:with-param name="expression" select="substring($expression, 1, $expLength - 1)" />
  525. <xsl:with-param name="isRow" select="false()" />
  526. </xsl:call-template>
  527. </xsl:otherwise>
  528. </xsl:choose>
  529. </xsl:when>
  530. <xsl:otherwise>
  531. <xsl:text>C</xsl:text>
  532. <xsl:if test="$targetColumnPos != $columnPos">
  533. <xsl:text>[</xsl:text><xsl:value-of select="$targetColumnPos - $columnPos"/><xsl:text>]</xsl:text>
  534. </xsl:if>
  535. </xsl:otherwise>
  536. </xsl:choose>
  537. </xsl:when>
  538. <xsl:otherwise>
  539. <xsl:variable name="sheetName" select="substring-before($expression, '.')" />
  540. <xsl:value-of select="$sheetName"/><xsl:text>!</xsl:text>
  541. <xsl:call-template name="translate-cell-expression">
  542. <xsl:with-param name="rowPos" select="$rowPos" />
  543. <xsl:with-param name="columnPos" select="$columnPos" />
  544. <xsl:with-param name="expression" select="substring-after($expression, $sheetName)" />
  545. </xsl:call-template>
  546. </xsl:otherwise>
  547. </xsl:choose>
  548. </xsl:template>
  549. <xsl:template name="calculate-square-numbers">
  550. <xsl:param name="base" />
  551. <xsl:param name="exponent" />
  552. <xsl:param name="return" select="1" />
  553. <xsl:choose>
  554. <xsl:when test="$exponent > '1'">
  555. <xsl:call-template name="calculate-square-numbers">
  556. <xsl:with-param name="base" select="$base" />
  557. <xsl:with-param name="exponent" select="$exponent - 1"/>
  558. <xsl:with-param name="return" select="$return * $base" />
  559. </xsl:call-template>
  560. </xsl:when>
  561. <xsl:when test="$exponent = '1'">
  562. <xsl:value-of select="$return * $base"/>
  563. </xsl:when>
  564. <!-- if exponent is equal '0' -->
  565. <xsl:otherwise>
  566. <xsl:value-of select="1"/>
  567. </xsl:otherwise>
  568. </xsl:choose>
  569. </xsl:template>
  570. <xsl:template name="character-to-number">
  571. <xsl:param name="character" />
  572. <xsl:choose>
  573. <xsl:when test="$character = 'A'">1</xsl:when>
  574. <xsl:when test="$character = 'B'">2</xsl:when>
  575. <xsl:when test="$character = 'C'">3</xsl:when>
  576. <xsl:when test="$character = 'D'">4</xsl:when>
  577. <xsl:when test="$character = 'E'">5</xsl:when>
  578. <xsl:when test="$character = 'F'">6</xsl:when>
  579. <xsl:when test="$character = 'G'">7</xsl:when>
  580. <xsl:when test="$character = 'H'">8</xsl:when>
  581. <xsl:when test="$character = 'I'">9</xsl:when>
  582. <xsl:when test="$character = 'J'">10</xsl:when>
  583. <xsl:when test="$character = 'K'">11</xsl:when>
  584. <xsl:when test="$character = 'L'">12</xsl:when>
  585. <xsl:when test="$character = 'M'">13</xsl:when>
  586. <xsl:when test="$character = 'N'">14</xsl:when>
  587. <xsl:when test="$character = 'O'">15</xsl:when>
  588. <xsl:when test="$character = 'P'">16</xsl:when>
  589. <xsl:when test="$character = 'Q'">17</xsl:when>
  590. <xsl:when test="$character = 'R'">18</xsl:when>
  591. <xsl:when test="$character = 'S'">19</xsl:when>
  592. <xsl:when test="$character = 'T'">20</xsl:when>
  593. <xsl:when test="$character = 'U'">21</xsl:when>
  594. <xsl:when test="$character = 'V'">22</xsl:when>
  595. <xsl:when test="$character = 'W'">23</xsl:when>
  596. <xsl:when test="$character = 'X'">24</xsl:when>
  597. <xsl:when test="$character = 'Y'">25</xsl:when>
  598. <xsl:when test="$character = 'Z'">26</xsl:when>
  599. <xsl:otherwise/>
  600. </xsl:choose>
  601. </xsl:template>
  602. </xsl:stylesheet>