1<?xml version="1.0" encoding="UTF-8"?>
2<!--
3
4  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
6  Copyright 2000, 2010 Oracle and/or its affiliates.
7
8  OpenOffice.org - a multi-platform office productivity suite
9
10  This file is part of OpenOffice.org.
11
12  OpenOffice.org is free software: you can redistribute it and/or modify
13  it under the terms of the GNU Lesser General Public License version 3
14  only, as published by the Free Software Foundation.
15
16  OpenOffice.org is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU Lesser General Public License version 3 for more details
20  (a copy is included in the LICENSE file that accompanied this code).
21
22  You should have received a copy of the GNU Lesser General Public License
23  version 3 along with OpenOffice.org.  If not, see
24  <http://www.openoffice.org/license.html>
25  for a copy of the LGPLv3 License.
26
27-->
28<xsl:stylesheet version="1.0"
29	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
30	xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
31	xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0"
32	xmlns:dc="http://purl.org/dc/elements/1.1/"
33	xmlns:dom="http://www.w3.org/2001/xml-events"
34	xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
35	xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
36	xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
37	xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
38	xmlns:math="http://www.w3.org/1998/Math/MathML"
39	xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
40	xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
41	xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
42	xmlns:ooo="http://openoffice.org/2004/office"
43	xmlns:oooc="http://openoffice.org/2004/calc"
44	xmlns:ooow="http://openoffice.org/2004/writer"
45	xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
46	xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
47	xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
48	xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
49	xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
50	xmlns:xlink="http://www.w3.org/1999/xlink"
51	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"
52	exclude-result-prefixes="chart config dc dom dr3d draw fo form math meta number office ooo oooc ooow script style svg table text xlink">
53
54
55	<!--+++++ INCLUDED XSL MODULES +++++-->
56
57	<!-- helper collection, to convert measures (e.g. inch to pixel using DPI (dots per inch) parameter)-->
58	<xsl:import  href="../../common/measure_conversion.xsl" />
59
60	<!-- excel table handling -->
61	<xsl:include  href="table.xsl" />
62
63	<!-- mapping rules of office style properties to Excel style properties -->
64	<xsl:include href="style_mapping.xsl" />
65
66	<!-- creating the Excel styles element  -->
67	<xsl:include href="styles.xsl" />
68
69	<!-- mapping formalar Expressions  -->
70	<xsl:include href="formular.xsl" />
71
72	<xsl:output method               = "xml"
73				indent               = "no"
74				encoding             = "UTF-8"
75				omit-xml-declaration = "no" />
76
77	<xsl:strip-space elements="ss:Data html:Data" />
78
79
80	<!-- common table handling -->
81	<xsl:variable name="namespace" select="'urn:schemas-microsoft-com:office:spreadsheet'" />
82
83	<!--+++++ PARAMETER SECTION +++++-->
84
85	<!-- OPTIONAL: (MANDATORY: for all input document with relative external links): parameter is a (relative) URL to the target directory.
86		 Relative links from the office document (e.g. to external graphics) will get this parameter as a prefix -->
87	<xsl:param name="targetBaseURL" select="'./'" />
88
89	<!-- OPTIONAL: (MANDATORY: for input document with relative internal links)
90		 To access contents of a office file (content like the meta.xml, styles.xml file or  graphics) a URL could be choosen.
91	 This could be even a JAR URL. The sourceBase of the content URL "jar:file:/C:/temp/Test.sxw!/content.xml" would be
92	 "jar:file:/C:/temp/Test.sxw!/" for example.
93		 When working with OpenOffice API a Package-URL encoded over HTTP can be used to access the jared contents of the the jared document. -->
94	<xsl:param name="sourceBaseURL" select="'./'" />
95
96	<!-- OPTIONAL: (MANDATORY: for session management by URL rewriting)
97		 Useful for WebApplications: if a HTTP session is not cookie based, URL rewriting is beeing used (the session is appended to the URL).
98		 This URL session is used for example when links to graphics are created by XSLT. Otherwise the user havt to log again in for every graphic he liks to see. -->
99	<xsl:param name="optionalURLSuffix" />
100
101	<!-- OPTIONAL: URL to office meta file (flat xml use the URL to the input file) -->
102	<xsl:param name="metaFileURL" />
103
104	<!-- OPTIONAL: URL to office meta file (flat xml use the URL to the input file) -->
105	<xsl:param name="stylesFileURL" />
106
107	<!-- OPTIONAL: in case of using a different processor than a JAVA XSLT, you can unable the Java functionality
108		 (e.g. encoding chapter names for the content-table as href and anchors ) -->
109	<xsl:param name="java"        select="true()" />
110	<xsl:param name="javaEnabled" select="boolean($java)" />
111
112	<!-- OPTIONAL: for activating the debug mode set the variable here to 'true()' or give any value from outside -->
113	<xsl:param name="debug"                    select="false()" />
114	<xsl:param name="debugEnabled"             select="boolean($debug)" />
115
116	<!-- matching configuration entries -->
117	<xsl:key name="config" use="@config:name"
118			 match="/*/office:settings/config:config-item-set/config:config-item-map-indexed/config:config-item-map-entry/config:config-item |
119					/*/office:settings/config:config-item-set/config:config-item-map-indexed/config:config-item-map-entry/config:config-item-map-named/config:config-item-map-entry/config:config-item" />
120
121	<xsl:key name="colors" match="/*/office:styles//@*[name() = 'fo:background-color' or name() = 'fo:color'] |
122								  /*/office:automatic-styles//@*[name() = 'fo:background-color' or name() = 'fo:color']" use="/" />
123	<xsl:key name="colorRGB" match="@fo:background-color | @fo:color" use="." />
124	<!-- *************************** -->
125	<!-- *** Built up Excel file *** -->
126	<!-- *************************** -->
127	<xsl:template match="/">
128		<xsl:processing-instruction  name="mso-application">progid="Excel.Sheet"</xsl:processing-instruction>
129		<!-- Note: for debugging purpose include schema location
130		<Workbook xsi:schemaLocation="urn:schemas-microsoft-com:office:spreadsheet <YOUR_SCHEMA_URL>/excelss.xsd"> -->
131		<Workbook>
132			<!-- adding some default settings -->
133			<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
134				<Colors>
135					<xsl:for-each select="key('colors', /)
136						[generate-id(.) =
137						 generate-id(key('colorRGB', .)[1]) and starts-with(., '#')] ">
138					<xsl:sort select="." />
139					<Color>
140						<Index><xsl:value-of select="position() + 2" /></Index>
141						<RGB><xsl:value-of select="." /></RGB>
142					</Color>
143					</xsl:for-each>
144                    <xsl:for-each select="key('config', 'TabColor')[not(.=preceding::config:config-item)]">
145                    <xsl:sort select="." />
146                    <Color>
147                        <Index><xsl:value-of select="56 - position()" /></Index>
148                        <RGB>
149                            <xsl:call-template name="colordecimal2rgb">
150                                <xsl:with-param name="colordecimal" select="."/>
151                            </xsl:call-template>
152                        </RGB>
153                    </Color>
154                    </xsl:for-each>
155				</Colors>
156			</OfficeDocumentSettings>
157			<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
158				<xsl:if test="key('config', 'HasSheetTabs') = 'false'">
159						<xsl:element name="HideWorkbookTabs" />
160				</xsl:if>
161				<WindowHeight>9000</WindowHeight>
162				<WindowWidth>13860</WindowWidth>
163				<WindowTopX>240</WindowTopX>
164				<WindowTopY>75</WindowTopY>
165				<ProtectStructure>False</ProtectStructure>
166				<ProtectWindows>False</ProtectWindows>
167			</ExcelWorkbook>
168			<!-- Note: the following handling will exchange the default, later
169				<x:ExcelWorkbook>
170					<xsl:apply-templates select="table:calculation-settings" />
171				</x:ExcelWorkbook>
172			-->
173			<xsl:element name="Styles">
174				<!-- our application default will not be used for export to Excel
175				<xsl:apply-templates select="/*/office:styles/style:default-style" mode="styles" />-->
176				<xsl:apply-templates select="/*/office:styles/style:style" mode="styles" />
177				<xsl:apply-templates select="/*/office:automatic-styles/style:style" mode="styles" >
178					<xsl:with-param name="isAutomatic" select="true()" />
179				</xsl:apply-templates>
180			</xsl:element>
181			<xsl:apply-templates select="/*/office:body" />
182		</Workbook>
183	</xsl:template>
184
185    <xsl:template name="colordecimal2rgb">
186        <xsl:param name="colordecimal"/>
187        <xsl:choose>
188            <xsl:when test="$colordecimal &lt;= 16777215 and $colordecimal &gt;= 65536">
189                <xsl:variable name="redValue" select="floor(($colordecimal) div 65536)"/>
190                <xsl:variable name="greenValue" select="floor(($colordecimal - ($redValue*65536)) div 256)"/>
191                <xsl:variable name="blueValue" select="$colordecimal - ($redValue*65536) - ($greenValue*256)"/>
192                <xsl:call-template name="dec_rgb2Hex">
193                    <xsl:with-param name="decRedValue" select="$redValue"/>
194                    <xsl:with-param name="decGreenValue" select="$greenValue"/>
195                    <xsl:with-param name="decBlueValue" select="$blueValue"/>
196                </xsl:call-template>
197            </xsl:when>
198            <xsl:when test="$colordecimal &lt;= 65535 and $colordecimal &gt;= 256">
199                <xsl:variable name="redValue" select="0"/>
200                <xsl:variable name="greenValue" select="$colordecimal div 256"/>
201                <xsl:variable name="blueValue" select="$colordecimal - ($greenValue*256)"/>
202                <xsl:call-template name="dec_rgb2Hex">
203                    <xsl:with-param name="decRedValue" select="$redValue"/>
204                    <xsl:with-param name="decGreenValue" select="$greenValue"/>
205                    <xsl:with-param name="decBlueValue" select="$blueValue"/>
206                </xsl:call-template>
207            </xsl:when>
208            <xsl:when test="$colordecimal &lt;= 255 and $colordecimal &gt;= 0">
209                <xsl:variable name="redValue" select="0"/>
210                <xsl:variable name="greenValue" select="0"/>
211                <xsl:variable name="blueValue" select="$colordecimal"/>
212                <xsl:call-template name="dec_rgb2Hex">
213                    <xsl:with-param name="decRedValue" select="$redValue"/>
214                    <xsl:with-param name="decGreenValue" select="$greenValue"/>
215                    <xsl:with-param name="decBlueValue" select="$blueValue"/>
216                </xsl:call-template>
217            </xsl:when>
218            <xsl:otherwise/>
219        </xsl:choose>
220    </xsl:template>
221    <xsl:template name="dec_rgb2Hex">
222        <xsl:param name="decRedValue"/>
223        <xsl:param name="decGreenValue"/>
224        <xsl:param name="decBlueValue"/>
225        <xsl:variable name="hexRedValue">
226            <xsl:variable name="tmpHexRedValue">
227                <xsl:call-template name="decimal2hex">
228                    <xsl:with-param name="dec-number" select="$decRedValue"/>
229                    <xsl:with-param name="last-value" select="'H'"/>
230                </xsl:call-template>
231            </xsl:variable>
232            <xsl:choose>
233                <xsl:when test="string-length($tmpHexRedValue) = 1">
234                    <xsl:value-of select="concat('0',$tmpHexRedValue)"/>
235                </xsl:when>
236                <xsl:otherwise>
237                    <xsl:value-of select="$tmpHexRedValue"/>
238                </xsl:otherwise>
239            </xsl:choose>
240        </xsl:variable>
241        <xsl:variable name="hexGreenValue">
242            <xsl:variable name="tmpHexGreenValue">
243                <xsl:call-template name="decimal2hex">
244                    <xsl:with-param name="dec-number" select="$decGreenValue"/>
245                    <xsl:with-param name="last-value" select="'H'"/>
246                </xsl:call-template>
247            </xsl:variable>
248            <xsl:choose>
249                <xsl:when test="string-length($tmpHexGreenValue) = 1">
250                    <xsl:value-of select="concat('0',$tmpHexGreenValue)"/>
251                </xsl:when>
252                <xsl:otherwise>
253                    <xsl:value-of select="$tmpHexGreenValue"/>
254                </xsl:otherwise>
255            </xsl:choose>
256        </xsl:variable>
257        <xsl:variable name="hexBlueValue">
258            <xsl:variable name="tmpHexBlueValue">
259                <xsl:call-template name="decimal2hex">
260                    <xsl:with-param name="dec-number" select="$decBlueValue"/>
261                    <xsl:with-param name="last-value" select="'H'"/>
262                </xsl:call-template>
263            </xsl:variable>
264            <xsl:choose>
265                <xsl:when test="string-length($tmpHexBlueValue) = 1">
266                    <xsl:value-of select="concat('0',$tmpHexBlueValue)"/>
267                </xsl:when>
268                <xsl:otherwise>
269                    <xsl:value-of select="$tmpHexBlueValue"/>
270                </xsl:otherwise>
271            </xsl:choose>
272        </xsl:variable>
273        <xsl:value-of select="concat('#',$hexRedValue,$hexGreenValue,$hexBlueValue)"/>
274    </xsl:template>
275    <xsl:template name="decimal2hex">
276        <!-- transforms a decimal number to a hex number,only for two-bit hex(less than 256 in decimal) currently -->
277        <xsl:param name="dec-number"/>
278        <xsl:param name="last-value"/>
279        <xsl:variable name="current-value">
280            <xsl:call-template name="decNumber2hex">
281                <xsl:with-param name="dec-value">
282                    <xsl:if test="$dec-number &gt; 15">
283                        <xsl:value-of select="floor($dec-number div 16)"/>
284                    </xsl:if>
285                    <xsl:if test="$dec-number &lt; 16">
286                        <xsl:value-of select="$dec-number"/>
287                    </xsl:if>
288                </xsl:with-param>
289            </xsl:call-template>
290        </xsl:variable>
291        <xsl:if test="$dec-number &gt; 15">
292            <xsl:call-template name="decimal2hex">
293                <xsl:with-param name="dec-number" select="$dec-number mod 16"/>
294                <xsl:with-param name="last-value" select="concat($last-value,$current-value)"/>
295            </xsl:call-template>
296        </xsl:if>
297        <xsl:if test="$dec-number &lt; 16">
298            <xsl:value-of select="substring-after(concat($last-value,$current-value),'H')"/>
299        </xsl:if>
300    </xsl:template>
301
302    <xsl:template name="decNumber2hex">
303        <!-- return a hex number for a decimal character -->
304        <xsl:param name="dec-value"/>
305        <xsl:choose>
306            <xsl:when test="$dec-value = 10">
307                <xsl:value-of select="'A'"/>
308            </xsl:when>
309            <xsl:when test="$dec-value = 11">
310                <xsl:value-of select="'B'"/>
311            </xsl:when>
312            <xsl:when test="$dec-value = 12">
313                <xsl:value-of select="'C'"/>
314            </xsl:when>
315            <xsl:when test="$dec-value = 13">
316                <xsl:value-of select="'D'"/>
317            </xsl:when>
318            <xsl:when test="$dec-value = 14">
319                <xsl:value-of select="'E'"/>
320            </xsl:when>
321            <xsl:when test="$dec-value = 15">
322                <xsl:value-of select="'F'"/>
323            </xsl:when>
324            <xsl:otherwise>
325                <xsl:value-of select="$dec-value"/>
326            </xsl:otherwise>
327        </xsl:choose>
328    </xsl:template>
329    <xsl:template name="GetTabColorIndex">
330        <xsl:param name="SheetColor"/>
331        <xsl:for-each select="key('config', 'TabColor')[not(.=preceding::config:config-item)]">
332        <xsl:sort select="." />
333            <xsl:variable name="tmpColor" select="."/>
334            <xsl:if test=". = $SheetColor" >
335                <xsl:value-of select="56 - position()"/>
336            </xsl:if>
337        </xsl:for-each>
338    </xsl:template>
339	<xsl:template match="office:body">
340		<!-- office:body table:table children are spreadsheets -->
341		<xsl:apply-templates />
342	</xsl:template>
343
344	<xsl:template match="office:spreadsheet">
345		<xsl:apply-templates />
346	</xsl:template>
347
348	<!-- office:body table:table children are spreadsheets -->
349	<xsl:template match="office:spreadsheet/table:table">
350		<xsl:element name="ss:Worksheet">
351			<xsl:variable name="TableName">
352				<xsl:value-of select="@table:name" />
353			</xsl:variable>
354			<xsl:attribute name="ss:Name">
355				<xsl:value-of select="$TableName" />
356			</xsl:attribute>
357			<xsl:call-template name="table:table" />
358			<xsl:element name="x:WorksheetOptions">
359				<xsl:if test="key('config', 'ShowGrid') = 'false'">
360						<xsl:element name="x:DoNotDisplayGridlines" />
361				</xsl:if>
362				<xsl:if test="key('config', 'HasColumnRowHeaders') = 'false'">
363						<xsl:element name="x:DoNotDisplayHeadings" />
364				</xsl:if>
365				<xsl:if test="key('config', 'IsOutlineSymbolsSet') = 'false'">
366						<xsl:element name="x:DoNotDisplayOutline" />
367				</xsl:if>
368				<xsl:if test="key('config', 'ShowZeroValues') = 'false'">
369						<xsl:element name="x:DoNotDisplayZeros" />
370				</xsl:if>
371                <xsl:if test="/*/office:settings/config:config-item-set/config:config-item-map-indexed/config:config-item-map-entry/config:config-item-map-named/config:config-item-map-entry[@config:name=$TableName]/config:config-item[@config:name='TabColor']">
372                    <xsl:element name="x:TabColorIndex">
373                        <xsl:variable name="TabColorIndex">
374                            <xsl:call-template name="GetTabColorIndex">
375                                <xsl:with-param name="SheetColor" select="/*/office:settings/config:config-item-set/config:config-item-map-indexed/config:config-item-map-entry/config:config-item-map-named/config:config-item-map-entry[@config:name=$TableName]/config:config-item[@config:name='TabColor']"/>
376                            </xsl:call-template>
377                        </xsl:variable>
378                        <xsl:value-of select="$TabColorIndex"/>
379                    </xsl:element>
380                </xsl:if>
381			</xsl:element>
382		</xsl:element>
383	</xsl:template>
384
385	<xsl:template match="table:decls" mode="ExcelWorkbook">
386		<xsl:apply-templates mode="ExcelWorkbook" />
387	</xsl:template>
388
389	<xsl:template match="table:calculation-settings"  mode="ExcelWorkbook">
390		<xsl:if test="table:precision-as-shown">
391			<x:PrecisionAsDisplayed/>
392		</xsl:if>
393		<xsl:if test="table:null-date/@office:date-value='1904-01-01'">
394			<x:Date1904/>
395		</xsl:if>
396		<xsl:apply-templates select="table:iteration" />
397	</xsl:template>
398
399	<xsl:template match="table:iteration" mode="ExcelWorkbook">
400		<xsl:element name="x:ExcelWorkbook">
401			<xsl:if test="@table:status = 'enable'">
402				<x:Iteration/>
403			</xsl:if>
404			<xsl:if test="@table:steps">
405				<xsl:element name="x:MaxIterations">
406					<xsl:value-of select="@table:steps" />
407				</xsl:element>
408			</xsl:if>
409			<xsl:if test="@table:maximum-difference">
410				<xsl:element name="x:MaxChange">
411					<xsl:value-of select="@table:maximum-difference" />
412				</xsl:element>
413			</xsl:if>
414		</xsl:element>
415	</xsl:template>
416
417</xsl:stylesheet>
418