view format.xsl @ 6:d6dae580f6a0

preliminary attempt to detect currency formatting
author Henry S. Thompson <ht@markup.co.uk>
date Sun, 02 Apr 2017 16:49:48 +0800
parents 106bf970a867
children fd8678fb7b4d
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="2.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" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
 <xsl:param name="elabDir"/>
 <xsl:variable name="xldir" select="replace(base-uri(),'/[^/]*/[^/]*$','')"/>
 <xsl:variable name="fmts" select="document(concat($elabDir,'/fmt.xml'))/fmts/fmt"/>
 <xsl:variable name="styles" select="document(concat($xldir,'/styles.xml'))/*"/>
 <xsl:variable name="xfs" select="$styles//s:cellXfs/s:xf"/>
 <xsl:variable name="nfs" select="$styles//s:numFmts/s:numFmt"/>

 <xsl:function name="e:currencyFormatP" as="xs:boolean">
  <xsl:param name="format"/>
  <!-- Really need a unicode block check -->
  <xsl:value-of select="matches($format,concat('^[&quot;',&quot;'&quot;,'][£$&#162;-&#165;]'))"/>
 </xsl:function>

 <xsl:template match="s:worksheet">
  <worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"><xsl:apply-templates select="@*|node()"/></worksheet>
 </xsl:template>
 <xsl:template match="s:c">
  <xsl:if test="@s">
   <xsl:variable name="i" select="number(@s)"/>
   <xsl:variable name="nfi" select="$xfs[position()-1=$i]/@numFmtId"/>
   <xsl:variable name="builtin" select="$fmts[@i=$nfi]"/>
   <xsl:variable name="biClass" select="$builtin/@class"/>
   <c>
    <xsl:choose>
      <xsl:when test="$biClass">
       <xsl:attribute namespace="http://markup.co.uk/excel" name="fClass">
        <xsl:value-of select="$biClass"/>
       </xsl:attribute>
      </xsl:when>
     <xsl:when test="$nfs[@numFmtId=$nfi]">
      <xsl:variable name="code" select="$nfs[@numFmtId=$nfi]/@formatCode"/>
      <xsl:choose>
       <xsl:when test="e:currencyFormatP($code)">
        <xsl:attribute namespace="http://markup.co.uk/excel" name="fClass">cur</xsl:attribute>
       </xsl:when>
       <xsl:otherwise>
        <xsl:attribute namespace="http://markup.co.uk/excel" name="code">
       <xsl:value-of select="$code"/>
      </xsl:attribute>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:when>
    </xsl:choose>
    <xsl:for-each select="@*"><xsl:copy/></xsl:for-each>
    <xsl:apply-templates/>
   </c>
  </xsl:if>
 </xsl:template>
 <xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
 </xsl:template>
</xsl:stylesheet>