XSLT Explorer: explorer.xsl

5 imports, 44 templates, 21 functions, 4 variables, 12 params, 1 FIXME: comment

List of Imports
List of Templates
t:functions-called
t:templates-called
t:variables-referenced
List of Functions
f:function-calls()
f:function-shadows()
f:generate-id()
f:template-calls()
f:template-shadows()
f:variable()
f:variables-referenced()
f:checksum()
f:clark-name()
f:clark-name()
f:encode-for-id()
f:fletcher16()
f:parse-expr()
f:process-avts()
f:source-location()
f:unique-id()
f:line-link()
f:reference-list()
f:summary-count()
f:summary-details()
f:xvariable()

explorer.xsl

5 imports

VERSION.xsl

3 variables (3 used only in one other module)

Instructions
§Variable $TITLE
Used by: template
Used in: summarize.xsl
§Variable $VERSION
Used by: template
Used in: summarize.xsl
§Variable $VERHASH
Used by: template
Used in: summarize.xsl
Source code
1
2
3
4
5
6
7
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
   <xsl:variable name="TITLE" select="'XSLT Explorer'"/>
   <xsl:variable name="VERSION" select="'0.1.5'"/>
   <xsl:variable name="VERHASH" select="'2621bf7'"/>
</xsl:stylesheet>

parse.xsl

14 templates, 9 functions, 1 param, 1 FIXME: comment

Instructions
§Param $fixme
Used by: template
Used in: parse.xsl
§Template match ≅ /
Mode: m:parse
Matches: /
§Template match ≅ xsl:stylesheet|xsl:transform
Mode: m:parse
Matches: xsl:stylesheet, xsl:transform
§Template match ≅ xsl:import
Mode: m:parse
Matches: xsl:import
§Template match ≅ xsl:include
Mode: m:parse
Matches: xsl:include
§Template match ≅ xsl:template
Mode: m:parse
Matches: xsl:template
§Template match ≅ xsl:function
Mode: m:parse
Matches: xsl:function
§Template match ≅ xsl:param|xsl:variable
Mode: m:parse
Matches: xsl:param, xsl:variable
§Template match ≅ xsl:call-template
Mode: m:parse
Matches: xsl:call-template
§Template match ≅ xsl:apply-templates
Mode: m:parse
Matches: xsl:apply-templates
§Template match ≅ element()
Mode: m:parse
Matches: element()
§Template match ≅ @select|xsl:evaluate/@context-…
Mode: m:parse
Matches: @select, xsl:evaluate/@context-item, xsl:evaluate/@xpath, xsl:group-by/@group-by, xsl:if/@test, xsl:template/@match, xsl:when/@test
§Template match ≅ @*
Mode: m:parse
Matches: @*
§Template match ≅ comment()
Uses: $fixme
Mode: m:parse
Matches: comment()
§Template match ≅ processing-instruction()|text(…
Mode: m:parse
Matches: processing-instruction(), text()
§Function f:process-avts($context as element(), $value as xs:string)
§Function f:parse-expr($context as element(), $expr as xs:string)
§Function f:unique-id($ref as node()) as xs:string
§Function f:encode-for-id($name as xs:string) as xs:string
§Function f:clark-name#1($node as node()) as xs:string
§Function f:clark-name#2($node as node(), $name as xs:string) as xs:string
Used in: parse.xsl
§Function f:source-location($context as node()) as attribute()*
§Function f:checksum($str as xs:string) as xs:string
§Function f:fletcher16($str as xs:integer*, $len as xs:integer, $index as xs:integer, $sum1 as xs:integer, $sum2 as xs:integer)
FIXME: comments
§Comment
FIXME: Implement XPath parsing as an extension function.
Source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:a="http://nwalsh.com/ns/xslt/analysis"
                xmlns:f="http://nwalsh.com/ns/xslt/functions"
                xmlns:m="http://nwalsh.com/ns/xslt/modes"
                xmlns:p="xpath-31"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns="http://nwalsh.com/ns/xslt/analysis"
                default-mode="m:parse"
                exclude-result-prefixes="a f m p xs"
                version="3.0">

<!-- FIXME: Implement XPath parsing as an extension function. -->
<?xsltexplorer-skip-import?>
<xsl:import href="xpath-31.xslt"/>

<xsl:param name="fixme" select="'FIXME:'"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="xsl:stylesheet|xsl:transform">
  <xsl:param name="href" as="xs:string?" select="()"/>
  <xsl:param name="extra-attributes" as="attribute()*" select="()"/>

  <stylesheet id="{f:encode-for-id(f:unique-id(.))}">
    <xsl:copy-of select="namespace::*[local-name(.) != '']"/>
    <xsl:attribute name="xml:base" select="resolve-uri(base-uri(.))"/>
    <xsl:if test="$href">
      <xsl:attribute name="href" select="$href"/>
    </xsl:if>
    <xsl:sequence select="if (exists($extra-attributes))
                          then $extra-attributes
                          else f:source-location(.)"/>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
  </stylesheet>
</xsl:template>

<xsl:template match="xsl:import">
  <xsl:variable name="pi"
                select="preceding-sibling::node()[not(self::text())][1]"/>
  <xsl:if test="not($pi/self::processing-instruction('xsltexplorer-skip-import'))">
    <xsl:message select="'Importing', @href/string(), '…'"/>
    <xsl:apply-templates select="doc(resolve-uri(@href, base-uri(.)))/node()">
      <xsl:with-param name="href" select="@href/string()"/>
      <xsl:with-param name="extra-attributes" as="attribute()+">
        <xsl:sequence select="f:source-location(.)"/>
        <xsl:attribute name="transclusion" select="'import'"/>
      </xsl:with-param>
    </xsl:apply-templates>
  </xsl:if>
</xsl:template>

<xsl:template match="xsl:include">
  <xsl:variable name="pi"
                select="preceding-sibling::node()[not(self::text())][1]"/>
  <xsl:if test="not(preceding-sibling
                    ::processing-instruction('xsltexplorer-skip-include'))">
    <xsl:message select="'Including', @href/string(), '…'"/>
    <xsl:apply-templates select="doc(resolve-uri(@href, base-uri(.)))/node()">
      <xsl:with-param name="href" select="@href/string()"/>
      <xsl:with-param name="extra-attributes" as="attribute()+">
        <xsl:sequence select="f:source-location(.)"/>
        <xsl:attribute name="transclusion" select="'include'"/>
      </xsl:with-param>
    </xsl:apply-templates>
  </xsl:if>
</xsl:template>

<xsl:template match="xsl:template">
  <xsl:message use-when="false()" select="'xsl:template', @name/string()"/>

  <xsl:variable name="match" as="xs:string*">
    <xsl:for-each select="tokenize(@match, '\|')">
      <xsl:sequence select="if (contains(., '['))
                            then normalize-space(substring-before(., '['))
                            else normalize-space(.)"/>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="match" as="xs:string*">
    <xsl:for-each select="distinct-values($match)">
      <xsl:sort select="."/>
      <xsl:sequence select="."/>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="mode" as="xs:string?">
    <xsl:choose>
      <xsl:when test="@mode">
        <xsl:sequence select="f:clark-name(@mode)"/>
      </xsl:when>
      <xsl:when test="ancestor::xsl:stylesheet[1]/@default-mode">
        <xsl:sequence select="f:clark-name(ancestor::xsl:stylesheet[1]/@default-mode)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="display-mode" as="xs:string?">
    <xsl:choose>
      <xsl:when test="@mode">
        <xsl:sequence select="@mode/string()"/>
      </xsl:when>
      <xsl:when test="ancestor::xsl:stylesheet[1]/@default-mode">
        <xsl:sequence select="ancestor::xsl:stylesheet[1]/@default-mode/string()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <template id="{f:encode-for-id(f:unique-id(.))}">
    <xsl:if test="@name">
      <xsl:attribute name="display-name" select="@name"/>
      <xsl:attribute name="name" select="f:clark-name(@name)"/>
    </xsl:if>
    <xsl:copy-of select="@priority,@as"/>
    <xsl:if test="exists($mode)">
      <xsl:attribute name="mode" select="$mode"/>
      <xsl:attribute name="display-mode" select="$display-mode"/>
    </xsl:if>
    <xsl:if test="exists($match)">
      <xsl:attribute name="match" select="string-join($match,'|')"/>
    </xsl:if>
    <xsl:sequence select="f:source-location(.)"/>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
  </template>
</xsl:template>

<xsl:template match="xsl:function">
  <xsl:message use-when="false()" select="'xsl:function', @name/string()"/>

  <function id="{f:encode-for-id(f:unique-id(.)||'#'||count(xsl:param))}"
            noarity-id="{f:clark-name(@name)}"
            name="{f:clark-name(@name)}#{count(xsl:param)}"
            display-name="{@name}#{count(xsl:param)}">
    <xsl:sequence select="f:source-location(.)"/>
    <xsl:copy-of select="@as"/>
    <xsl:apply-templates select="@*"/>
    <xsl:for-each select="xsl:param">
      <param>
        <xsl:copy-of select="@name,@as"/>
      </param>
    </xsl:for-each>
    <xsl:apply-templates/>
  </function>
</xsl:template>

<xsl:template match="xsl:variable|xsl:param">
  <xsl:message use-when="false()" select="node-name(.), @name/string()"/>

  <variable id="{f:encode-for-id(f:unique-id(.))}"
            class="{local-name(.)}">
    <xsl:copy-of select="namespace::*[local-name(.) != '']"/>
    <xsl:attribute name="display-name" select="@name"/>
    <xsl:attribute name="name" select="f:clark-name(@name)"/>
    <xsl:copy-of select="@static,@as"/>
    <xsl:sequence select="f:source-location(.)"/>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
  </variable>
</xsl:template>

<xsl:template match="xsl:call-template">
  <xsl:message use-when="false()" select="node-name(.), @name/string()"/>

  <call-template ref="{f:clark-name(@name)}">
    <xsl:copy-of select="@name"/>
    <xsl:sequence select="f:source-location(.)"/>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
  </call-template>
</xsl:template>

<xsl:template match="xsl:apply-templates">
  <xsl:message use-when="false()" select="node-name(.), @name/string()"/>

  <xsl:variable name="to" as="xs:string*">
    <xsl:for-each select="tokenize(@select, '\|')">
      <xsl:sequence select="if (contains(., '['))
                            then normalize-space(substring-before(., '['))
                            else normalize-space(.)"/>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="to" as="xs:string*">
    <xsl:for-each select="distinct-values($to)">
      <xsl:sort select="."/>
      <xsl:sequence select="."/>
    </xsl:for-each>
  </xsl:variable>

  <apply-templates>
    <xsl:if test="exists($to)">
      <xsl:attribute name="to" select="string-join($to,'|')"/>
    </xsl:if>
    <xsl:copy-of select="@name,@mode"/>
    <xsl:sequence select="f:source-location(.)"/>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
  </apply-templates>
</xsl:template>

<xsl:template match="element()">
  <xsl:apply-templates select="@*"/>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="@select[namespace-uri(parent::*)
                             ='http://www.w3.org/1999/XSL/Transform']
                     |xsl:if/@test
                     |xsl:when/@test
                     |xsl:evaluate/@context-item
                     |xsl:evaluate/@xpath
                     |xsl:template/@match
                     |xsl:group-by/@group-by">
  <xsl:sequence select="f:parse-expr(parent::*, .)"/>
</xsl:template>

<xsl:template match="@*">
  <xsl:if test="contains(string(.), '{')">
    <!-- this is primitive -->
    <xsl:variable name="value" select="replace(., '\{\{', '')"/>
    <xsl:variable name="value" select="replace($value, '\}\}', '')"/>
    <xsl:sequence select="f:process-avts(parent::*, $value)"/>
  </xsl:if>
</xsl:template>

<xsl:template match="comment()">
  <xsl:if test="contains(., $fixme)">
    <comment id="{f:encode-for-id(f:unique-id(.))}">
      <xsl:sequence select="f:source-location(.)"/>
      <xsl:sequence select="string(.)"/>
    </comment>
  </xsl:if>
</xsl:template>

<xsl:template match="text()|processing-instruction()"/>

<!-- ============================================================ -->

<xsl:function name="f:process-avts">
  <xsl:param name="context" as="element()"/>
  <xsl:param name="value" as="xs:string"/>

  <xsl:if test="contains($value, '{')">
    <xsl:variable name="expr" select="substring-after($value, '{')"/>
    <xsl:variable name="rest" select="substring-after($expr, '}')"/>
    <xsl:variable name="expr" select="substring-before($expr, '}')"/>
    <xsl:sequence select="f:parse-expr($context, $expr)"/>
    <xsl:sequence select="f:process-avts($context, $rest)"/>
  </xsl:if>
</xsl:function>

<xsl:function name="f:parse-expr">
  <xsl:param name="context" as="element()"/>
  <xsl:param name="expr" as="xs:string"/>

  <!-- If there are no $ signs and no parens, then there
       are no variable references or function calls, so
       don't bother trying to parse the whole thing -->
  <xsl:if test="contains($expr, '$') or contains($expr, '(')">
    <xsl:variable name="parsetree">
      <xsl:sequence select="p:parse-XPath($expr)"/>
    </xsl:variable>    

    <xsl:variable name="functions" as="element()*">
      <xsl:for-each select="$parsetree//FunctionCall">
        <xsl:variable name="arity" select="count(ArgumentList/Argument)"/>
        <xsl:variable name="qname" select="FunctionEQName/FunctionName/QName"/>

        <xsl:variable name="ns" as="xs:string">
          <xsl:choose>
            <xsl:when test="not(contains($qname, ':'))"> <!-- default -->
              <xsl:sequence select="'http://www.w3.org/2005/xpath-functions'"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:variable name="prefix" select="substring-before($qname, ':')"/>
              <xsl:sequence select="namespace-uri-for-prefix($prefix, $context)"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="name"
                      select="if (contains($qname, ':'))
                              then substring-after($qname, ':')
                              else string($qname)"/>
        <xsl:variable name="id"
                      select="'{'||$ns||'}'||$name"/>
        <function-call name="{$qname}" ref="{$id}#{$arity}">
          <xsl:copy-of select="$context/namespace::*[local-name(.) != '']"/>
        </function-call>
      </xsl:for-each>
    </xsl:variable>

    <xsl:for-each-group select="$functions" group-by="@ref">
      <xsl:sequence select="current-group()[1]"/>
    </xsl:for-each-group>

    <xsl:variable name="variables" as="element()*">
      <xsl:for-each select="$parsetree//VarRef//FunctionName/QName">
        <xsl:variable name="ns" as="xs:string">
          <xsl:choose>
            <xsl:when test="not(contains(., ':'))"> <!-- default -->
              <xsl:sequence select="''"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:variable name="prefix" select="substring-before(., ':')"/>
              <xsl:sequence select="namespace-uri-for-prefix($prefix, $context)"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="name"
                      select="if (contains(., ':'))
                              then substring-after(., ':')
                              else string(.)"/>
        <xsl:variable name="id"
                      select="'{'||$ns||'}'||$name"/>
        <variable-ref name="{.}" ref="{$id}">
          <xsl:copy-of select="$context/namespace::*[local-name(.) != '']"/>
        </variable-ref>
      </xsl:for-each>
    </xsl:variable>

    <xsl:for-each-group select="$variables" group-by="@ref">
      <xsl:sequence select="current-group()[1]"/>
    </xsl:for-each-group>
  </xsl:if>
</xsl:function>

<!-- ============================================================ -->

<xsl:function name="f:unique-id" as="xs:string">
  <xsl:param name="ref" as="node()"/>

  <xsl:variable name="prefix"
                select="if ($ref/self::xsl:stylesheet)
                        then ''
                        else f:unique-id($ref/ancestor::xsl:stylesheet[1]) || '-'"/>

  <xsl:choose>
    <xsl:when test="$ref/self::xsl:stylesheet">
      <xsl:sequence select="'S-' || f:checksum(base-uri($ref))"/>
    </xsl:when>
    <xsl:when test="$ref/@name">
      <xsl:sequence select="$prefix || f:clark-name($ref/@name)"/>
    </xsl:when>
    <xsl:when test="$ref/self::xsl:template">
      <xsl:sequence select="$prefix || 't-' || (count($ref/preceding::xsl:template)+1)"/>
    </xsl:when>
    <xsl:when test="$ref/self::comment()">
      <xsl:sequence select="$prefix || 'c-' || (count($ref/preceding::comment())+1)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="$prefix || generate-id($ref)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<xsl:function name="f:encode-for-id" as="xs:string">
  <xsl:param name="name" as="xs:string"/>

  <xsl:variable name="id" select="replace($name, '\{https?:/+', '')"/>
  <xsl:variable name="id" select="translate($id, '{}/#', '...,')"/>

  <xsl:sequence select="$id"/>
</xsl:function>

<xsl:function name="f:clark-name" as="xs:string">
  <xsl:param name="node" as="node()"/>
  <xsl:choose>
    <xsl:when test="$node/self::attribute()">
      <xsl:sequence select="f:clark-name($node/parent::*, string($node))"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="f:clark-name($node, string($node))"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<xsl:function name="f:clark-name" as="xs:string">
  <xsl:param name="node" as="node()"/>
  <xsl:param name="name" as="xs:string"/>

  <xsl:choose>
    <xsl:when test="contains($name, ':')">
      <xsl:variable name="prefix" select="substring-before($name, ':')"/>
      <xsl:variable name="name" select="substring-after($name, ':')"/>
      <xsl:variable name="ns" select="namespace-uri-for-prefix($prefix, $node)"/>
      <xsl:sequence select="'{' || $ns || '}' || $name"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="'{}' || $name"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<xsl:function name="f:source-location" as="attribute()*">
  <xsl:param name="context" as="node()"/>

  <xsl:if xmlns:saxon="http://saxon.sf.net/"
          use-when="function-available('saxon:line-number')"
          test="saxon:line-number($context) gt 0">
    <xsl:attribute name="line-number" select="saxon:line-number($context)"/>
    <xsl:if use-when="function-available('saxon:column-number')"
            test="saxon:column-number($context) gt 0">
      <xsl:attribute name="column-number" select="saxon:column-number($context)"/>
    </xsl:if>
  </xsl:if>
</xsl:function>

<!-- ============================================================ -->
<!-- From: https://stackoverflow.com/questions/6753343/using-xsl-to-make-a-hash-of-xml-file -->

<xsl:function name="f:checksum" as="xs:string">
  <xsl:param name="str" as="xs:string"/>

  <xsl:variable name="filename"
                select="if (($xspec-tests = ('1', 'yes', 'true'))
                            and contains($str, 'src/test/resources/'))
                        then substring-after($str, 'src/test/resources')
                        else $str"/>

  <xsl:variable name="codepoints" select="string-to-codepoints($filename)"/>
  <xsl:variable name="checksum"
                select="f:fletcher16($codepoints, count($codepoints), 1, 0, 0)"/>
  <!-- FIX ME: turn this into hex -->
  <xsl:sequence select="string($checksum)"/>
</xsl:function>

<xsl:function name="f:fletcher16">
  <xsl:param name="str" as="xs:integer*"/>
  <xsl:param name="len" as="xs:integer" />
  <xsl:param name="index" as="xs:integer" />
  <xsl:param name="sum1" as="xs:integer" />
  <xsl:param name="sum2" as="xs:integer"/>
  <xsl:choose>
    <xsl:when test="$index gt $len">
      <xsl:sequence select="$sum2 * 256 + $sum1"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="newSum1" as="xs:integer"
                    select="($sum1 + $str[$index]) mod 255"/>
      <xsl:sequence select="f:fletcher16($str, $len, $index + 1, $newSum1,
                                         ($sum2 + $newSum1) mod 255)" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

</xsl:stylesheet>

resolve.xsl

5 templates, 1 function

Instructions
§Template match ≅ a:variable-ref
Mode: m:resolve
Matches: a:variable-ref
§Template match ≅ a:call-template
Mode: m:resolve
Matches: a:call-template
§Template match ≅ a:function-call
Mode: m:resolve
Matches: a:function-call
§Template match ≅ element()
Mode: m:resolve
Matches: element()
§Template match ≅ attribute()|comment()|processi…
Mode: m:resolve
Matches: attribute(), comment(), processing-instruction(), text()
§Function f:xvariable($context as element(), $ref as xs:string) as element(a:variable)?
Source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:a="http://nwalsh.com/ns/xslt/analysis"
                xmlns:f="http://nwalsh.com/ns/xslt/functions"
                xmlns:fp="http://nwalsh.com/ns/xslt/functions/private"
                xmlns:h="http://www.w3.org/1999/xhtml"
                xmlns:m="http://nwalsh.com/ns/xslt/modes"
                xmlns:t="http://nwalsh.com/ns/xslt/templates"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns="http://nwalsh.com/ns/xslt/analysis"
                default-mode="m:resolve"
                exclude-result-prefixes="a f fp h m t xs"
                version="3.0">

<xsl:template match="a:variable-ref">
  <xsl:variable name="ref" select="f:xvariable(., @ref)"/>

  <!-- If it's a reference to a top-level variable, then the last one wins. -->
  <xsl:variable name="ref" select="if (empty($ref) or $ref/parent::a:stylesheet)
                                   then (//a:stylesheet/a:variable[@name = $ref/@name])[last()]
                                   else $ref"/>

  <xsl:copy>
    <xsl:apply-templates select="@*"/>

    <xsl:choose>
      <xsl:when test="empty($ref)">
        <xsl:message select="'No variable:', @ref/string()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="xref" select="$ref/@id"/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:apply-templates select="*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="a:call-template">
  <xsl:variable name="ref" select="@ref"/>
  <xsl:variable name="ref" select="(//a:stylesheet/a:template[@name = $ref])[last()]"/>

  <xsl:copy>
    <xsl:apply-templates select="@*"/>

    <xsl:choose>
      <xsl:when test="empty($ref)">
        <xsl:message select="'No template:', @ref/string()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="xref" select="$ref/@id"/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:apply-templates select="*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="a:function-call">
  <xsl:variable name="ref" select="@ref"/>
  <xsl:variable name="ref" select="(//a:stylesheet/a:function[@name = $ref])[last()]"/>

  <xsl:copy>
    <xsl:apply-templates select="@*"/>

    <xsl:choose>
      <xsl:when test="empty($ref)">
        <xsl:choose>
          <xsl:when test="starts-with(@ref, '{http://www.w3.org/2005/xpath-functions}')
                          or starts-with(@ref, '{http://www.w3.org/2001/XMLSchema}')
                          or starts-with(@ref, '{http://saxon.sf.net/}')
                          or starts-with(@ref, '{http://www.w3.org/2005/xpath-functions/array}')
                          or starts-with(@ref, '{http://www.w3.org/2005/xpath-functions/map}')">
            <!-- ignore standard, builtin functions -->
          </xsl:when>
          <xsl:otherwise>
            <xsl:message select="'No function:', @ref/string()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="xref" select="$ref/@id"/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:apply-templates select="*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="element()">
  <xsl:copy>
    <xsl:apply-templates select="@*,node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="attribute()|text()|comment()|processing-instruction()">
  <xsl:copy/>
</xsl:template>

<xsl:function name="f:xvariable" as="element(a:variable)?">
  <xsl:param name="context" as="element()"/>
  <xsl:param name="ref" as="xs:string"/>

  <xsl:choose>
    <xsl:when test="$context/preceding-sibling::a:variable[@name=$ref]">
      <xsl:sequence select="$context/preceding-sibling::a:variable[@name=$ref][1]"/>
    </xsl:when>
    <xsl:when test="$context/self::a:stylesheet">
      <xsl:sequence select="(root($context)//a:stylesheet/a:variable[@name = $ref])[last()]"/>
    </xsl:when>
    <xsl:when test="empty($context)">
      <xsl:sequence select="()"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="f:xvariable($context/parent::*, $ref)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

</xsl:stylesheet>

analyze.xsl

6 templates, 7 functions

Instructions
§Template match ≅ /
Mode: m:analyze
Matches: /
§Template match ≅ a:stylesheet
Mode: m:analyze
Matches: a:stylesheet
§Template match ≅ a:function
§Template match ≅ a:variable
§Template match ≅ a:template
§Template match ≅ a:comment
Mode: m:analyze
Matches: a:comment
§Function f:variables-referenced($node as element()) as element(a:variable)*
§Function f:function-calls($node as element()) as element(a:function)*
Used by: template, template
Used in: analyze.xsl
§Function f:template-calls($node as element()) as element(a:template)*
§Function f:generate-id($node as element()) as xs:string
§Function f:variable($context as element(), $ref as xs:string) as element(a:variable)?
§Function f:template-shadows($context as element(), $ref as xs:string) as element(a:template)?
§Function f:function-shadows($context as element(), $ref as xs:string) as element(a:function)?
Source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:a="http://nwalsh.com/ns/xslt/analysis"
                xmlns:f="http://nwalsh.com/ns/xslt/functions"
                xmlns:fp="http://nwalsh.com/ns/xslt/functions/private"
                xmlns:h="http://www.w3.org/1999/xhtml"
                xmlns:m="http://nwalsh.com/ns/xslt/modes"
                xmlns:t="http://nwalsh.com/ns/xslt/templates"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns="http://nwalsh.com/ns/xslt/analysis"
                default-mode="m:analyze"
                exclude-result-prefixes="a f fp h m t xs"
                version="3.0">

<xsl:key name="fnoarity" match="a:function" use="@noarity-id"/>
<xsl:key name="id" match="*" use="@id"/>
<xsl:key name="xref" match="*" use="@xref"/>

<xsl:template match="/">
  <summary>
    <xsl:attribute name="xml:base" select="base-uri(/*)"/>
    <xsl:attribute name="imports"
                   select="count(/a:stylesheet//a:stylesheet[@transclusion='import'])"/>
    <xsl:attribute name="includes"
                   select="count(/a:stylesheet//a:stylesheet[@transclusion='include'])"/>
    <xsl:attribute name="variables"
                   select="count(//a:variable[parent::a:stylesheet and @class='variable'])"/>
    <xsl:attribute name="params"
                   select="count(//a:variable[parent::a:stylesheet and @class='param'])"/>
    <xsl:attribute name="templates"
                   select="count(//a:template[parent::a:stylesheet])"/>
    <xsl:attribute name="functions"
                   select="count(//a:function[parent::a:stylesheet])"/>
    <xsl:attribute name="fixmes"
                   select="count(//a:comment)"/>
    <xsl:apply-templates/>
  </summary>
</xsl:template>

<xsl:template match="a:stylesheet">
  <xsl:variable name="stylesheet" select="."/>
  <xsl:variable name="depth" select="count(ancestor::a:stylesheet)+1"/>

  <stylesheet>
    <xsl:copy-of select="@id,@xml:base,@depth,@href,@transclusion"/>

    <xsl:variable name="imports" select="a:stylesheet[@transclusion='import']"/>
    <xsl:variable name="includes" select="a:stylesheet[@transclusion='include']"/>
    <xsl:variable name="vars" select="a:variable[@class='variable']"/>
    <xsl:variable name="params" select="a:variable[@class='param']"/>
    <xsl:variable name="templates" select="a:template"/>
    <xsl:variable name="functions" select="a:function"/>
    <xsl:variable name="fixmes" select=".//a:comment except .//a:stylesheet//a:comment"/>

    <xsl:attribute name="imports" select="count($imports)"/>
    <xsl:attribute name="includes" select="count($includes)"/>
    <xsl:attribute name="variables" select="count($vars)"/>
    <xsl:attribute name="params" select="count($params)"/>
    <xsl:attribute name="templates" select="count($templates)"/>
    <xsl:attribute name="functions" select="count($functions)"/>
    <xsl:attribute name="fixmes" select="count($fixmes)"/>

    <xsl:variable name="vp-unused" as="element(a:variable)*"
                  select="a:variable[empty(key('xref', @id))]"/>

    <xsl:variable name="vp-shadows"
                  select="a:variable[f:variable(., @name/string())]"/>

    <xsl:variable name="vp-elsewhere" as="element(a:variable)*">
      <xsl:for-each select="a:variable">
        <xsl:variable name="used-in"
                      select="key('xref', @id, root($stylesheet))
                                /ancestor::a:stylesheet[not(@transclusion='include')][1]
                              union ()"/>
        <xsl:if test="count($used-in) = 1 and exists($used-in except parent::*)">
          <xsl:sequence select="."/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <variables>
      <xsl:for-each select="$vp-unused[@class='variable']">
        <unused><xsl:sequence select="f:generate-id(.)"/></unused>
      </xsl:for-each>
      <xsl:for-each select="$vp-shadows[@class='variable']">
        <shadows><xsl:sequence select="f:generate-id(.)"/></shadows>
      </xsl:for-each>
      <xsl:for-each select="$vp-elsewhere[@class='variable']">
        <elsewhere><xsl:sequence select="f:generate-id(.)"/></elsewhere>
      </xsl:for-each>
    </variables>

    <params>
      <xsl:for-each select="$vp-unused[@class='param']">
        <unused><xsl:sequence select="f:generate-id(.)"/></unused>
      </xsl:for-each>
      <xsl:for-each select="$vp-shadows[@class='param']">
        <shadows><xsl:sequence select="f:generate-id(.)"/></shadows>
      </xsl:for-each>
      <xsl:for-each select="$vp-elsewhere[@class='param']">
        <elsewhere><xsl:sequence select="f:generate-id(.)"/></elsewhere>
      </xsl:for-each>
    </params>

    <xsl:variable name="templates-unused" as="element(a:template)*">
      <xsl:for-each select="a:template[@name]">
        <xsl:variable name="this" select="."/>
        <xsl:variable name="used-in" as="element()*">
          <xsl:for-each select="key('xref', @id, root($stylesheet))">
            <!-- recursion doesn't count as use -->
            <xsl:if test="not($this intersect ancestor::*)">
              <xsl:sequence select="."/>
            </xsl:if>
          </xsl:for-each>
        </xsl:variable>
        <xsl:if test="empty($used-in)">
          <xsl:sequence select="."/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="templates-shadow"
                  select="a:template[@name and f:template-shadows(., @name/string())]"/>

    <xsl:variable name="templates-elsewhere" as="element(a:template)*">
      <xsl:for-each select="a:template[@name]">
        <xsl:variable name="used-by"
                      select="(key('xref', @id)
                              /ancestor::*[parent::a:stylesheet[not(@transclusion='include')]][1])
                              union ()"/>
        <xsl:variable name="used-by-modules"
                      select="$used-by/parent::* union ()"/>
        <xsl:if test="count($used-by-modules) = 1
                      and not(parent::* is $used-by-modules)">
          <xsl:sequence select="."/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <templates>
      <xsl:for-each select="$templates-unused">
        <unused><xsl:sequence select="f:generate-id(.)"/></unused>
      </xsl:for-each>
      <xsl:for-each select="$templates-shadow">
        <shadows><xsl:sequence select="f:generate-id(.)"/></shadows>
      </xsl:for-each>
      <xsl:for-each select="$templates-elsewhere">
        <elsewhere><xsl:sequence select="f:generate-id(.)"/></elsewhere>
      </xsl:for-each>
    </templates>

    <comments/>

    <xsl:variable name="functions-shadow"
                  select="a:function[@name and f:function-shadows(., @name/string())]"/>

    <functions>
      <xsl:variable name="functions-unused" as="element(a:function)*">
        <xsl:for-each select="a:function">
          <xsl:variable name="this" select="."/>
          <xsl:variable name="used-in" as="element()*">
            <xsl:for-each select="key('xref', @id, root($stylesheet))">
              <!-- recursion doesn't count as use -->
              <xsl:if test="not($this intersect ancestor::*)">
                <xsl:sequence select="."/>
              </xsl:if>
            </xsl:for-each>
          </xsl:variable>
          <xsl:if test="empty($used-in)">
            <xsl:sequence select="."/>
          </xsl:if>
        </xsl:for-each>
      </xsl:variable>

      <xsl:variable name="functions-elsewhere" as="element(a:function)*">
        <xsl:for-each select="a:function">
          <xsl:variable name="used-in"
                        select="key('xref', @id, root($stylesheet))
                                    /ancestor::a:stylesheet[not(@transclusion='include')][1]
                                union ()"/>
          <xsl:if test="count($used-in) = 1 and exists($used-in except parent::*)">
            <xsl:sequence select="."/>
          </xsl:if>
        </xsl:for-each>
      </xsl:variable>

      <xsl:for-each select="$functions-unused">
        <unused><xsl:sequence select="f:generate-id(.)"/></unused>
      </xsl:for-each>
      <xsl:for-each select="$functions-shadow">
        <shadows><xsl:sequence select="f:generate-id(.)"/></shadows>
      </xsl:for-each>
      <xsl:for-each select="$functions-elsewhere">
        <elsewhere><xsl:sequence select="f:generate-id(.)"/></elsewhere>
      </xsl:for-each>
    </functions>

    <xsl:apply-templates select="a:variable|a:template|a:function|a:stylesheet"/>
    <xsl:apply-templates select=".//a:comment except .//a:stylesheet//a:comment"/>
  </stylesheet>
</xsl:template>

<xsl:template match="a:function">
  <xsl:variable name="used-by" as="element()*">
    <xsl:for-each select="key('xref', @id)">
      <xsl:sequence
          select="ancestor::*[parent::a:stylesheet[not(@transclusion='include')]][1]"/>
    </xsl:for-each>
  </xsl:variable>

  <!-- remove dups -->
  <xsl:variable name="used-by" select="$used-by union ()"/>

  <xsl:variable name="used-by-modules"
                select="$used-by/parent::* union ()"/>

  <xsl:variable name="used-only-elsewhere"
                select="count($used-by-modules) = 1
                        and not(parent::* is $used-by-modules)"/>

  <xsl:variable name="shadows"
                select="f:function-shadows(., @name/string())"/>

  <xsl:variable name="classes" as="xs:string+">
    <xsl:sequence select="local-name(.)"/>
    <xsl:sequence select="if (empty($used-by)) then 'unused' else ()"/>
    <xsl:sequence select="if ($shadows) then 'shadows' else ()"/>
    <xsl:sequence select="if ($used-only-elsewhere) then 'elsewhere' else ()"/>
  </xsl:variable>

  <function id="{f:generate-id(.)}"
            class="{string-join($classes, ' ')}"
            recursive="{exists(. intersect $used-by)}"
            elsewhere="{$used-only-elsewhere}">
    <xsl:copy-of select="@name,@display-name,@id,@as,@line-number,@column-number"/>
    <xsl:copy-of select="a:param" copy-namespaces="no"/>

    <xsl:for-each select="$shadows">
      <shadow><xsl:sequence select="f:generate-id(.)"/></shadow>
    </xsl:for-each>

    <xsl:for-each select="key('fnoarity', @noarity-id) except $shadows">
      <no-arity><xsl:sequence select="f:generate-id(.)"/></no-arity>
    </xsl:for-each>

    <xsl:for-each select="f:variables-referenced(.)">
      <variable><xsl:sequence select="f:generate-id(.)"/></variable>
    </xsl:for-each>

    <xsl:for-each select="$used-by/self::a:function except .">
      <function><xsl:sequence select="f:generate-id(.)"/></function>
    </xsl:for-each>

    <xsl:for-each select="f:template-calls(.)">
      <template><xsl:sequence select="f:generate-id(.)"/></template>
    </xsl:for-each>

    <xsl:for-each select="$used-by">
      <used-by><xsl:sequence select="f:generate-id(.)"/></used-by>
    </xsl:for-each>

    <xsl:for-each select="$used-by-modules">
      <used-by-module><xsl:sequence select="f:generate-id(.)"/></used-by-module>
    </xsl:for-each>
  </function>
</xsl:template>

<xsl:template match="a:variable">
  <xsl:variable name="this" select="."/>
  <xsl:variable name="id" select="@id/string()"/>

  <xsl:variable name="used-by" as="element()*">
    <xsl:for-each select="key('xref', @id)">
      <xsl:sequence
          select="ancestor::*[parent::a:stylesheet[not(@transclusion='include')]][1]"/>
    </xsl:for-each>
  </xsl:variable>

  <!-- remove dups -->
  <xsl:variable name="used-by" select="$used-by union ()"/>

  <xsl:variable name="used-by-modules"
                select="$used-by/parent::* union ()"/>

  <xsl:variable name="used-only-elsewhere"
                select="count($used-by-modules) = 1
                        and not(parent::* is $used-by-modules)"/>

  <xsl:variable name="shadows" select="f:variable(., @name/string())"/>

  <xsl:variable name="classes" as="xs:string+">
    <xsl:sequence select="@class/string()"/>
    <xsl:sequence select="if ($shadows) then 'shadows' else ()"/>
    <xsl:sequence select="if (empty($used-by)) then 'unused' else ()"/>
    <xsl:sequence select="if ($used-only-elsewhere) then 'elsewhere' else ()"/>
  </xsl:variable>

  <variable id="{f:generate-id(.)}"
            class="{string-join($classes, ' ')}"
            type="{@class}"
            elsewhere="{$used-only-elsewhere}">
    <xsl:copy-of select="@static,@name,@line-number,@column-number,@display-name,@as"/>

    <xsl:for-each select="$shadows">
      <shadow><xsl:sequence select="f:generate-id(.)"/></shadow>
    </xsl:for-each>

    <xsl:for-each select="f:variables-referenced(.)">
      <variable><xsl:sequence select="f:generate-id(.)"/></variable>
    </xsl:for-each>

    <xsl:for-each select="f:function-calls(.)">
      <function><xsl:sequence select="f:generate-id(.)"/></function>
    </xsl:for-each>

    <xsl:for-each select="f:template-calls(.)">
      <template><xsl:sequence select="f:generate-id(.)"/></template>
    </xsl:for-each>

    <xsl:for-each select="$used-by">
      <used-by class="{local-name(.)}"><xsl:sequence select="f:generate-id(.)"/></used-by>
    </xsl:for-each>

    <xsl:for-each select="$used-by-modules">
      <used-by-module><xsl:sequence select="f:generate-id(.)"/></used-by-module>
    </xsl:for-each>
  </variable>
</xsl:template>

<xsl:template match="a:template">
  <xsl:variable name="this" select="."/>

  <xsl:variable name="used-by" as="element()*">
    <xsl:if test="@name">
      <xsl:sequence
          select="(key('xref', @id)/ancestor::*
                     [parent::a:stylesheet[not(@transclusion='include')]][1])
                   union ()"/>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="used-by-modules"
                select="$used-by/parent::* union ()"/>

  <xsl:variable name="used-only-elsewhere"
                select="count($used-by-modules) = 1
                        and not(parent::* is $used-by-modules)"/>

  <xsl:variable name="shadows"
                select="if (@name) then f:template-shadows(., @name/string()) else ()"/>

  <xsl:variable name="classes" as="xs:string+">
    <xsl:sequence select="local-name(.)"/>
    <xsl:sequence select="if ($shadows) then 'shadows' else ()"/>
    <xsl:sequence select="if (@name and empty($used-by)) then 'unused' else ()"/>
    <xsl:sequence select="if ($used-only-elsewhere) then 'elsewhere' else ()"/>
  </xsl:variable>

  <template id="{f:generate-id(.)}"
            class="{string-join($classes, ' ')}"
            recursive="{exists(. intersect $used-by)}"
            elsewhere="{$used-only-elsewhere}">
    <xsl:copy-of select="@name,@display-name,@match,@as,@mode,@display-mode,
                         @priority,@line-number,@column-number"/>

    <xsl:for-each select="$shadows">
      <shadow><xsl:sequence select="f:generate-id(.)"/></shadow>
    </xsl:for-each>

    <xsl:for-each select="f:variables-referenced(.)">
      <variable><xsl:sequence select="f:generate-id(.)"/></variable>
    </xsl:for-each>

    <xsl:for-each select="f:function-calls(.)">
      <function><xsl:sequence select="f:generate-id(.)"/></function>
    </xsl:for-each>

    <xsl:for-each select="f:template-calls(.) except .">
      <template><xsl:sequence select="f:generate-id(.)"/></template>
    </xsl:for-each>

    <xsl:for-each select="$used-by">
      <used-by><xsl:sequence select="f:generate-id(.)"/></used-by>
    </xsl:for-each>

    <xsl:for-each select="$used-by-modules">
      <used-by-module><xsl:sequence select="f:generate-id(.)"/></used-by-module>
    </xsl:for-each>
  </template>
</xsl:template>

<xsl:template match="a:comment">
  <comment id="{f:generate-id(.)}"
           class="comment">
    <xsl:copy-of select="@line-number,@column-number"/>
    <xsl:sequence select="normalize-space(.)"/>
  </comment>
</xsl:template>

<xsl:function name="f:variables-referenced" as="element(a:variable)*">
  <xsl:param name="node" as="element()"/>
  <xsl:sequence select="($node//a:variable-ref ! key('id', @xref))[parent::a:stylesheet] union ()"/>
</xsl:function>

<xsl:function name="f:function-calls" as="element(a:function)*">
  <xsl:param name="node" as="element()"/>
  <xsl:sequence select="($node//a:function-call ! key('id', @xref))[parent::a:stylesheet] union ()"/>
</xsl:function>

<xsl:function name="f:template-calls" as="element(a:template)*">
  <xsl:param name="node" as="element()"/>
  <xsl:sequence select="($node//a:call-template ! key('id', @xref))[parent::a:stylesheet] union ()"/>
</xsl:function>

<xsl:function name="f:generate-id" as="xs:string">
  <xsl:param name="node" as="element()"/>
  <xsl:if test="not($node/@id)">
    <xsl:message terminate="yes" select="'No id?', $node"/>
  </xsl:if>
  <xsl:sequence select="$node/@id/string()"/>
</xsl:function>

<!-- ============================================================ -->

<xsl:function name="f:variable" as="element(a:variable)?">
  <xsl:param name="context" as="element()"/>
  <xsl:param name="ref" as="xs:string"/>

  <xsl:choose>
    <xsl:when test="$context/preceding-sibling::a:variable[@name=$ref]">
      <xsl:sequence select="$context/preceding-sibling::a:variable[@name=$ref][1]"/>
    </xsl:when>
    <xsl:when test="$context/self::a:stylesheet">
      <xsl:sequence select="($context/preceding::a:stylesheet/a:variable[@name=$ref])[last()]"/>
    </xsl:when>
    <xsl:when test="empty($context)">
      <xsl:sequence select="()"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="f:variable($context/parent::*, $ref)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<xsl:function name="f:template-shadows" as="element(a:template)?">
  <xsl:param name="context" as="element()"/>
  <xsl:param name="ref" as="xs:string"/>

  <xsl:choose>
    <xsl:when test="$context/preceding-sibling::a:template[@name=$ref]">
      <xsl:sequence select="$context/preceding-sibling::a:template[@name=$ref][1]"/>
    </xsl:when>
    <xsl:when test="$context/self::a:stylesheet">
      <xsl:sequence
          select="($context/preceding::a:stylesheet/a:template[@name=$ref])[last()]"/>
    </xsl:when>
    <xsl:when test="empty($context)">
      <xsl:sequence select="()"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="f:template-shadows($context/parent::*, $ref)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<xsl:function name="f:function-shadows" as="element(a:function)?">
  <xsl:param name="context" as="element()"/>
  <xsl:param name="ref" as="xs:string"/>

  <xsl:choose>
    <xsl:when test="$context/preceding-sibling::a:function[@name=$ref]">
      <xsl:sequence select="$context/preceding-sibling::a:function[@name=$ref][1]"/>
    </xsl:when>
    <xsl:when test="$context/self::a:stylesheet">
      <xsl:sequence
          select="($context/preceding::a:stylesheet/a:function[@name=$ref])[last()]"/>
    </xsl:when>
    <xsl:when test="empty($context)">
      <xsl:sequence select="()"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="f:function-shadows($context/parent::*, $ref)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

</xsl:stylesheet>

summarize.xsl

15 templates, 4 functions, 1 variable, 5 params

Instructions
§Param $css
Used by: template
Used in: summarize.xsl
§Param $js
Used by: template
Used in: summarize.xsl
§Param $prismcss
Used by: template
Used in: summarize.xsl
§Param $prismjs
Used by: template
Used in: summarize.xsl
§Param $title
Used by: template
Used in: summarize.xsl
§Variable $Z
Used by: template
Used in: summarize.xsl
§Template match ≅ a:summary
Mode: m:summarize
Matches: a:summary
§Template match ≅ a:stylesheet
Mode: m:summarize
Matches: a:stylesheet
§Template match ≅ a:function
§Template match ≅ a:variable
§Template match ≅ a:template
§Template match ≅ a:comment
Mode: m:summarize
Matches: a:comment
§Template t:variables-referenced match ≅
Used in: summarize.xsl
Mode: m:summarize
§Template t:functions-called match ≅
Used in: summarize.xsl
Mode: m:summarize
§Template t:templates-called match ≅
Used in: summarize.xsl
Mode: m:summarize
§Function f:reference-list($refs as element()+)
§Template match ≅ *
Mode: m:reference
Matches: *
§Template match ≅ a:stylesheet
Mode: m:reference
Matches: a:stylesheet
§Template match ≅ a:variable
Mode: m:reference
Matches: a:variable
§Template match ≅ a:function
Mode: m:reference
Matches: a:function
§Template match ≅ a:template
Mode: m:reference
Matches: a:template
§Template match ≅ a:stylesheet
Mode: m:toc
Matches: a:stylesheet
§Function f:line-link($node as element()) as element(h:span)?
§Function f:summary-count($count as xs:string, $label as xs:string) as element(h:span)?
Used by: template, template
Used in: summarize.xsl
§Function f:summary-details($detail as element())
Used by: template
Used in: summarize.xsl
Source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:a="http://nwalsh.com/ns/xslt/analysis"
                xmlns:f="http://nwalsh.com/ns/xslt/functions"
                xmlns:fp="http://nwalsh.com/ns/xslt/functions/private"
                xmlns:h="http://www.w3.org/1999/xhtml"
                xmlns:m="http://nwalsh.com/ns/xslt/modes"
                xmlns:t="http://nwalsh.com/ns/xslt/templates"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns="http://www.w3.org/1999/xhtml"
                default-mode="m:summarize"
                exclude-result-prefixes="a f fp h m t xs"
                version="3.0">

<xsl:param name="css" select="'css/xsltexplorer.css'"/>
<xsl:param name="js" select="'js/xsltexplorer.js'"/>
<xsl:param name="prismcss" select="'css/prism.css'"/>
<xsl:param name="prismjs" select="'js/prism.js'"/>

<xsl:param name="title" select="()"/>

<xsl:variable name="Z" select="xs:dayTimeDuration('PT0H')"/>

<xsl:template match="a:summary">
  <html>
    <head>
      <title>
        <xsl:sequence select="if (exists($title))
                              then $title
                              else $TITLE
                                   || ': '
                                   || tokenize(base-uri(/*), '/')[last()]"/>
      </title>
      <meta name="viewport" content="width=device-width,initial-scale=1" />
      <link rel="shortcut icon" href="https://xslt.xmlexplorer.com/img/icon64.png"
            type="image/png" />
      <link rel="stylesheet" href="{$prismcss}"/>
      <link rel="stylesheet" href="{$css}"/>
      <link rel="schema.dc" href="http://purl.org/dc/elements/1.1/"/>
      <meta name="dc.modified"
            content="{format-dateTime(adjust-dateTime-to-timezone(current-dateTime(), $Z),
                                      '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]Z')}"/>
      <meta name="generator" content="{$TITLE} {$VERSION} / {$VERHASH}"/>
      <script src="{$prismjs}"></script>
    </head>
    <body>
      <main>
        <h1>
          <a href="https://xslt.xmlexplorer.com/">
            <img class="logo" src="https://xslt.xmlexplorer.com/img/logo256.png"/>
          </a>
          <xsl:sequence select="if (exists($title))
                                then $title
                                else $TITLE
                                     || ': '
                                     || tokenize(base-uri(/*), '/')[last()]"/>
        </h1>

        <xsl:variable name="details" as="element(h:span)*">
          <xsl:sequence select="@imports ! f:summary-count(./string(), 'import')"/>
          <xsl:sequence select="@includes ! f:summary-count(./string(), 'include')"/>
          <xsl:sequence select="@templates ! f:summary-count(./string(), 'template')"/>
          <xsl:sequence select="@functions ! f:summary-count(./string(), 'function')"/>
          <xsl:sequence select="@variables ! f:summary-count(./string(), 'variable')"/>
          <xsl:sequence select="@params ! f:summary-count(./string(), 'param')"/>
          <xsl:sequence select="@fixmes ! f:summary-count(./string(), 'FIXME: comment')"/>
        </xsl:variable>

        <xsl:if test="$details">
          <p>
            <xsl:for-each select="$details">
              <xsl:if test="position() gt 1">, </xsl:if>
              <!-- strip off the spans so they aren't visible to CSS/JS -->
              <xsl:sequence select="node()"/>
            </xsl:for-each>
          </p>
        </xsl:if>

        <xsl:if test="a:stylesheet/a:stylesheet">
          <div class="toc">
            <span class="closed">List of Imports</span>
            <xsl:apply-templates select="a:stylesheet" mode="m:toc"/>
          </div>
        </xsl:if>

        <xsl:variable name="list" select="//a:stylesheet/a:template[@name]"/>
        <xsl:if test="$list">
          <div class="lot">
            <span class="closed">List of Templates</span>
            <dl class="columns">
              <xsl:for-each select="$list">
                <xsl:sort select="@id"/>
                <dt>
                  <xsl:apply-templates select="." mode="m:reference"/>
                </dt>
              </xsl:for-each>
            </dl>
          </div>
        </xsl:if>         

        <xsl:variable name="list" select="//a:stylesheet/a:function"/>
        <xsl:if test="$list">
          <div class="lof">
            <span class="closed">List of Functions</span>
            <dl class="columns">
              <xsl:for-each select="$list">
                <xsl:sort select="@id"/>
                <dt>
                  <xsl:apply-templates select="." mode="m:reference"/>
                </dt>
              </xsl:for-each>
            </dl>
          </div>
        </xsl:if>            

        <xsl:apply-templates/>
      </main>
      <footer>
        <xsl:variable name="now" select="adjust-dateTime-to-timezone(current-dateTime(), $Z)"/>
        <p>
          <xsl:text>Generated by </xsl:text>
          <a href="https://xslt.xmlexplorer.com/">
            <xsl:sequence select="$TITLE"/>
          </a>
          <xsl:text> version </xsl:text>
          <span title="git hash: {$VERHASH}">
            <xsl:sequence select="$VERSION"/>
          </span>
          <xsl:text> at </xsl:text>
          <xsl:sequence select="format-dateTime($now, '[H01]:[m01]')"/>
          <xsl:text> on </xsl:text>
          <xsl:sequence select="format-dateTime($now, '[D01] [MNn,*-3] [Y0001]')"/>
          <xsl:text>.</xsl:text>
        </p>
        <p>
          <xsl:text>Source: </xsl:text>
          <xsl:sequence select="@xml:base/string()"/>
        </p>
      </footer>
    </body>
    <script src="{$js}"/>
  </html>
</xsl:template>

<xsl:template match="a:stylesheet">
  <xsl:variable name="stylesheet" select="."/>

  <xsl:variable name="hsize" select="min((count(ancestor::a:stylesheet)+1,4))"/>

  <div class="{local-name(.)}" id="{@id}">
    <xsl:element name="h{$hsize}" namespace="http://www.w3.org/1999/xhtml">
      <xsl:value-of select="tokenize(@xml:base, '/')[last()]"/>
      <xsl:message select="'Summarizing', @href/string(), '…'"/>
    </xsl:element>

    <xsl:variable name="summary" as="element(h:span)*">
      <xsl:sequence select="f:summary-count(@imports, 'import')"/>
      <xsl:sequence select="f:summary-count(@includes, 'include')"/>
    </xsl:variable>

    <xsl:if test="exists($summary)">
      <p>
        <xsl:for-each select="$summary">
          <xsl:if test="position() gt 1">, </xsl:if>
          <xsl:sequence select="."/>
        </xsl:for-each>
      </p>
    </xsl:if>

    <xsl:apply-templates select="a:stylesheet"/>

    <xsl:variable name="summary" as="element(h:span)*">
      <xsl:if test="@templates ne '0'">
        <span>
          <xsl:sequence select="f:summary-count(@templates, 'template')"/>
          <xsl:sequence select="f:summary-details(a:templates)"/>
        </span>
      </xsl:if>          
      <xsl:if test="@functions ne '0'">
        <span>
          <xsl:sequence select="f:summary-count(@functions, 'function')"/>
          <xsl:sequence select="f:summary-details(a:functions)"/>
        </span>
      </xsl:if>          
      <xsl:if test="@variables ne '0'">
        <span>
          <xsl:sequence select="f:summary-count(@variables, 'variable')"/>
          <xsl:sequence select="f:summary-details(a:variables)"/>
        </span>
      </xsl:if>          
      <xsl:if test="@params ne '0'">
        <span>
          <xsl:sequence select="f:summary-count(@params, 'param')"/>
          <xsl:sequence select="f:summary-details(a:params)"/>
        </span>
      </xsl:if>          
      <xsl:if test="@fixmes ne '0'">
        <span>
          <xsl:sequence select="f:summary-count(@fixmes, 'FIXME: comment')"/>
          <xsl:sequence select="f:summary-details(a:comments)"/>
        </span>
      </xsl:if>          
    </xsl:variable>

    <xsl:if test="exists($summary)">
      <p>
        <xsl:for-each select="$summary">
          <xsl:if test="position() gt 1">, </xsl:if>
          <xsl:sequence select="."/>
        </xsl:for-each>
      </p>
    </xsl:if>

    <div class="instructions">
      <div class="title closed">Instructions</div>
      <div class="body">
        <xsl:apply-templates select="a:* except (a:stylesheet|a:variables|a:functions
                                                 |a:templates|a:params
                                                 |a:comments|a:comment)"/>
      </div>
    </div>

    <xsl:if test=".//a:comment except .//a:stylesheet//a:comment">
      <div class="instructions">
        <div class="title closed">FIXME: comments</div>
        <div class="body">
          <xsl:apply-templates select=".//a:comment except .//a:stylesheet//a:comment"/>
        </div>
      </div>
    </xsl:if>

    <xsl:if test="string($source-listings) = ('1','true','yes')">
      <div class="source-code">
        <div class="title closed">Source code</div>
        <div class="body">
          <div>
            <xsl:try>
              <xsl:variable name="code" select="unparsed-text(@xml:base)"/>
              <table>
                <tr>
                  <td valign="top" align="right">
                    <pre class="fake-prism">
                      <xsl:for-each select="tokenize($code,'&#10;')">
                        <span id="line-{generate-id($stylesheet)}-{position()}">
                          <xsl:choose>
                            <xsl:when test="position() eq 1 or (position() mod 5 = 0)">
                              <xsl:sequence select="position()"/>
                            </xsl:when>
                            <xsl:otherwise>
                              <span class="lno">
                                <xsl:sequence select="position()"/>
                              </span>
                            </xsl:otherwise>
                          </xsl:choose>
                        </span>
                        <xsl:text>&#10;</xsl:text>
                      </xsl:for-each>
                    </pre>
                  </td>
                  <td valign="top">
                    <pre>
                      <code class="language-xml">
                        <xsl:sequence select="$code"/>
                      </code>
                    </pre>
                  </td>
                </tr>
              </table>
              <xsl:catch>
                <xsl:message select="'Failed to load source:', @xml:base"/>
                <xsl:sequence select="()"/>
              </xsl:catch>
            </xsl:try>
          </div>
        </div>
      </div>
    </xsl:if>
  </div>
</xsl:template>

<xsl:template match="a:function">
  <div id="{@id}"
       class="instruction {@class}">
    <div class="title">
      <xsl:sequence select="f:line-link(.)"/>
      <xsl:text>Function </xsl:text>
      <xsl:choose>
        <xsl:when test="count(a:no-arity) gt 1">
          <xsl:sequence select="@display-name/string()"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:sequence select="substring-before(@display-name/string(), '#')"/>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>(</xsl:text>
      <xsl:for-each select="a:param">
        <xsl:if test="position() gt 1">, </xsl:if>
        <xsl:text>$</xsl:text>
        <xsl:value-of select="@name/string()"/>
        <xsl:if test="@as">
          <xsl:text> as </xsl:text>
          <xsl:value-of select="@as/string()"/>
        </xsl:if>
      </xsl:for-each>
      <xsl:text>)</xsl:text>

      <xsl:if test="@as">
        <xsl:sequence select="' as ' || @as"/>
      </xsl:if>

      <xsl:if test="@recursive = 'true'">
        <xsl:text> </xsl:text>
        <span class="recursive" title="Recursive function">♻</span>
      </xsl:if>
    </div>

    <div class="props">
      <xsl:if test="a:shadow">
        <div class="shadows">
          <span class="marker">⚠</span>
          <xsl:text>Shadows: </xsl:text>
          <xsl:apply-templates select="key('id', a:shadow)" mode="m:reference"/>
        </div>
      </xsl:if>

      <xsl:call-template name="t:variables-referenced"/>
      <xsl:call-template name="t:functions-called"/>
      <xsl:call-template name="t:templates-called"/>

      <div class="used-by">
        <xsl:choose>
          <xsl:when test="a:used-by">
            <span class="marker">
              <xsl:if test="count(a:used-by) = 1 and a:used-by/string() = @id/string()">
                <span class="marker">☞</span>
              </xsl:if>
            </span>
            <xsl:text>Used by: </xsl:text>
            <xsl:sequence select="f:reference-list(a:used-by)"/>
          </xsl:when>
          <xsl:otherwise>
            <span class="marker">☞</span>
            <span class="unused">Unused</span>
          </xsl:otherwise>
        </xsl:choose>
      </div>

      <xsl:if test="a:used-by-module">
        <div class="used-in">
          <span class="marker">
            <xsl:if test="@elsewhere = 'true'">
              <xsl:attribute name="title" select="'Only used in one other module'"/>
              <xsl:text>☝</xsl:text>
            </xsl:if>
          </span>
          <xsl:text>Used in: </xsl:text>
          <xsl:sequence select="f:reference-list(a:used-by-module)"/>
        </div>
      </xsl:if>
    </div>
  </div>
</xsl:template>

<xsl:template match="a:variable">
  <div id="{@id}"
       class="instruction {@class}">
    <div class="title">
      <xsl:sequence select="f:line-link(.)"/>
      <xsl:sequence
          select="if (@type = 'param') then 'Param ' else 'Variable '"/>
      <code>
        <xsl:sequence select="'$'||@display-name/string()"/>
      </code>
      <xsl:if test="@as">
        <xsl:text> as </xsl:text>
        <code>
          <xsl:sequence select="@as/string()"/>
        </code>
      </xsl:if>
      <xsl:if test="@static = 'yes'">
        <xsl:text> [static]</xsl:text>
      </xsl:if>
    </div>

    <div class="props">
      <xsl:if test="a:shadow">
        <div class="shadows">
          <span class="marker">⚠</span>
          <xsl:text>Shadows: </xsl:text>
          <xsl:apply-templates select="key('id', a:shadow)" mode="m:reference"/>
        </div>
      </xsl:if>

      <xsl:call-template name="t:variables-referenced"/>
      <xsl:call-template name="t:functions-called"/>
      <xsl:call-template name="t:templates-called"/>

      <div class="used-by">
        <xsl:choose>
          <xsl:when test="a:used-by">
            <span class="marker"></span>
            <xsl:text>Used by: </xsl:text>
            <xsl:sequence select="f:reference-list(a:used-by)"/>
          </xsl:when>
          <xsl:otherwise>
            <span class="marker">☞</span>
            <span class="unused">Unused</span>
          </xsl:otherwise>
        </xsl:choose>
      </div>

      <xsl:if test="a:used-by-module">
        <div class="used-in">
          <span class="marker">
            <xsl:if test="@elsewhere = 'true'">
              <xsl:attribute name="title" select="'Only used in one other module'"/>
              <xsl:text>☝</xsl:text>
            </xsl:if>
          </span>
          <xsl:text>Used in: </xsl:text>
          <xsl:sequence select="f:reference-list(a:used-by-module)"/>
        </div>
      </xsl:if>
    </div>
  </div>
</xsl:template>

<xsl:template match="a:template">
  <div id="{@id}"
       class="instruction {@class}">
    <div class="title">
      <xsl:sequence select="f:line-link(.)"/>
      <xsl:text>Template</xsl:text>

      <xsl:if test="@name">
        <xsl:sequence select="' ' || @display-name/string()"/>
      </xsl:if>
      
      <xsl:choose>
        <xsl:when test="@match and string-length(@match) gt 30">
          <xsl:sequence select="' match ≅ ' || substring(@match, 1, 30) || '…'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:sequence select="' match ≅ ' || @match/string()"/>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:if test="@as">
        <xsl:sequence select="' as ' || @as"/>
      </xsl:if>

      <xsl:if test="@recursive = 'true'">
        <xsl:text> </xsl:text>
        <span class="recursive" title="Recursive template">♻</span>
      </xsl:if>
    </div>

    <div class="props">
      <xsl:if test="a:shadow">
        <div class="shadows">
          <span class="marker">⚠</span>
          <xsl:text>Shadows: </xsl:text>
          <xsl:apply-templates select="key('id', a:shadow)" mode="m:reference"/>
        </div>
      </xsl:if>

      <xsl:call-template name="t:variables-referenced"/>
      <xsl:call-template name="t:functions-called"/>
      <xsl:call-template name="t:templates-called"/>

      <xsl:if test="@name">
        <div class="used-by">
          <xsl:choose>
            <xsl:when test="a:used-by">
              <span class="marker">
                <xsl:if test="count(a:used-by) = 1 and a:used-by/string() = @id/string()">
                  <span class="marker">☞</span>
                </xsl:if>
              </span>
              <xsl:text>Used by: </xsl:text>
              <xsl:sequence select="f:reference-list(a:used-by)"/>
            </xsl:when>
            <xsl:otherwise>
              <span class="marker">☞</span>
              <span class="unused">
                <xsl:text>Unused</xsl:text>
                <xsl:if test="@match">
                  <xsl:text> (by name)</xsl:text>
                </xsl:if>
              </span>
            </xsl:otherwise>
          </xsl:choose>
        </div>
      </xsl:if>

      <xsl:if test="a:used-by-module">
        <div class="used-in">
          <span class="marker">
            <xsl:if test="@elsewhere = 'true'">
              <xsl:attribute name="title" select="'Only used in one other module'"/>
              <xsl:text>☝</xsl:text>
            </xsl:if>
          </span>
          <xsl:text>Used in: </xsl:text>
          <xsl:sequence select="f:reference-list(a:used-by-module)"/>
        </div>
      </xsl:if>

      <xsl:if test="@mode">
        <div class="mode">
          <xsl:text>Mode: </xsl:text>
          <code>
            <xsl:value-of select="@display-mode/string()"/>
          </code>
        </div>
      </xsl:if>

      <xsl:if test="@priority">
        <div class="priority">
          <xsl:text>Priority: </xsl:text>
          <code>
            <xsl:value-of select="@priority/string()"/>
          </code>
        </div>
      </xsl:if>

      <xsl:if test="@match">
        <div class="matches">
          <xsl:text>Matches: </xsl:text>
          <xsl:for-each select="tokenize(@match,'\|')">
            <xsl:if test="position() gt 1">, </xsl:if>
            <code>
              <xsl:value-of select="."/>
            </code>
          </xsl:for-each>
        </div>
      </xsl:if>
    </div>
  </div>
</xsl:template>

<xsl:template match="a:comment">
  <div id="{@id}"
       class="instruction {@class}">
    <div class="title">
      <xsl:sequence select="f:line-link(.)"/>
      <xsl:text>Comment</xsl:text>
    </div>

    <div class="props">
      <xsl:sequence select="string(.)"/>
    </div>
  </div>
</xsl:template>

<!-- ============================================================ -->

<xsl:template name="t:variables-referenced">
  <xsl:if test="a:variable">
    <div class="uses">
      <xsl:text>Uses: </xsl:text>
      <xsl:for-each select="a:variable ! key('id', .)">
        <xsl:sort select="@name"/>
        <xsl:if test="position() gt 1">, </xsl:if>
        <xsl:apply-templates select="." mode="m:reference"/>
      </xsl:for-each>
    </div>
  </xsl:if>
</xsl:template>

<xsl:template name="t:functions-called">
  <xsl:if test="a:function">
    <div class="calls">
      <xsl:text>Calls: </xsl:text>
      <xsl:for-each select="a:function ! key('id', .)">
        <xsl:sort select="@name"/>
        <xsl:if test="position() gt 1">, </xsl:if>
        <xsl:apply-templates select="." mode="m:reference"/>
      </xsl:for-each>
    </div>
  </xsl:if>
</xsl:template>

<xsl:template name="t:templates-called">
  <xsl:if test="a:template">
    <div class="calls">
      <xsl:text>Calls: </xsl:text>
      <xsl:for-each select="a:template ! key('id', .)">
        <xsl:sort select="@name"/>
        <xsl:if test="position() gt 1">, </xsl:if>
        <xsl:apply-templates select="." mode="m:reference"/>
      </xsl:for-each>
    </div>
  </xsl:if>
</xsl:template>

<!-- ============================================================ -->

<xsl:function name="f:reference-list">
  <xsl:param name="refs" as="element()+"/>

  <xsl:for-each select="$refs ! key('id', .)">
    <xsl:sort select="@name"/>
    <xsl:if test="position() gt 1">, </xsl:if>
    <xsl:apply-templates select="." mode="m:reference"/>
  </xsl:for-each>
</xsl:function>

<xsl:template match="*" mode="m:reference">
  <a href="#{@id}" class="error">
    <xsl:text>???</xsl:text>
    <xsl:sequence select="local-name(.)"/>
  </a>
</xsl:template>

<xsl:template match="a:stylesheet" mode="m:reference">
  <a href="#{@id}">
    <xsl:sequence select="if (@href)
                          then string(@href)
                          else '«root»'"/>
  </a>
</xsl:template>

<xsl:template match="a:variable" mode="m:reference">
  <a href="#{@id}">
    <xsl:sequence select="'$'||@display-name/string()"/>
  </a>
</xsl:template>

<xsl:template match="a:function" mode="m:reference">
  <xsl:variable name="fdefs"
                select="key('fnoarity', @noarity-id)"/>

  <a href="#{@id}">
    <xsl:choose>
      <xsl:when test="count($fdefs) gt 1">
        <xsl:sequence select="@display-name/string()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="substring-before(@display-name/string(), '#')"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>()</xsl:text>
  </a>
</xsl:template>

<xsl:template match="a:template" mode="m:reference">
  <a href="#{@id}">
    <xsl:choose>
      <xsl:when test="@name">
        <xsl:sequence select="@display-name/string()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>template</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </a>
</xsl:template>

<!-- ============================================================ -->

<xsl:template match="a:stylesheet" mode="m:toc">
  <xsl:if test="a:stylesheet">
    <ul>
      <xsl:for-each select="a:stylesheet">
        <li>
          <a href="#{@id}">
            <xsl:sequence select="@href/string()"/>
          </a>
          <xsl:apply-templates select="." mode="m:toc"/>
        </li>
      </xsl:for-each>
    </ul>
  </xsl:if>
</xsl:template>

<!-- ============================================================ -->
<xsl:function name="f:line-link" as="element(h:span)?">
  <xsl:param name="node" as="element()"/>

  <span class="link">
    <xsl:if test="string($source-listings) = ('1','true','yes')
                  and $node/@line-number">
      <xsl:attribute name="title" select="'Line ' || $node/@line-number/string()"/>
      <a href="#line-{generate-id($node/ancestor::a:stylesheet[1])}-{$node/@line-number}"
         class="goto-lno">
        <xsl:text>§</xsl:text>
      </a>
    </xsl:if>
  </span>
</xsl:function>

<xsl:function name="f:summary-count" as="element(h:span)?">
  <xsl:param name="count" as="xs:string"/>
  <xsl:param name="label" as="xs:string"/>

  <xsl:if test="$count ne '0'">
    <span class="{$label} all">
      <xsl:choose>
        <xsl:when test="$count = '1'">
          <xsl:sequence select="$count || ' ' || $label"/>
        </xsl:when>
        <xsl:when test="not($count castable as xs:integer)">
          <xsl:sequence select="$count || ' ' || $label"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:sequence select="$count || ' ' || $label || 's'"/>
        </xsl:otherwise>
      </xsl:choose>
    </span>
  </xsl:if>
</xsl:function>

<xsl:function name="f:summary-details">
  <xsl:param name="detail" as="element()"/>

  <!-- hack -->
  <xsl:variable name="class" select="local-name($detail)"/>
  <xsl:variable name="class" select="substring($class, 1, string-length($class) - 1)"/>

  <xsl:variable name="detail" as="element(h:span)*">
    <xsl:if test="$detail/a:unused">
      <span class="{$class} unused">
        <xsl:sequence select="count($detail/a:unused) || ' unused'"/>
      </span>
    </xsl:if>
    <xsl:if test="$detail/a:shadows">
      <span class="{$class} shadows">
        <xsl:sequence select="count($detail/a:shadows) || ' shadow'"/>
      </span>
    </xsl:if>
    <xsl:if test="$detail/a:elsewhere">
      <span class="{$class} elsewhere">
        <xsl:sequence select="count($detail/a:elsewhere) || ' used only in one other module'"/>
      </span>
    </xsl:if>
  </xsl:variable>

  <xsl:if test="exists($detail)">
    <xsl:text> (</xsl:text>
    <xsl:for-each select="$detail">
      <xsl:if test="position() gt 1">, </xsl:if>
      <xsl:sequence select="."/>
    </xsl:for-each>
    <xsl:text>)</xsl:text>
  </xsl:if>
</xsl:function>

</xsl:stylesheet>

4 templates, 6 params (1 used only in one other module)

Instructions
§Param $source-listings
Used in: summarize.xsl
§Param $debug-parse [static]
Used by: template
Used in: «root»
§Param $debug-resolve [static]
Used by: template
Used in: «root»
§Param $debug-analyze [static]
Used by: template
Used in: «root»
§Param $format
Used by: template
Used in: «root»
§Param $xspec-tests
§Template match ≅ /
§Template match ≅ element()
Mode: m:fix-base-uri
Matches: element()
§Template match ≅ @xml:base
Mode: m:fix-base-uri
Matches: @xml:base
§Template match ≅ attribute()|comment()|processi…
Mode: m:fix-base-uri
Matches: attribute(), comment(), processing-instruction(), text()
Source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:m="http://nwalsh.com/ns/xslt/modes"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns="http://www.w3.org/1999/xhtml"
                exclude-result-prefixes="m xs"
                version="3.0">

<xsl:import href="VERSION.xsl"/>
<xsl:import href="parse.xsl"/>
<xsl:import href="resolve.xsl"/>
<xsl:import href="analyze.xsl"/>
<xsl:import href="summarize.xsl"/>

<xsl:output method="xhtml" encoding="utf-8" indent="no"
            omit-xml-declaration="yes"/>

<xsl:param name="source-listings" select="'true'"/>

<xsl:param name="debug-parse" select="()" static="yes"/>
<xsl:param name="debug-resolve" select="()" static="yes"/>
<xsl:param name="debug-analyze" select="()" static="yes"/>
<xsl:param name="format" select="'visual'"/>
<xsl:param name="xspec-tests" select="'false'"/>

<xsl:template match="/">
  <xsl:variable name="parsed">
    <xsl:apply-templates select="/" mode="m:parse"/>
  </xsl:variable>

  <xsl:result-document use-when="exists($debug-parse)"
                       href="{$debug-parse}" method="xml" indent="yes">
    <xsl:sequence select="$parsed"/>
  </xsl:result-document>

  <xsl:variable name="resolved">
    <xsl:apply-templates select="$parsed" mode="m:resolve"/>
  </xsl:variable>

  <xsl:result-document use-when="exists($debug-resolve)"
                       href="{$debug-resolve}" method="xml" indent="yes">
    <xsl:sequence select="$resolved"/>
  </xsl:result-document>

  <xsl:variable name="analyzed">
    <xsl:apply-templates select="$resolved" mode="m:analyze"/>
  </xsl:variable>

  <xsl:result-document use-when="exists($debug-analyze)"
                       href="{$debug-analyze}" method="xml" indent="yes">
    <xsl:sequence select="$analyzed"/>
  </xsl:result-document>

  <xsl:choose>
    <xsl:when test="$format = 'data'">
      <xsl:choose>
        <xsl:when test="$xspec-tests = ('true','yes','1')">
          <xsl:apply-templates select="$analyzed" mode="m:fix-base-uri"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:sequence select="$analyzed"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="$analyzed" mode="m:summarize"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- ============================================================ -->

<xsl:template match="element()" mode="m:fix-base-uri">
  <xsl:copy>
    <xsl:apply-templates select="@*,node()" mode="m:fix-base-uri"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@xml:base" mode="m:fix-base-uri">
  <xsl:attribute name="xml:base" select="'...'"/>
</xsl:template>

<xsl:template match="attribute()|text()|comment()|processing-instruction()"
              mode="m:fix-base-uri">
  <xsl:copy/>
</xsl:template>

</xsl:stylesheet>