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