view format.xsl @ 74:7827e686be75 default tip

refactoring again...
author Henry S. Thompson <ht@markup.co.uk>
date Wed, 05 Jul 2017 18:26:27 +0100
parents e500d7c18aad
children
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:param name="xlDir"/>
 <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;,'\\]\p{Sc}'))"/>
 </xsl:function>

 <xsl:template match="s:worksheet">
  <!-- Just here to bind the namespaces at the top -->
  <worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"><xsl:apply-templates select="@*|node()"/></worksheet>
 </xsl:template>
 
 <xsl:template match="s:c[@t and @t!='n']">
  <!-- These take precedence over formatting-->
  <xsl:copy>
   <xsl:attribute namespace="http://markup.co.uk/excel" name="type">
    <xsl:choose>
     <xsl:when test="@t='s'">str</xsl:when>
     <xsl:when test="@t='b'">bool</xsl:when><!-- not yet seen -->
     <xsl:when test="@t='d'">date</xsl:when><!-- not yet seen -->
     <xsl:when test="@t='e'">err</xsl:when><!-- not yet seen -->
     <xsl:when test="@t='inlineStr'">str</xsl:when><!-- not yet seen -->
     <xsl:when test="@t='str'">str</xsl:when><!-- result of formula with string value -->
     <xsl:otherwise><xsl:message terminate="yes">Unknown value for c/@t <xsl:value-of select="@t"/></xsl:message></xsl:otherwise>
    </xsl:choose>
   </xsl:attribute>
   <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template match="s:c[not(@t) or @t='n']">
  <!-- I _think_ this can only happen if the contents are in some sense numeric -->
   <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"/>
  <xsl:if test="@r='xxx'"><xsl:message><xsl:value-of select="@r"/>: <xsl:value-of select="concat(@s,'|',$nfi,'|',$biClass)"/></xsl:message></xsl:if>
   <c>
    <xsl:choose>
      <xsl:when test="$biClass">
       <xsl:choose>
        <xsl:when test="$biClass='date'">
         <xsl:attribute namespace="http://markup.co.uk/excel" name="type">date</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
         <xsl:attribute namespace="http://markup.co.uk/excel" name="type">num</xsl:attribute>
         <xsl:if test="not($biClass=('num','gen'))"><xsl:attribute namespace="http://markup.co.uk/excel" name="class">
        <xsl:value-of select="$biClass"/>
       </xsl:attribute></xsl:if>
        </xsl:otherwise>
       </xsl:choose>
      </xsl:when>
     <xsl:otherwise>
      <!-- Note the completely vanilla no @t no @s case comes here.  I _think_ it's correct to treat that as numeric -->
      <xsl:attribute namespace="http://markup.co.uk/excel" name="type">num</xsl:attribute><!-- Would be wrong if any non-numeric formatCodes exist -->
      <xsl:if 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="class">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:if>
     </xsl:otherwise>
    </xsl:choose>
    <xsl:for-each select="@*"><xsl:copy/></xsl:for-each>
    <xsl:apply-templates/>
   </c>  
 </xsl:template>
 <xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
 </xsl:template>
</xsl:stylesheet>