view ascii.xsl @ 8:785187b9caef

working toward doing an ascii-rectangle repr of a sheet
author Henry S. Thompson <ht@markup.co.uk>
date Sun, 02 Apr 2017 21:13:16 +0800
parents
children 01e80c7a9575
line wrap: on
line source

<?xml version='1.0'?>
<!DOCTYPE doc SYSTEM "../../../lib/xml/xsl.dtd" >
<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">
 <xsl:variable name="dim" select="//s:dimension/@ref"/>
 <xsl:variable name="codeBase" select="string-to-codepoints('A')-1"/>
 
 <xsl:function name="e:a2n" as="xs:integer">
  <!-- Convert an alphabetic excel column 'index' into a number,
       a sort of base 26 + 1, since 'A' is 1 and 'AA' is 27 -->
  <xsl:param name="aa" as="xs:string"/>
  <xsl:value-of select="let $l := string-length($aa),
                        $lv := string-to-codepoints(substring($aa,$l))-$codeBase
                        return if ($l=1) then $lv
                               else $lv+(26*e:a2n(substring($aa,1,$l - 1)))"/>
 </xsl:function>
 
 <xsl:function name="e:n2aa" as="xs:integer*">
  <!-- Does the real work for e:n2a -->
  <xsl:param name="n" as="xs:integer"/>
  <xsl:sequence select="let $least := $codeBase+(($n - 1) mod 26)+1
                         return if ($n &lt; 27) then $least
                          else (e:n2aa($n idiv 26),$least)"/>
 </xsl:function>

 <xsl:function name="e:n2a" as="xs:string">
  <!-- Invert the a2n computation -->
  <xsl:param name="n" as="xs:integer"/>
  <xsl:value-of select="codepoints-to-string(e:n2aa($n))"/>
 </xsl:function>
 
 <xsl:template match="s:sheetData">
  <xsl:if test="s:row">
   <xsl:variable name="dims" select="analyze-string($dim,'([A-Z]+)([0-9]+):([A-Z]+)([0-9]+)')"/> 
   <xsl:variable name="width" select="1+number($dims//xpf:group[@nr='4'])-number($dims//xpf:group[@nr='2'])"/>
   <xsl:message><xsl:value-of select="$width"/>|<xsl:value-of select="1+e:a2n($dims//xpf:group[@nr='3'])-e:a2n($dims//xpf:group[@nr='1'])"/>|<xsl:value-of select="e:a2n('AA')"/></xsl:message>
   <xsl:message><xsl:value-of select="e:n2a(1)"/>|<xsl:value-of select="e:n2a(26)"/>|<xsl:value-of select="e:n2a(27)"/>|<xsl:value-of select="e:n2a(52)"/>|<xsl:value-of select="e:n2a(54)"/></xsl:message>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>