view rect.xsl @ 26:d2ca3ea1f5ae

plumb in and use inverted ref table (via key)
author Henry S. Thompson <ht@markup.co.uk>
date Fri, 07 Apr 2017 18:42:47 +0100
parents 87e0d620deea
children 8309dcfce613
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://markup.co.uk/excel" xmlns:xpf="http://www.w3.org/2005/xpath-functions">
 <xsl:strip-space elements="s:sheetData"/>
 
 <xsl:variable name="refs" select="collection()[2]/*"/>
 
 <xsl:key name="ref" match="e:i" use="@r"/>
 
 <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:variable name="dim" select="//s:dimension/@ref"/>
 <xsl:variable name="codeBase" select="string-to-codepoints('A')-1"/>
 
 <xsl:template match="/">
  <xsl:apply-templates select="//s:sheetData"/>
 </xsl:template>

 <xsl:template match="s:sheetData">
  <xsl:message><xsl:value-of select="count($refs/*)"/></xsl:message>
  <xsl:if test="s:row">
   <xsl:variable name="dims" select="analyze-string($dim,'([A-Z]+)([0-9]+):([A-Z]+)([0-9]+)')"/> 
   <xsl:variable name="height" select="1+xs:integer($dims//xpf:group[@nr='4'])-xs:integer($dims//xpf:group[@nr='2'])"/>
   <xsl:variable name="width" select="1+e:a2n($dims//xpf:group[@nr='3'])-e:a2n($dims//xpf:group[@nr='1'])"/>
   <!-- Brute force! -->
   <xsl:variable name="sheet" select="."/>
   <a>
    <xsl:text>&#10;</xsl:text>
    <xsl:for-each select="(1 to $height)"><xsl:variable name="row" select="."/>
     <r r="{$row}">
      <xsl:variable select="$sheet/s:row[@r=$row]/s:c" name="cells"/>
      <xsl:if test="$cells/*">
       <xsl:for-each select="(1 to $width)">
       <xsl:variable name="col" select="."/>
       <xsl:variable name="r" select="concat(e:n2a($col),string($row))"/>
       <xsl:variable name="c" select="$cells[@r=$r]"/>
        <xsl:variable name="reffed" select="key('ref',$r,$refs)"/>
       <c c="{$col}">
        <xsl:if test="$c/s:f"><xsl:attribute name="f">1</xsl:attribute></xsl:if>
        <xsl:if test="$c/*"><t><xsl:value-of select="substring($c/@e:type,1,1)"/></t></xsl:if>
        <xsl:if test="$reffed"><xsl:copy-of select="$reffed/e:r"/></xsl:if>
       </c>
      </xsl:for-each></xsl:if>
     </r>
     <xsl:text>&#10;</xsl:text></xsl:for-each>
   </a>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>