changeset 32:f4432c1faf7f

move to more structure mostly working but external vars are lost
author Henry S. Thompson <ht@markup.co.uk>
date Wed, 12 Apr 2017 14:33:47 +0100
parents 6270bef9b5d4
children 27bffc66ce10
files refs.xsl
diffstat 1 files changed, 33 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/refs.xsl	Wed Apr 12 11:36:27 2017 +0100
+++ b/refs.xsl	Wed Apr 12 14:33:47 2017 +0100
@@ -20,7 +20,7 @@
                            else ()"/>
  </xsl:function>
 
- <xsl:function name="e:tokenise" as="array(xs:string*)*">
+ <xsl:function name="e:tokenise" as="array(element(*)*)*">
   <!-- Tokenise a formula, recursively wrt variables -->
   <xsl:param name="formula" as="xs:string" required="yes"/>
   <xsl:sequence select="
@@ -40,7 +40,7 @@
                             else (),
                   $defns := for $var in $vars return e:lookup($var),
                   $recur := for $sub in $defns 
-                              return if ($sub) then e:tokenise($defns) else (),
+                              return if ($sub) then e:tokenise($sub) else (),
                   $singles := for $i in (1 to $n) return
                             let $t := $tokens[$i],
                                 $l := $tokens[$i - 1],
@@ -49,7 +49,7 @@
                                 not($l[@nr=10 and
                                        .=(':','!')]) and
                                 not($r[@nr=10 and .=':']))
-                             then string($t)
+                             then e:single($t)
                              else (),
                   $ranges := for $i in (1 to count($tokens)) return
                             let $t := $tokens[$i] return
@@ -57,29 +57,45 @@
                                    not($i gt 2 and
                                        $tokens[$i - 2][@nr=10 and .='!'])])
                              then let $l := $tokens[$i - 1],
-                                      $r := $tokens[$i + 1],
-                                      $l1 := if ($l/@nr=9) then e:lookup($l)
-                                             else $l,
-                                      $r1 := if ($r/@nr=9) then e:lookup($r)
-                                             else $r
-                                      return concat($l1,':',$r1)
+                                      $r := $tokens[$i + 1]
+                                      return e:range(e:single($l),
+                                                     e:single($r))
                              else (),
                   $externals := for $i in (1 to count($tokens)) return
                             let $t := $tokens[$i] return
                             if ($t/@nr=7)
                              then 
-                              let $bit := concat($t,'!',$tokens[$i + 2]) return
+                              let $ref := e:single($tokens[$i + 2]) return
                                if ((($i+3) le $n) and
                                    $tokens[$i + 3][@nr=10 and .=':'])
-                                then concat($bit,':',
-                                            $tokens[$i + 4])
-                                else $bit
+                                then e:external($t,e:range($ref,
+                                                       e:single($tokens[$i+4])))
+                                else e:external($t,$ref)
                              else ()
                   return [($singles,for $a in $recur return $a?1),
                           ($ranges,for $a in $recur return $a?2),
                           ($externals,for $a in $recur return $a?3)]
              else ()"/>
  </xsl:function>
+ 
+ <xsl:function name="e:single" as="element(e:s)">
+  <xsl:param name="group" as="element(xf:group)" required="yes"/>
+  <xsl:variable name="val" select="if ($group/@nr=9) then e:lookup($group)
+                                             else string($group)"/>
+  <s><xsl:value-of select="$val"/></s>
+ </xsl:function>
+ 
+ <xsl:function name="e:range" as="element(e:r)">
+  <xsl:param name="l" as="element(e:s)" required="yes"/>
+  <xsl:param name="r" as="element(e:s)" required="yes"/>
+  <r><xsl:copy-of select="$l"/><xsl:copy-of select="$r"/></r>
+ </xsl:function>
+ 
+ <xsl:function name="e:external" as="element(e:e)">
+  <xsl:param name="source" as="element(xf:group)" required="yes"/>
+  <xsl:param name="ref" as="element(*)" required="yes"/>
+  <e s="{$source}"><xsl:sequence select="$ref"/></e>
+ </xsl:function>
 
  <xsl:template match="/">
   <refs sheetName="{$sheet-name}"><xsl:apply-templates select="//s:c"/></refs>
@@ -95,16 +111,11 @@
           since we're not going to try to detect cross-document refs -->
    <xsl:variable name="ranges" select="$tokens?2"/>
    <xsl:variable name="externals" select="$tokens?3"/>
+   <!-- Lost distinct-values filter, not sure it's really possible... -->
    <ref c="{@r}">
-    <xsl:for-each select="distinct-values($singles)">
-     <s><xsl:value-of select="."/></s>
-    </xsl:for-each>
-    <xsl:for-each select="distinct-values($ranges)">
-     <r><xsl:value-of select="."/></r>
-    </xsl:for-each>
-    <xsl:for-each select="distinct-values($externals)">
-     <e><xsl:value-of select="."/></e>
-    </xsl:for-each>
+    <xsl:copy-of select="$singles"/>
+    <xsl:copy-of select="$ranges"/>
+    <xsl:copy-of select="$externals"/>
    </ref></xsl:if>
  </xsl:template>