view a2n.xsl @ 51:793496d2d165

allow command line input
author Henry S. Thompson <ht@markup.co.uk>
date Tue, 16 May 2017 17:27:19 +0100
parents 5d9806f90896
children
line wrap: on
line source

<?xml version='1.0'?>
<!DOCTYPE xsl:stylesheet SYSTEM "file:///C:/C64/home/ht/lib/xml/xsl.dtd" >
<xsl:stylesheet xmlns:e="http://markup.co.uk/excel" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="3.0" xmlns:xpf="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="e xpf xs" xmlns="http://markup.co.uk/excel">

 <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" required="yes"/>
 <xsl:variable name="codeBase" select="string-to-codepoints('A')-1"/>
  <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:pr" as="element(xpf:analyze-string-result)">
  <xsl:param name="ref" as="xs:string"/>
  <xsl:sequence select="analyze-string($ref,'([$]?)([A-Z]+)([$]?)([0-9]+)')"/>
 </xsl:function>
 
 <xsl:function name="e:cr" as="element(e:s)">
  <!-- Parse a single cell reference into left and right halves,
       either e:a(bsolute) or e:r(elocatable) -->
  <xsl:param name="ref" as="xs:string" required="yes"/>
  <xsl:param name="row" as="xs:integer" required="yes"/>
  <xsl:param name="col" as="xs:integer" required="yes"/>
  <xsl:variable name="cr" select="e:pr($ref)"/>
  <xsl:variable name="c" select="if ($cr//xpf:group[@nr=1][text()]) then 'a' else 'r'"/>
  <xsl:variable name="cv" select="e:a2n($cr//xpf:group[@nr=2])"/>
  <xsl:variable name="r" select="if ($cr//xpf:group[@nr=3][text()]) then 'a' else 'r'"/>
  <xsl:variable name="rv" select="xs:integer($cr//xpf:group[@nr=4])"/>
  <s r="{$ref}">
   <xsl:element name="{$c}" namespace="http://markup.co.uk/excel">
    <xsl:if test="$c='r'">
     <xsl:attribute name="d"><xsl:value-of select="$cv - $col"/></xsl:attribute>
    </xsl:if>
    <xsl:value-of select="$cv"/>
   </xsl:element>
   <xsl:element name="{$r}" namespace="http://markup.co.uk/excel">
    <xsl:if test="$r='r'">
     <xsl:attribute name="d"><xsl:value-of select="$rv - $row"/></xsl:attribute>
    </xsl:if>
    <xsl:value-of select="$rv"/>
   </xsl:element>
  </s>  
 </xsl:function>
</xsl:stylesheet>