annotate rect.xsl @ 16:2bbd067529b6

improve efficiency, detect blank rows, don't type empty cells
author Henry S. Thompson <ht@markup.co.uk>
date Mon, 03 Apr 2017 19:22:03 +0100
parents f78e8c55c06b
children 9b1b169dc8db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
1 <?xml version='1.0'?>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
2 <!DOCTYPE doc SYSTEM "../../../lib/xml/xsl.dtd" >
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
3 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" xmlns:s="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:e="http://markup.co.uk/excel" exclude-result-prefixes="xs s xpf" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:xpf="http://www.w3.org/2005/xpath-functions">
13
f78e8c55c06b three steps now
Henry S. Thompson <ht@markup.co.uk>
parents: 12
diff changeset
4 <xsl:strip-space elements="s:sheetData"/>
8
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
5
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
6 <xsl:function name="e:a2n" as="xs:integer">
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
7 <!-- Convert an alphabetic excel column 'index' into a number,
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
8 a sort of base 26 + 1, since 'A' is 1 and 'AA' is 27 -->
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
9 <xsl:param name="aa" as="xs:string"/>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
10 <xsl:value-of select="let $l := string-length($aa),
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
11 $lv := string-to-codepoints(substring($aa,$l))-$codeBase
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
12 return if ($l=1) then $lv
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
13 else $lv+(26*e:a2n(substring($aa,1,$l - 1)))"/>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
14 </xsl:function>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
15
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
16 <xsl:function name="e:n2aa" as="xs:integer*">
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
17 <!-- Does the real work for e:n2a -->
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
18 <xsl:param name="n" as="xs:integer"/>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
19 <xsl:sequence select="let $least := $codeBase+(($n - 1) mod 26)+1
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
20 return if ($n &lt; 27) then $least
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
21 else (e:n2aa($n idiv 26),$least)"/>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
22 </xsl:function>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
23
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
24 <xsl:function name="e:n2a" as="xs:string">
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
25 <!-- Invert the a2n computation -->
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
26 <xsl:param name="n" as="xs:integer"/>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
27 <xsl:value-of select="codepoints-to-string(e:n2aa($n))"/>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
28 </xsl:function>
10
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
29
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
30 <xsl:variable name="dim" select="//s:dimension/@ref"/>
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
31 <xsl:variable name="codeBase" select="string-to-codepoints('A')-1"/>
16
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
32
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
33 <xsl:template match="/">
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
34 <xsl:apply-templates select="//s:sheetData"/>
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
35 </xsl:template>
10
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
36
8
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
37 <xsl:template match="s:sheetData">
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
38 <xsl:if test="s:row">
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
39 <xsl:variable name="dims" select="analyze-string($dim,'([A-Z]+)([0-9]+):([A-Z]+)([0-9]+)')"/>
10
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
40 <xsl:variable name="height" select="1+xs:integer($dims//xpf:group[@nr='4'])-xs:integer($dims//xpf:group[@nr='2'])"/>
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
41 <xsl:variable name="width" select="1+e:a2n($dims//xpf:group[@nr='3'])-e:a2n($dims//xpf:group[@nr='1'])"/>
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
42 <!-- Brute force! -->
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
43 <xsl:variable name="sheet" select="."/>
11
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
44 <e:a>
10
01e80c7a9575 simple ascii type matrix output working
Henry S. Thompson <ht@markup.co.uk>
parents: 8
diff changeset
45 <xsl:text>&#10;</xsl:text>
11
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
46 <xsl:for-each select="(1 to $height)"><xsl:variable name="row" select="."/>
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
47 <e:r r="{$row}">
16
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
48 <xsl:variable select="$sheet/s:row[@r=$row]/s:c" name="cells"/>
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
49 <xsl:if test="$cells/*">
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
50 <xsl:for-each select="(1 to $width)">
11
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
51 <xsl:variable name="col" select="."/>
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
52 <xsl:variable name="r" select="concat(e:n2a($col),string($row))"/>
16
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
53 <xsl:variable name="c" select="$cells[@r=$r]"/>
11
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
54 <e:c c="{$col}">
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
55 <xsl:if test="$c/s:f"><xsl:attribute name="f">1</xsl:attribute></xsl:if>
16
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
56 <xsl:if test="$c/*"><xsl:value-of select="substring($c/@e:type,1,1)"/></xsl:if>
11
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
57 </e:c>
16
2bbd067529b6 improve efficiency, detect blank rows, don't type empty cells
Henry S. Thompson <ht@markup.co.uk>
parents: 13
diff changeset
58 </xsl:for-each></xsl:if>
11
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
59 </e:r>
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
60 <xsl:text>&#10;</xsl:text></xsl:for-each>
480454d218f7 add markup, indicate if computed or not
Henry S. Thompson <ht@markup.co.uk>
parents: 10
diff changeset
61 </e:a>
8
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
62 </xsl:if>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
63 </xsl:template>
785187b9caef working toward doing an ascii-rectangle repr of a sheet
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
64 </xsl:stylesheet>