view rect.xsl @ 34:93fd0d532754

fix bug in refs wrt e.g. [1]!.SGX, adapt html and ascii to new-format refs, move a2n and n2a into separate files for re-use
author Henry S. Thompson <ht@markup.co.uk>
date Wed, 12 Apr 2017 21:35:04 +0100
parents 8309dcfce613
children 468a6cf8bf0b
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:include href="a2n.xsl"/>
 <xsl:include href="n2a.xsl"/>
 
 <xsl:variable name="refs" select="collection()[2]/*"/>
 
 <xsl:key name="ref" match="e:i" use="@r"/>

 <xsl:variable name="dim" select="//s:dimension/@ref"/>
 
 <xsl:template match="/">
  <xsl:apply-templates select="//s:sheetData"/>
 </xsl:template>

 <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="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:if test="$c/@e:class"><xsl:attribute name="c"><xsl:value-of select="substring($c/@e:class,1,1)"/></xsl:attribute></xsl:if>
          <xsl:if test="$c/@e:code"><xsl:attribute name="l"><xsl:value-of select="$c/@e:code"/></xsl:attribute></xsl:if>
          <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>