formular.xsl 30 KB

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