Mercurial > hg > ooxml
annotate refs.xsl @ 28:c56a2e6990bd
convert tokenisation to a function, so can make recursive
author | Henry S. Thompson <ht@markup.co.uk> |
---|---|
date | Tue, 11 Apr 2017 12:28:44 +0100 |
parents | 8309dcfce613 |
children | 87ed04a0fde2 |
rev | line source |
---|---|
17
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
1 <?xml version='1.0'?> |
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
2 <!DOCTYPE doc SYSTEM "../../../lib/xml/xsl.dtd" > |
23 | 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"> |
27
8309dcfce613
preparing for variable deref
Henry S. Thompson <ht@markup.co.uk>
parents:
25
diff
changeset
|
4 <xsl:param name="sheet-number"/> |
8309dcfce613
preparing for variable deref
Henry S. Thompson <ht@markup.co.uk>
parents:
25
diff
changeset
|
5 <xsl:param name="xlDir"/> |
8309dcfce613
preparing for variable deref
Henry S. Thompson <ht@markup.co.uk>
parents:
25
diff
changeset
|
6 |
17
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
7 <xsl:variable name="pat1">("[^"]*")|(\{[^}]+})|(,)|([^=\-+*/();:,.$<>^!]+(?:\.[^=\-+*/();:,.$<>^!]+)*\()|([)])|(^=|\()|((?:'[^']+')|(?:\[[0-9]+\][^!]*))|(\$?[A-Z]+\$?[0-9]+)|([a-zA-Z_\\][a-zA-Z0-9._]*)|(.)</xsl:variable> |
27
8309dcfce613
preparing for variable deref
Henry S. Thompson <ht@markup.co.uk>
parents:
25
diff
changeset
|
8 <xsl:param name="pat" select="$pat1"/><!-- xsl:param for refinement debugging by passing in the pattern --> |
8309dcfce613
preparing for variable deref
Henry S. Thompson <ht@markup.co.uk>
parents:
25
diff
changeset
|
9 |
8309dcfce613
preparing for variable deref
Henry S. Thompson <ht@markup.co.uk>
parents:
25
diff
changeset
|
10 <xsl:variable name="workbook" select="document(concat($xlDir,'/workbook.xml'))/*"/> |
8309dcfce613
preparing for variable deref
Henry S. Thompson <ht@markup.co.uk>
parents:
25
diff
changeset
|
11 <xsl:variable name="sheet-name" select="$workbook/s:sheets/s:sheet[@sheetId=$sheet-number]/@name"/> |
17
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
12 |
28
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
13 <xsl:function name="e:tokenise" as="array(xs:string*)*"> |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
14 <xsl:param name="formula" as="xs:string" required="yes"/> |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
15 <xsl:sequence select=" |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
16 let $tokens := analyze-string($formula,$pat)/xf:match/xf:group |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
17 return if ($tokens[@nr=(7,8,9)]) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
18 then |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
19 let $n := count($tokens), |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
20 $singles := for $i in (1 to $n) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
21 return if ($tokens[$i][@nr=(8,9)] and |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
22 not($tokens[$i - 1][@nr=10 and |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
23 .=(':','!')]) and |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
24 not($tokens[$i + 1][@nr=10 and .=':'])) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
25 then translate($tokens[$i],'$','') |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
26 else (), |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
27 $ranges := for $i in (1 to count($tokens)) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
28 return if ($tokens[$i][@nr=10 and .=':' and |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
29 not($i gt 2 and |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
30 $tokens[$i - 2][@nr=10 and .='!'])]) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
31 then translate(concat($tokens[$i - 1],':', |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
32 $tokens[$i + 1]),'$','') |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
33 else (), |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
34 $externals := for $i in (1 to count($tokens)) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
35 return if ($tokens[$i][@nr=7]) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
36 then |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
37 let $bit := concat($tokens[$i],'!', |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
38 translate($tokens[$i + 2], |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
39 '$','')) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
40 return if ((($i+3) le $n) and |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
41 $tokens[$i + 3][@nr=10 and .=':']) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
42 then concat($bit,':', |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
43 translate($tokens[$i + 4], |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
44 '$','')) |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
45 else $bit |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
46 else () |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
47 return [$singles,$ranges,$externals] |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
48 else ()"/> |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
49 </xsl:function> |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
50 |
17
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
51 <xsl:template match="/"> |
27
8309dcfce613
preparing for variable deref
Henry S. Thompson <ht@markup.co.uk>
parents:
25
diff
changeset
|
52 <refs sheetName="{$sheet-name}"><xsl:apply-templates select="//s:c"/></refs> |
17
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
53 </xsl:template> |
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
54 |
18
16bba7a0d0bd
one form of refs output, not sure it's what is wanted...
Henry S. Thompson <ht@markup.co.uk>
parents:
17
diff
changeset
|
55 <xsl:template match="s:c[s:f]"> |
28
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
56 <xsl:variable name="tokens" select="e:tokenise(s:f/.)"/> |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
57 <xsl:if test="@r='xxx'"><xsl:message>|</xsl:message> |
21 | 58 </xsl:if> |
28
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
59 <xsl:if test="count($tokens)>0"> |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
60 <xsl:variable name="singles" select="$tokens?1"/> |
23 | 61 <!-- Note that we don't bother to treat external ranges as ranges, |
62 since we're not going to try to detect cross-document refs --> | |
28
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
63 <xsl:variable name="ranges" select="$tokens?2"/> |
c56a2e6990bd
convert tokenisation to a function, so can make recursive
Henry S. Thompson <ht@markup.co.uk>
parents:
27
diff
changeset
|
64 <xsl:variable name="externals" select="$tokens?3"/> |
23 | 65 <ref c="{@r}"> |
25
a5125a2e77ea
towards inverted map of refs
Henry S. Thompson <ht@markup.co.uk>
parents:
24
diff
changeset
|
66 <xsl:for-each select="distinct-values($singles)"> |
24
87e0d620deea
switch to elements from attributes and default namespace
Henry S. Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
67 <s><xsl:value-of select="."/></s> |
87e0d620deea
switch to elements from attributes and default namespace
Henry S. Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
68 </xsl:for-each> |
25
a5125a2e77ea
towards inverted map of refs
Henry S. Thompson <ht@markup.co.uk>
parents:
24
diff
changeset
|
69 <xsl:for-each select="distinct-values($ranges)"> |
24
87e0d620deea
switch to elements from attributes and default namespace
Henry S. Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
70 <r><xsl:value-of select="."/></r> |
87e0d620deea
switch to elements from attributes and default namespace
Henry S. Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
71 </xsl:for-each> |
25
a5125a2e77ea
towards inverted map of refs
Henry S. Thompson <ht@markup.co.uk>
parents:
24
diff
changeset
|
72 <xsl:for-each select="distinct-values($externals)"> |
24
87e0d620deea
switch to elements from attributes and default namespace
Henry S. Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
73 <e><xsl:value-of select="."/></e> |
87e0d620deea
switch to elements from attributes and default namespace
Henry S. Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
74 </xsl:for-each> |
23 | 75 </ref></xsl:if> |
17
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
76 </xsl:template> |
18
16bba7a0d0bd
one form of refs output, not sure it's what is wanted...
Henry S. Thompson <ht@markup.co.uk>
parents:
17
diff
changeset
|
77 |
16bba7a0d0bd
one form of refs output, not sure it's what is wanted...
Henry S. Thompson <ht@markup.co.uk>
parents:
17
diff
changeset
|
78 <xsl:template match="s:c"/> |
17
a874f2a81ed2
default vis pipeline to raw xml, start extracting refs
Henry S. Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
79 </xsl:stylesheet> |