Mercurial > hg > ooxml
comparison tokenise.xsl @ 37:ac3cd8de7a10
towards big rework of tokenisation
author | Henry S. Thompson <ht@markup.co.uk> |
---|---|
date | Tue, 25 Apr 2017 18:30:04 +0100 |
parents | |
children | 468a6cf8bf0b |
comparison
equal
deleted
inserted
replaced
36:ae605b77d1e4 | 37:ac3cd8de7a10 |
---|---|
1 <?xml version='1.0'?> | |
2 <!DOCTYPE xsl:stylesheet SYSTEM "../../../lib/xml/xsl.dtd" > | |
3 <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 e xf" xmlns="http://markup.co.uk/excel" xmlns:xf="http://www.w3.org/2005/xpath-functions"> | |
4 <xsl:param name="sheet-number"/> | |
5 <xsl:param name="xlDir"/> | |
6 | |
7 <xsl:include href="a2n.xsl"/> | |
8 | |
9 <xsl:variable name="pat1">("[^"]*")|(\{[^}]+})|(,)|([^=\-+*/();:,.$<>^!]+(?:\.[^=\-+*/();:,.$<>^!]+)*\()|([)])|(^=|\()|((?:(?:'[^']+')|(?:\[[0-9]+\][^!]*)|(?:[a-zA-Z_][a-zA-Z0-9._]*)!))|(\$?[A-Z]+\$?[0-9]+)|([a-zA-Z_\\][a-zA-Z0-9._]*)|(.)</xsl:variable> | |
10 <xsl:param name="pat" select="$pat1"/><!-- xsl:param for refinement debugging by passing in the pattern --> | |
11 | |
12 <xsl:variable name="workbook" select="document(concat($xlDir,'/workbook.xml'))/*"/> | |
13 <xsl:variable name="sheet-name" select="$workbook/s:sheets/s:sheet[@sheetId=$sheet-number]/@name"/> | |
14 | |
15 <xsl:function name="e:lookup" as="xs:string*"> | |
16 <xsl:param name="name" as="xs:string" required="yes"/> | |
17 <xsl:variable name="defn" select="$workbook/s:definedNames/s:definedName[@name=$name]"/> | |
18 <xsl:sequence select="let $prefix := concat($sheet-name,'!') | |
19 return if ($defn and | |
20 starts-with($defn,$prefix)) | |
21 then substring-after($defn,$prefix) | |
22 else ()"/> | |
23 </xsl:function> | |
24 | |
25 <xsl:function name="e:tokenise" as="element(*)*"> | |
26 <!-- Tokenise a formula, recursively wrt variables | |
27 Output is composed of e:* as follows: | |
28 c: A list (function parameter) separator | |
29 e: An external (variable, cell or range) reference | |
30 f: A function name followed by an opening parenthesis | |
31 l: The beginning of the formula or an opening paren | |
32 m: A constant matrix | |
33 p: A close-paren | |
34 q: A text (delimited by double quotes) | |
35 r: A range reference | |
36 s: A single-cell reference | |
37 v: A variable name [should only occur inside e] | |
38 x: Amalgamated single characters not matched by anything else | |
39 --> | |
40 <xsl:param name="formula" as="xs:string" required="yes"/> | |
41 <!-- The row and column number of the cell whence the formula came --> | |
42 <xsl:param name="row" required="yes" as="xs:int"/> | |
43 <xsl:param name="col" required="yes" as="xs:int"/> | |
44 <xsl:sequence select=" | |
45 let $tokens := analyze-string($formula,$pat)/xf:match/xf:group | |
46 return e:tok1($tokens,count($tokens),1,$row,$col,())"/> | |
47 </xsl:function> | |
48 | |
49 <xsl:function name="e:tok1" as="element(*)*"> | |
50 <xsl:param name="tokens" as="element(xf:group)*" required="yes"/> | |
51 <xsl:param name="n" required="yes" as="xs:int"/> | |
52 <xsl:param name="i" required="yes" as="xs:int"/> | |
53 <xsl:param name="row" required="yes" as="xs:int"/> | |
54 <xsl:param name="col" required="yes" as="xs:int"/> | |
55 <xsl:param name="soFar" required="yes" as="element(*)*"/> | |
56 <xsl:sequence select=" | |
57 if ($i gt $n) | |
58 then $soFar | |
59 else | |
60 let $next := e:expand($tokens,$i,true(),$row,$col), | |
61 $j := $next?1, | |
62 $res := $next?2 return | |
63 e:tok1($tokens,$n,$j,$row,$col,($soFar,$res))"/> | |
64 </xsl:function> | |
65 | |
66 <xsl:function name="e:expand" as="element(*)*"> | |
67 <xsl:param name="tokens" required="yes" as="element(xf:group)*"/> | |
68 <xsl:param name="i" required="yes" as="xs:int"/> | |
69 <xsl:param name="local" required="yes" as="xs:boolean"/> | |
70 <xsl:param name="row" required="yes" as="xs:int"/> | |
71 <xsl:param name="col" required="yes" as="xs:int"/> | |
72 <xsl:sequence select=" | |
73 let $t := $tokens[$i], | |
74 $r := $tokens[$i + 1] return | |
75 if ($t/@nr=1) then e:exp1($i,'q',string($t)) | |
76 else if ($t/@nr=2) then e:exp1($i,'m',string($t)) | |
77 else if ($t/@nr=3) then e:exp1($i,'c',',') | |
78 else if ($t/@nr=4) then e:exp1($i,'f',string($t)) | |
79 else if ($t/@nr=5) then e:exp1($i,'p',')') | |
80 else if ($t/@nr=6) then e:exp1($i,'l',string($t)) | |
81 else if ($t/@nr=7) | |
82 then if (substring-before($t,'!')=('[0]',$sheet-name)) | |
83 then (: it's a local reference after all :) | |
84 e:expand($tokens,$i+1,true(),$row,$col) | |
85 else let $ext := e:expand($tokens,$i+1,false(),$row,$col) return | |
86 [$ext?1,e:external($ext?2)] | |
87 else if ($t/@nr=10) then e:amalgamate($tokens,$i+1,string($t)) | |
88 else if ($r[@nr=10 and .=':']) | |
89 then (: a range, takes priority :) | |
90 e:range($tokens,$i,$ext,$row,$col) | |
91 else if ($t/@nr=8) then e:single($i,$ext,string($t)) | |
92 else if ($t/@nr=9) | |
93 then if ($ext) then (: can't expand :) e:exp1($i,'v',string($t)) | |
94 else e:tokenise(e:lookup(string($t)),$row,$col) | |
95 else (-- shouldn't ever get here --) ()"/> | |
96 </xsl:function> | |
97 | |
98 <xsl:function name="e:exp1" as="array(*)"> | |
99 <xsl:param name="i" as="xs:int"/> | |
100 <xsl:param name="name" as="xs:string"/> | |
101 <xsl:param name="val" as="xs:string"/> | |
102 <xsl:variable name="elt"> | |
103 <xsl:element name="{$name}" namespace="http://markup.co.uk/excel"> | |
104 <xsl:value-of select="$val"/> | |
105 </xsl:element> | |
106 </xsl:variable> | |
107 <xsl:sequence select="[$i+1,$elt]"/> | |
108 </xsl:function> | |
109 | |
110 <xsl:function name="e:single" as="element(*)"> | |
111 <xsl:param name="group" as="element(xf:group)"/> | |
112 <xsl:param name="row" as="xs:integer"/> | |
113 <xsl:param name="col" as="xs:integer"/> | |
114 <xsl:param name="external" as="xs:boolean"/> | |
115 <xsl:variable name="val" select="if ($group/@nr=9) then e:lookup($group) | |
116 else string($group)"/> | |
117 <xsl:choose> | |
118 <xsl:when test="count($val)>0 or not($external)"> | |
119 <xsl:sequence select="e:cr($val,$row,$col)"/> | |
120 </xsl:when> | |
121 <xsl:otherwise> | |
122 <v><xsl:value-of select="$group"/></v> | |
123 </xsl:otherwise> | |
124 </xsl:choose> | |
125 </xsl:function> | |
126 | |
127 <xsl:function name="e:range" as="element(e:r)"> | |
128 <xsl:param name="l" as="element(e:s)" required="yes"/> | |
129 <xsl:param name="r" as="element(e:s)" required="yes"/> | |
130 <r><xsl:copy-of select="$l"/><xsl:copy-of select="$r"/></r> | |
131 </xsl:function> | |
132 | |
133 <xsl:function name="e:external" as="element(e:e)"> | |
134 <xsl:param name="source" as="element(xf:group)" required="yes"/> | |
135 <xsl:param name="ref" as="element(*)" required="yes"/> | |
136 <e s="{$source}"><xsl:sequence select="$ref"/></e> | |
137 </xsl:function> | |
138 </xsl:stylesheet> |