Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / tei / odds2 / RngToRnc.xsl @ 187

History | View | Annotate | Download (58.2 kB)

1
<?xml version="1.0" encoding="utf-8"?>
2
<!--*- XML -*-->
3
<!-- adapted by Sebastian Rahtz from: -->
4
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
5
<!-- $Id: RngToRnc.xsl 9513 2011-10-17 08:55:51Z rahtz $ -->
6
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
7
<!-- 
8
  modified 2010-08-14 by Syd Bauman:
9
     bug-fix: modified expressions defining 'pfx' variables
10
     so that only 1 namespace is handed to name().
11
-->
12
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13

    
14
     Copyright (c) 2002, Pantor Engineering AB
15
     All rights reserved.
16
     
17
     Redistribution and use in source and binary forms, with or
18
     without modification, are permitted provided that the following
19
     conditions are met:
20
     
21
     * Redistributions of source code must retain the above copyright
22
       notice, this list of conditions and the following disclaimer. 
23

    
24
     * Redistributions in binary form must reproduce the above
25
       copyright notice, this list of conditions and the following
26
       disclaimer in the documentation and/or other materials provided
27
       with the distribution.
28

    
29
     * Neither the name of Pantor Engineering AB nor the names of its
30
       contributors may be used to endorse or promote products derived
31
       from this software without specific prior written permission.
32
     
33
     THIS   SOFTWARE   IS PROVIDED BY    THE   COPYRIGHT  HOLDERS  AND
34
     CONTRIBUTORS "AS IS"   AND ANY  EXPRESS OR  IMPLIED   WARRANTIES,
35
     INCLUDING,  BUT  NOT LIMITED  TO,   THE  IMPLIED  WARRANTIES   OF
36
     MERCHANTABILITY    AND  FITNESS  FOR   A  PARTICULAR  PURPOSE ARE
37
     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
38
     BE  LIABLE   FOR ANY    DIRECT, INDIRECT,   INCIDENTAL,  SPECIAL,
39
     EXEMPLARY, OR CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED
40
     TO, PROCUREMENT  OF  SUBSTITUTE GOODS OR  SERVICES;  LOSS OF USE,
41
     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42
     ANY THEORY OF  LIABILITY, WHETHER IN CONTRACT,  STRICT LIABILITY,
43
     OR  TORT (INCLUDING NEGLIGENCE OR  OTHERWISE) ARISING  IN ANY WAY
44
     OUT OF  THE  USE OF   THIS  SOFTWARE,  EVEN IF ADVISED   OF   THE
45
     POSSIBILITY OF SUCH DAMAGE.
46

    
47
     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
48
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
49
     Created by David.Rosenborg@pantor.com
50
     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
51
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52

    
53
     RngToRncText.xsl converts a RELAX NG schema in XML syntax to the
54
     compact syntax.
55

    
56
     If the source document is a RELAX NG schema, an extension
57
     function converting result tree fragments to node sets is
58
     required. If the processor supports the exslt:node-set function,
59
     no configuration should be needed. See RngToRncProcessorConfig.xsl
60
     for further information.
61

    
62
     This stylesheet can also be applied to the intermediate XML
63
     representation of a compact schema as output by the
64
     RngToRncXml.xsl stylesheet. In this case, no extension function
65
     is required.
66

    
67
     For a description of the underlying XML to compact syntax
68
     transformation, see RngToRncXml.xsl.
69

    
70
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
71
<xsl:transform xmlns:rng="http://relaxng.org/ns/structure/1.0"
72
               xmlns:sch="http://purl.oclc.org/dsdl/schematron"
73
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
74
               xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
75
               version="2.0"
76
               exclude-result-prefixes="rng sch a">
77
  <xsl:param name="top"/>
78
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
79
<!-- Parameters -->
80
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
81
<!-- collapse-lines:
82
     If true, output constructs spanning multiple lines will be
83
     groupd into a single line unless it exceeds $line-width chars. -->
84
  <xsl:param name="collapse-lines" select="true ()"/>
85
   <!-- indent-width:
86
     The number of characters to indent at each indentation level -->
87
  <xsl:param name="indent-width" select="3"/>
88
   <!-- line-width:
89
     see the group-lines parameter. -->
90
  <xsl:param name="line-width" select="80"/>
91
   <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
92
<!-- $Id: RngToRnc.xsl 9513 2011-10-17 08:55:51Z rahtz $ -->
93
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
94
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
95

    
96
     Copyright (c) 2002, Pantor Engineering AB
97
     All rights reserved.
98
     
99
     Redistribution and use in source and binary forms, with or
100
     without modification, are permitted provided that the following
101
     conditions are met:
102
     
103
     * Redistributions of source code must retain the above copyright
104
       notice, this list of conditions and the following disclaimer. 
105

    
106
     * Redistributions in binary form must reproduce the above
107
       copyright notice, this list of conditions and the following
108
       disclaimer in the documentation and/or other materials provided
109
       with the distribution.
110

    
111
     * Neither the name of Pantor Engineering AB nor the names of its
112
       contributors may be used to endorse or promote products derived
113
       from this software without specific prior written permission.
114
     
115
     THIS   SOFTWARE   IS PROVIDED BY    THE   COPYRIGHT  HOLDERS  AND
116
     CONTRIBUTORS "AS IS"   AND ANY  EXPRESS OR  IMPLIED   WARRANTIES,
117
     INCLUDING,  BUT  NOT LIMITED  TO,   THE  IMPLIED  WARRANTIES   OF
118
     MERCHANTABILITY    AND  FITNESS  FOR   A  PARTICULAR  PURPOSE ARE
119
     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
120
     BE  LIABLE   FOR ANY    DIRECT, INDIRECT,   INCIDENTAL,  SPECIAL,
121
     EXEMPLARY, OR CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED
122
     TO, PROCUREMENT  OF  SUBSTITUTE GOODS OR  SERVICES;  LOSS OF USE,
123
     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
124
     ANY THEORY OF  LIABILITY, WHETHER IN CONTRACT,  STRICT LIABILITY,
125
     OR  TORT (INCLUDING NEGLIGENCE OR  OTHERWISE) ARISING  IN ANY WAY
126
     OUT OF  THE  USE OF   THIS  SOFTWARE,  EVEN IF ADVISED   OF   THE
127
     POSSIBILITY OF SUCH DAMAGE.
128

    
129
     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
130
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
131
     Created by David.Rosenborg@pantor.com
132
     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
133
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
134

    
135
     RngToRncXml.xsl converts a RELAX NG schema in XML syntax to an
136
     XML representation of the compact syntax.
137

    
138
     The output is an intermediate format suitable for further
139
     transformation into, for example, plain text or HTML.
140

    
141
     The default namespace declaration is chosen from the first
142
     (in document order) occurrence of an ns attribute, unless
143
     supplied as the parameter default-ns.
144

    
145
     If the value of an href attribute ends with '.rng', the suffix
146
     will be replace by the string '.rnc'. This feature can be
147
     disabled by setting the stylesheet parameter rewrite-suffix to
148
     false.
149

    
150
     Current limitations/known deficiencies:
151

    
152
     * Annotations are not handled in name classes
153

    
154
     * Uses of namespace prefixes must be consistent throughout the
155
       source schema. That is, a namespace prefix used in two QNames
156
       must be declared to the same namespace URI for both
157
       occurrences.
158

    
159
     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
160
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
161
<!-- Parameters -->
162
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
163
<!-- rewrite-suffix:
164
     If true and the value of an href attribute ends with '.rng', the
165
     suffix will be replace by the string '.rnc'. -->
166
  <xsl:param name="rewrite-suffix" select="true ()"/>
167
   <!-- retain-prefixes:
168
     If true, namespace declarations will use prefixes from the source
169
     document if possible. (Doesn't work with MSXML 4.0 or Xalan-J 2.4.0) -->
170
  <xsl:param name="retain-prefixes"
171
              select="system-property ('xsl:vendor') != 'Microsoft' and     not (starts-with (system-property ('xsl:vendor'), 'Apache'))"/>
172
   <!-- default-ns:
173
     The uri of the default namespace. There must be at least one
174
     ns attribute in the schema containing this uri. -->
175
  <xsl:param name="default-ns" select="string (/descendant::rng:*[@ns][1]/@ns)"/>
176
   <!-- prefixes:
177
    A space separated list of prefix mappings: prefix '=' namespace-uri.
178
    Note: Since space is used as a delimiter, no space is allowed 
179
    immediately before or after the equal sign. Example: 
180

    
181
    "foo=http://example.org/ns/foo bar=http://example.org/ns/bar"
182

    
183
    The prefixes will be used whenever a prefix is needed for the
184
    corresponding namespace URI. Prefixes declared in this
185
    parameter has higher precedence than the prefixes declared
186
    in the prefix-map parameter and those found in the schema.
187
  -->
188
  <xsl:param name="prefixes"/>
189
   <!-- prefix-map:
190
     A node set containing element nodes like:
191

    
192
     <ns prefix="x">http://example.org/x</ns>
193

    
194
     The name of the element is not significant. Prefixes from this
195
     map will be used in favor of generated and the ones found in the
196
     source schema. A prefix must not be the empty string. Also, it
197
     must not conflict with any other prefixes in the map or in the
198
     schema. -->
199
  <xsl:param name="prefix-map" select="/.."/>
200
   <!-- recursive:
201
     If true, recursively process includes and external
202
     references. Note: no include logic is applied. The external
203
     schemas are simply structurally included, wrapped in an <external>
204
     element. -->
205
  <xsl:param name="recursive" select="false ()"/>
206
  <xsl:template name="str">
207
      <xsl:param name="text"/>
208
      <xsl:param name="quote" select="'&#34;'"/>
209
      <xsl:param name="is-multiline" select="false ()"/>
210
      <xsl:variable name="lit" select="concat ($quote, $text, $quote)"/>
211
      <str size="{string-length ($lit)}">
212
         <xsl:if test="$is-multiline">
213
            <xsl:attribute name="multiline">yes</xsl:attribute>
214
         </xsl:if>
215
         <xsl:value-of select="$lit"/>
216
      </str>
217
  </xsl:template>
218
  <xsl:template name="text">
219
      <xsl:param name="text"/>
220
      <xsl:attribute name="size">
221
         <xsl:value-of select="string-length ($text)"/>
222
      </xsl:attribute>
223
      <xsl:value-of select="$text"/>
224
  </xsl:template>
225
   <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
226
  <xsl:key name="prefix"
227
            match="rng:element/@name [contains (., ':')] | rng:attribute/@name [contains (., ':')]"
228
            use="substring-before (., ':')"/>
229
  <xsl:key name="ns" match="*" use="namespace::*"/>
230
  <xsl:key name="annot-ns" match="*[not (self::rng:*)]" use="namespace-uri (.)"/>
231
  <xsl:key name="annot-ns" match="@*[namespace-uri (.) != '']" use="namespace-uri (.)"/>
232
  <xsl:output method="xml" indent="yes"/>
233
  <xsl:strip-space elements="rng:*"/>
234
  <xsl:preserve-space elements="rng:value rng:param"/>
235
  <xsl:variable name="xsd" select="'http://www.w3.org/2001/XMLSchema-datatypes'"/>
236
  <xsl:variable name="default-ns-nd" select="/descendant::rng:*[@ns = $default-ns][1]/@ns"/>
237
  <xsl:variable name="default-ns-id-rtf">
238
      <xsl:call-template name="get-prefix">
239
         <xsl:with-param name="nd" select="$default-ns-nd"/>
240
      </xsl:call-template>
241
  </xsl:variable>
242
  <xsl:variable name="default-ns-id" select="string ($default-ns-id-rtf)"/>
243
  <xsl:variable name="has-default-ns" select="boolean ($default-ns-nd)"/>
244
  <xsl:variable name="has-local" select="not (key ('prefix', 'local'))"/>
245
  <xsl:template name="make-compact-schema">
246
      <compact-schema>
247
<!-- Declarations -->
248
      <xsl:if test="$has-default-ns">
249
            <xsl:call-template name="make-ns-declaration">
250
               <xsl:with-param name="is-default" select="true ()"/>
251
               <xsl:with-param name="prefix" select="$default-ns-id"/>
252
               <xsl:with-param name="uri" select="$default-ns"/>
253
            </xsl:call-template>
254
         </xsl:if>
255
         <xsl:if test="$has-local">
256
            <xsl:call-template name="make-ns-declaration">
257
               <xsl:with-param name="prefix" select="'local'"/>
258
               <xsl:with-param name="uri" select="''"/>
259
            </xsl:call-template>
260
         </xsl:if>
261
         <xsl:call-template name="inhnamespace"/>
262
         <!--
263
      <xsl:choose>
264
	<xsl:when test="$retain-prefixes">
265
	  <xsl:for-each select="//*">
266
	    <xsl:variable name="elm" select="self::*"/>
267
	    <xsl:for-each select="namespace::*">
268
	      <xsl:variable name="ns" select="string (.)"/>
269
	      <xsl:if test="(not ($has-default-ns) or $ns != $default-ns) and
270
		count (key ('ns', $ns)[1] | $elm) = 1 and
271
		name (.) != 'xml' and 
272
		((not ($ns = 'http://relaxng.org/ns/structure/1.0') and 
273
		//*[namespace-uri (.) = $ns or @ns = $ns or 
274
		@*[namespace-uri (.) = $ns]]) or 
275
		//*[namespace-uri (.) != 
276
		'http://relaxng.org/ns/structure/1.0']//
277
		*[namespace-uri (.) = $ns or @*[namespace-uri (.) = $ns]] or
278
		key ('prefix', name (.)))">
279
		<xsl:call-template name="make-ns-declaration">
280
		  <xsl:with-param name="prefix">
281
		    <xsl:variable name="mapped">
282
		      <xsl:call-template name="mapped-prefix">
283
			<xsl:with-param name="ns" select="string (.)"/>
284
		      </xsl:call-template>
285
		    </xsl:variable>
286
		    <xsl:choose>
287
		      <xsl:when test="string ($mapped)">
288
			<xsl:value-of select="$mapped"/>
289
		      </xsl:when>
290
		      <xsl:when test="name (.)">
291
			<xsl:value-of select="name (.)"/>
292
		      </xsl:when>
293
		      <xsl:otherwise>
294
			<xsl:value-of select="generate-id ($elm)"/>
295
		      </xsl:otherwise>
296
		    </xsl:choose>
297
		  </xsl:with-param>
298
		  <xsl:with-param name="uri" select="."/>
299
		</xsl:call-template>
300
	      </xsl:if>
301
	    </xsl:for-each>
302
	  </xsl:for-each>
303
	</xsl:when>
304

    
305
	<xsl:otherwise>
306
	  <xsl:for-each select=".//rng:element | .//rng:attribute">
307
	    <xsl:if test="@name [contains (., ':')]">
308
	      <xsl:variable name="pfx" select="substring-before (@name, ':')"/>
309
	      <xsl:if test="count (key ('prefix', $pfx)[1] | @name) = 1">
310
		<xsl:variable name="ns"
311
		  select="string (namespace::*[name () = $pfx])"/>
312
		<xsl:call-template name="make-ns-declaration">
313
		  <xsl:with-param name="prefix">
314
		    <xsl:variable name="mapped">
315
		      <xsl:call-template name="mapped-prefix">
316
			<xsl:with-param name="ns" select="$ns"/>
317
		      </xsl:call-template>
318
		    </xsl:variable>
319
		    <xsl:choose>
320
		      <xsl:when test="string ($mapped)">
321
			<xsl:value-of select="$mapped"/>
322
		      </xsl:when>
323
		      <xsl:otherwise>
324
			<xsl:value-of select="$pfx"/>
325
		      </xsl:otherwise>
326
		    </xsl:choose>
327
		  </xsl:with-param>
328
		  <xsl:with-param name="uri" select="$ns"/>
329
		</xsl:call-template>
330
	      </xsl:if>
331
	    </xsl:if>
332
	  </xsl:for-each>
333

    
334
	  <xsl:for-each select="//*[not (self::rng:*)] | 
335
	                        //*/@*[namespace-uri (.) != '']">
336
	    <xsl:variable name="ns" select="namespace-uri (.)"/>
337
	    <xsl:if test="count (key ('annot-ns', $ns)[1] | .) = 1">
338
	      <xsl:call-template name="make-ns-declaration">
339
		<xsl:with-param name="prefix">
340
		  <xsl:variable name="mapped">
341
		    <xsl:call-template name="mapped-prefix">
342
		      <xsl:with-param name="ns" select="$ns"/>
343
		    </xsl:call-template>
344
		  </xsl:variable>
345
		  <xsl:choose>
346
		    <xsl:when test="string ($mapped)">
347
		      <xsl:value-of select="$mapped"/>
348
		    </xsl:when>
349
		    <xsl:otherwise>
350
		      <xsl:value-of select="generate-id (.)"/>
351
		    </xsl:otherwise>
352
		  </xsl:choose>
353
		</xsl:with-param>
354
		<xsl:with-param name="uri" select="$ns"/>
355
	      </xsl:call-template>
356
	    </xsl:if>
357
	  </xsl:for-each>
358
	</xsl:otherwise>
359
      </xsl:choose>
360
      
361
      <xsl:for-each select="//@datatypeLibrary">
362
	<xsl:if test=". != $xsd and . != ''">
363
	  <declaration size="9">datatypes</declaration>
364
	  <sp size="1"/>
365
	  <prefix>
366
	    <xsl:call-template name="text">
367
	      <xsl:with-param name="text" select="generate-id (.)"/>
368
	    </xsl:call-template>
369
	  </prefix>
370
	  <t size="3"> = </t>
371
	  <xsl:call-template name="str">
372
	    <xsl:with-param name="text" select="."/>
373
	  </xsl:call-template>
374
	  <nl size="1"/>
375
	</xsl:if>
376
      </xsl:for-each>
377
-->
378
      <nl size="1"/>
379
         <!-- Pattern -->
380
      <xsl:apply-templates mode="RNC"/>
381
      </compact-schema>
382
  </xsl:template>
383
  <xsl:template match="/rng:grammar">
384
      <grammar>
385
         <xsl:apply-templates mode="RNC"/>
386
      </grammar>
387
  </xsl:template>
388
  <xsl:template name="make-block">
389
      <xsl:param name="head"/>
390
      <xsl:param name="body"/>
391
      <xsl:param name="collapse" select="true ()"/>
392
      <group>
393
         <xsl:if test="not ($collapse)">
394
            <xsl:attribute name="collapse">no</xsl:attribute>
395
         </xsl:if>
396
         <xsl:copy-of select="$head"/>
397
         <nl size="1"/>
398
         <t size="1">{</t>
399
         <indent>
400
            <nl size="1"/>
401
            <xsl:copy-of select="$body"/>
402
         </indent>
403
         <nl size="1"/>
404
         <t size="1">}</t>
405
      </group>
406
  </xsl:template>
407
  <xsl:template name="make-parenthesis">
408
      <xsl:param name="body"/>
409
      <t size="1">(</t>
410
      <indent>
411
         <nl size="1"/>
412
         <xsl:copy-of select="$body"/>
413
      </indent>
414
      <nl size="1"/>
415
      <t size="1">)</t>
416
  </xsl:template>
417
  <xsl:template match="rng:grammar" mode="RNC">
418
      <grammar>
419
         <group>
420
            <xsl:call-template name="annotations"/>
421
            <xsl:call-template name="make-block">
422
               <xsl:with-param name="collapse" select="false ()"/>
423
               <xsl:with-param name="head">
424
                  <keyword size="7">grammar</keyword>
425
               </xsl:with-param>
426
               <xsl:with-param name="body">
427
                  <xsl:apply-templates mode="RNC"/>
428
               </xsl:with-param>
429
            </xsl:call-template>
430
         </group>
431
      </grammar>
432
  </xsl:template>
433
  <xsl:template match="rng:list | rng:mixed" mode="RNC">
434
      <group>
435
         <xsl:call-template name="annotations"/>
436
         <xsl:call-template name="make-block">
437
            <xsl:with-param name="head">
438
               <keyword>
439
                  <xsl:call-template name="text">
440
                     <xsl:with-param name="text" select="local-name (.)"/>
441
                  </xsl:call-template>
442
               </keyword>
443
            </xsl:with-param>
444
            <xsl:with-param name="body">
445
               <xsl:call-template name="group-body"/>
446
            </xsl:with-param>
447
         </xsl:call-template>
448
      </group>
449
  </xsl:template>
450
  <xsl:template match="rng:start" mode="RNC">
451
      <xsl:call-template name="annotations"/>
452
      <group>
453
         <define size="5">
454
            <xsl:copy-of select="@combine"/>
455
            <xsl:text>start</xsl:text>
456
         </define>
457
         <xsl:call-template name="assignment-op"/>
458
         <indent>
459
            <nl size="1"/>
460
            <xsl:call-template name="group-body"/>
461
         </indent>
462
      </group>
463
      <xsl:call-template name="spacer"/>
464
  </xsl:template>
465
  <xsl:template name="assignment-op">
466
      <xsl:choose>
467
         <xsl:when test="@combine = 'choice'">
468
            <t size="3"> |=</t>
469
         </xsl:when>
470
         <xsl:when test="@combine = 'interleave'">
471
            <t size="3"> &amp;=</t>
472
         </xsl:when>
473
         <xsl:otherwise>
474
            <t size="2"> =</t>
475
         </xsl:otherwise>
476
      </xsl:choose>
477
  </xsl:template>
478
  <xsl:template match="rng:define" mode="RNC">
479
      <xsl:variable name="grammar" select="ancestor::rng:grammar [1]"/>
480
      <xsl:call-template name="annotations"/>
481
      <group>
482
         <define name="{@name}">
483
            <xsl:copy-of select="@combine"/>
484
            <xsl:call-template name="text">
485
               <xsl:with-param name="text">
486
                  <xsl:call-template name="quote-keyword">
487
                     <xsl:with-param name="name" select="@name"/>
488
                  </xsl:call-template>
489
               </xsl:with-param>
490
            </xsl:call-template>
491
         </define>
492
         <xsl:call-template name="assignment-op"/>
493
         <indent>
494
            <nl size="1"/>
495
            <xsl:call-template name="group-body"/>
496
         </indent>
497
      </group>
498
      <xsl:call-template name="spacer"/>
499
  </xsl:template>
500
  <xsl:template match="rng:div" mode="RNC">
501
      <xsl:call-template name="annotations"/>
502
      <xsl:call-template name="make-block">
503
         <xsl:with-param name="collapse" select="false ()"/>
504
         <xsl:with-param name="head">
505
            <keyword size="3">div</keyword>
506
         </xsl:with-param>
507
         <xsl:with-param name="body">
508
            <xsl:apply-templates mode="RNC"/>
509
         </xsl:with-param>
510
      </xsl:call-template>
511
      <xsl:call-template name="spacer"/>
512
  </xsl:template>
513
  <xsl:template match="rng:include" mode="RNC">
514
      <xsl:call-template name="annotations"/>
515
      <group collapse="no">
516
         <keyword size="7">include</keyword>
517
         <sp size="1"/>
518
         <include-href ref="{generate-id ()}">
519
            <xsl:call-template name="external"/>
520
         </include-href>
521
         <xsl:if test="*">
522
            <nl size="1"/>
523
            <t size="1">{</t>
524
            <indent>
525
               <nl size="1"/>
526
               <xsl:apply-templates mode="RNC"/>
527
            </indent>
528
            <nl size="1"/>
529
            <t size="1">}</t>
530
         </xsl:if>
531
      </group>
532
      <xsl:call-template name="spacer"/>
533
      <xsl:if test="$recursive">
534
         <include href="{@href}" id="{generate-id ()}">
535
            <xsl:for-each select="document (@href)/rng:grammar">
536
               <xsl:call-template name="make-compact-schema"/>
537
            </xsl:for-each>
538
         </include>
539
      </xsl:if>
540
  </xsl:template>
541
  <xsl:template match="rng:externalRef" mode="RNC">
542
      <group>
543
         <xsl:call-template name="annotations"/>
544
         <keyword size="8">external</keyword>
545
         <sp size="1"/>
546
         <external-href ref="{generate-id ()}">
547
            <xsl:call-template name="external"/>
548
         </external-href>
549
      </group>
550
      <xsl:if test="$recursive">
551
         <external href="{@href}" id="{generate-id ()}">
552
            <xsl:for-each select="document (@href)">
553
               <xsl:call-template name="make-compact-schema"/>
554
            </xsl:for-each>
555
         </external>
556
      </xsl:if>
557
  </xsl:template>
558
  <xsl:template name="make-inherit">
559
      <xsl:param name="prefix"/>
560
      <sp size="1"/>
561
      <keyword size="7">inherit</keyword>
562
      <t size="3"> = </t>
563
      <prefix>
564
         <xsl:call-template name="text">
565
            <xsl:with-param name="text" select="$prefix"/>
566
         </xsl:call-template>
567
      </prefix>
568
  </xsl:template>
569
  <xsl:template name="rewrite-suffix">
570
      <xsl:param name="href" select="@href"/>
571
      <xsl:variable name="suffix" select="substring ($href, (string-length ($href) - 4) + 1, 4)"/>
572
      <xsl:choose>
573
         <xsl:when test="$rewrite-suffix and $suffix = '.rng'">
574
            <xsl:value-of select="substring ($href, 1, string-length ($href) - 4)"/>
575
            <xsl:text>.rnc</xsl:text>
576
         </xsl:when>
577
         <xsl:otherwise>
578
            <xsl:value-of select="$href"/>
579
         </xsl:otherwise>
580
      </xsl:choose>
581
  </xsl:template>
582
  <xsl:template name="external">
583
      <xsl:call-template name="str">
584
         <xsl:with-param name="text">
585
            <xsl:call-template name="rewrite-suffix"/>
586
         </xsl:with-param>
587
      </xsl:call-template>
588
      <xsl:variable name="ns" select="ancestor-or-self::*[@ns][1]/@ns"/>
589
      <xsl:choose>
590
         <xsl:when test="$has-default-ns and $ns = $default-ns"/>
591
         <xsl:when test="$has-local and $ns = ''">
592
            <xsl:call-template name="make-inherit">
593
               <xsl:with-param name="prefix">local</xsl:with-param>
594
            </xsl:call-template>
595
         </xsl:when>
596
         <xsl:when test="not($ns='')">
597
            <xsl:call-template name="make-inherit">
598
               <xsl:with-param name="prefix">
599
                  <xsl:call-template name="get-prefix">
600
                     <xsl:with-param name="nd" select="$ns"/>
601
                  </xsl:call-template>
602
               </xsl:with-param>
603
            </xsl:call-template>
604
         </xsl:when>
605
         <xsl:when test="$has-default-ns">
606
            <xsl:call-template name="make-inherit">
607
               <xsl:with-param name="prefix">inh</xsl:with-param>
608
            </xsl:call-template>
609
         </xsl:when>
610
      </xsl:choose>
611
  </xsl:template>
612
  <xsl:template match="rng:ref" mode="RNC">
613
      <group>
614
         <xsl:call-template name="annotations"/>
615
         <ref name="{@name}">
616
            <xsl:call-template name="text">
617
               <xsl:with-param name="text">
618
                  <xsl:call-template name="quote-keyword">
619
                     <xsl:with-param name="name" select="@name"/>
620
                  </xsl:call-template>
621
               </xsl:with-param>
622
            </xsl:call-template>
623
         </ref>
624
      </group>
625
  </xsl:template>
626
  <xsl:template match="rng:parentRef" mode="RNC">
627
      <group>
628
         <xsl:call-template name="annotations"/>
629
         <keyword size="6">parent</keyword>
630
         <sp size="1"/>
631
         <parent-ref name="{@name}">
632
            <xsl:call-template name="text">
633
               <xsl:with-param name="text">
634
                  <xsl:call-template name="quote-keyword">
635
                     <xsl:with-param name="name" select="@name"/>
636
                  </xsl:call-template>
637
               </xsl:with-param>
638
            </xsl:call-template>
639
         </parent-ref>
640
      </group>
641
  </xsl:template>
642
  <xsl:template match="rng:element | rng:attribute" mode="RNC">
643
      <group>
644
         <xsl:call-template name="annotations"/>
645
         <group>
646
            <xsl:variable name="head">
647
               <keyword>
648
                  <xsl:call-template name="text">
649
                     <xsl:with-param name="text" select="local-name (.)"/>
650
                  </xsl:call-template>
651
               </keyword>
652
               <sp size="1"/>
653
               <xsl:call-template name="name-class">
654
                  <xsl:with-param name="is-attr" select="boolean (self::rng:attribute)"/>
655
               </xsl:call-template>
656
            </xsl:variable>
657
            <xsl:choose>
658
               <xsl:when test="not (rng:*) or (count (rng:*) = 1 and not (@name))">
659
                  <xsl:copy-of select="$head"/>
660
                  <t size="3"> { </t>
661
                  <atom size="4">text</atom>
662
                  <t size="2"> }</t>
663
               </xsl:when>
664
               <xsl:otherwise>
665
                  <xsl:call-template name="make-block">
666
                     <xsl:with-param name="head">
667
                        <xsl:copy-of select="$head"/>
668
                     </xsl:with-param>
669
                     <xsl:with-param name="body">
670
                        <xsl:choose>
671
                           <xsl:when test="@name">
672
                              <xsl:call-template name="group-body"/>
673
                           </xsl:when>
674
                           <xsl:otherwise>
675
                              <xsl:call-template name="group-body">
676
                                 <xsl:with-param name="patterns" select="rng:*[1]/following-sibling::rng:*"/>
677
                              </xsl:call-template>
678
                           </xsl:otherwise>
679
                        </xsl:choose>
680
                     </xsl:with-param>
681
                  </xsl:call-template>
682
               </xsl:otherwise>
683
            </xsl:choose>
684
         </group>
685
      </group>
686
  </xsl:template>
687
  <xsl:template name="expression">
688
      <xsl:param name="operator"/>
689
      <xsl:param name="is-prefix-operator" select="false ()"/>
690
      <group>
691
         <xsl:call-template name="annotations"/>
692
         <group>
693
            <xsl:variable name="need-paren-rtf">
694
               <xsl:call-template name="need-paren"/>
695
            </xsl:variable>
696
            <xsl:variable name="need-paren" select="boolean (string ($need-paren-rtf))"/>
697
            <xsl:choose>
698
               <xsl:when test="$need-paren">
699
                  <xsl:call-template name="make-parenthesis">
700
                     <xsl:with-param name="body">
701
                        <xsl:call-template name="expression-body">
702
                           <xsl:with-param name="operator" select="$operator"/>
703
                           <xsl:with-param name="is-prefix-operator" select="$is-prefix-operator"/>
704
                        </xsl:call-template>
705
                     </xsl:with-param>
706
                  </xsl:call-template>
707
               </xsl:when>
708
               <xsl:otherwise>
709
                  <xsl:call-template name="expression-body">
710
                     <xsl:with-param name="operator" select="$operator"/>
711
                     <xsl:with-param name="is-prefix-operator" select="$is-prefix-operator"/>
712
                  </xsl:call-template>
713
               </xsl:otherwise>
714
            </xsl:choose>
715
         </group>
716
      </group>
717
  </xsl:template>
718
  <xsl:template match="rng:group" mode="RNC">
719
      <xsl:call-template name="expression">
720
         <xsl:with-param name="operator" select="','"/>
721
      </xsl:call-template>
722
  </xsl:template>
723
  <xsl:template match="rng:choice" mode="RNC" name="make-choice">
724
      <xsl:call-template name="expression">
725
         <xsl:with-param name="operator" select="'| '"/>
726
         <xsl:with-param name="is-prefix-operator" select="true ()"/>
727
      </xsl:call-template>
728
  </xsl:template>
729
  <xsl:template match="rng:interleave" mode="RNC">
730
      <xsl:call-template name="expression">
731
         <xsl:with-param name="operator" select="'&amp; '"/>
732
         <xsl:with-param name="is-prefix-operator" select="true ()"/>
733
      </xsl:call-template>
734
  </xsl:template>
735
  <xsl:template match="rng:optional" mode="RNC">
736
      <xsl:call-template name="expression">
737
         <xsl:with-param name="operator" select="','"/>
738
      </xsl:call-template>
739
      <t size="1">?</t>
740
  </xsl:template>
741
  <xsl:template match="rng:zeroOrMore" mode="RNC">
742
      <xsl:call-template name="expression">
743
         <xsl:with-param name="operator" select="','"/>
744
      </xsl:call-template>
745
      <t size="1">*</t>
746
  </xsl:template>
747
  <xsl:template match="rng:oneOrMore" mode="RNC">
748
      <xsl:call-template name="expression">
749
         <xsl:with-param name="operator" select="','"/>
750
      </xsl:call-template>
751
      <t size="1">+</t>
752
  </xsl:template>
753
  <xsl:template match="rng:text" mode="RNC">
754
      <group>
755
         <xsl:call-template name="annotations"/>
756
         <atom size="4">text</atom>
757
      </group>
758
  </xsl:template>
759
  <xsl:template match="rng:empty" mode="RNC">
760
      <group>
761
         <xsl:call-template name="annotations"/>
762
         <atom size="5">empty</atom>
763
      </group>
764
  </xsl:template>
765
  <xsl:template match="rng:notAllowed" mode="RNC">
766
      <group>
767
         <xsl:call-template name="annotations"/>
768
         <atom size="10">notAllowed</atom>
769
      </group>
770
  </xsl:template>
771
  <xsl:template match="rng:data" mode="RNC">
772
      <group>
773
         <xsl:call-template name="annotations"/>
774
         <group>
775
            <xsl:call-template name="type-name"/>
776
            <xsl:if test="rng:param">
777
               <nl size="1"/>
778
               <t size="1">{</t>
779
               <indent>
780
                  <xsl:for-each select="rng:param">
781
                     <nl size="1"/>
782
                     <group>
783
                        <xsl:call-template name="annotations"/>
784
                        <param>
785
                           <xsl:call-template name="text">
786
                              <xsl:with-param name="text" select="@name"/>
787
                           </xsl:call-template>
788
                        </param>
789
                        <t size="3"> = </t>
790
                        <xsl:call-template name="make-string-literal"/>
791
                     </group>
792
                  </xsl:for-each>
793
               </indent>
794
               <nl size="1"/>
795
               <t size="1">}</t>
796
            </xsl:if>
797
            <xsl:for-each select="rng:except">
798
               <nl size="1"/>
799
               <t size="1">-</t>
800
               <nl size="1"/>
801
               <xsl:choose>
802
                  <xsl:when test="count (rng:*) &gt; 1">
803
                     <xsl:call-template name="make-choice"/>
804
                  </xsl:when>
805
                  <xsl:otherwise>
806
                     <xsl:apply-templates select="*" mode="RNC"/>
807
                  </xsl:otherwise>
808
               </xsl:choose>
809
            </xsl:for-each>
810
         </group>
811
      </group>
812
  </xsl:template>
813
  <xsl:template match="rng:value" mode="RNC">
814
      <group>
815
         <xsl:call-template name="annotations"/>
816
         <xsl:variable name="type">
817
            <xsl:call-template name="type-name"/>
818
         </xsl:variable>
819
         <xsl:if test="$type != 'token'">
820
            <xsl:copy-of select="$type"/>
821
            <sp size="1"/>
822
         </xsl:if>
823
         <xsl:call-template name="make-string-literal"/>
824
      </group>
825
  </xsl:template>
826
  <xsl:template name="quote-quotes">
827
      <xsl:param name="str"/>
828
      <xsl:param name="pos" select="1"/>
829
      <xsl:variable name="c" select="substring ($str, $pos, 1)"/>
830
      <xsl:if test="$c">
831
         <xsl:choose>
832
            <xsl:when test="$c = '&#34;'">" '"' "</xsl:when>
833
            <xsl:when test="$c = '&#xA;'">\x{A}</xsl:when>
834
            <xsl:when test="$c = '&#xD;'">\x{D}</xsl:when>
835
            <xsl:otherwise>
836
               <xsl:value-of select="$c"/>
837
            </xsl:otherwise>
838
         </xsl:choose>
839
         <xsl:call-template name="quote-quotes">
840
            <xsl:with-param name="str" select="$str"/>
841
            <xsl:with-param name="pos" select="$pos + 1"/>
842
         </xsl:call-template>
843
      </xsl:if>
844
  </xsl:template>
845
  <xsl:template name="make-string-literal">
846
      <xsl:choose>
847
         <xsl:when test="not (contains (., '&#34;') or   contains (., &#34;'&#34;) or   contains (., '&#xA;') or contains (., '&#xD;'))">
848
            <xsl:call-template name="str">
849
               <xsl:with-param name="text" select="."/>
850
            </xsl:call-template>
851
         </xsl:when>
852
         <xsl:when test="not (contains (., '&#34;&#34;&#34;'))">
853
            <xsl:call-template name="str">
854
               <xsl:with-param name="quote" select="'&#34;&#34;&#34;'"/>
855
               <xsl:with-param name="text" select="."/>
856
               <xsl:with-param name="is-multiline" select="true ()"/>
857
            </xsl:call-template>
858
         </xsl:when>
859
         <xsl:when test="not (contains (., &#34;'''&#34;))">
860
            <xsl:call-template name="str">
861
               <xsl:with-param name="quote">'''</xsl:with-param>
862
               <xsl:with-param name="text" select="."/>
863
               <xsl:with-param name="is-multiline" select="true ()"/>
864
            </xsl:call-template>
865
         </xsl:when>
866
         <xsl:otherwise>
867
            <xsl:call-template name="str">
868
               <xsl:with-param name="text">
869
                  <xsl:call-template name="quote-quotes">
870
                     <xsl:with-param name="str" select="."/>
871
                  </xsl:call-template>
872
               </xsl:with-param>
873
            </xsl:call-template>
874
         </xsl:otherwise>
875
      </xsl:choose>
876
  </xsl:template>
877
  <xsl:template name="type-name">
878
      <xsl:variable name="dt"
879
                    select="ancestor-or-self::rng:*[@datatypeLibrary][1]/@datatypeLibrary"/>
880
      <xsl:variable name="dts" select="string ($dt)"/>
881
      <type>
882
         <xsl:call-template name="text">
883
            <xsl:with-param name="text">
884
               <xsl:choose>
885
                  <xsl:when test="self::rng:value and not (@type)">token</xsl:when>
886
                  <xsl:when test="$dts = '' and @type= 'string'">string</xsl:when>
887
                  <xsl:when test="$dts = '' and @type= 'token'">token</xsl:when>
888
                  <xsl:when test="$dts = ''">
889
                     <xsl:text>xsd:</xsl:text>
890
                     <xsl:value-of select="@type"/>
891
                  </xsl:when>
892
                  <xsl:when test="$dts = $xsd">
893
                     <xsl:text>xsd:</xsl:text>
894
                     <xsl:value-of select="@type"/>
895
                  </xsl:when>
896
                  <xsl:otherwise>
897
                     <xsl:value-of select="generate-id ($dt)"/>
898
                     <xsl:text>:</xsl:text>
899
                     <xsl:value-of select="@type"/>
900
                  </xsl:otherwise>
901
               </xsl:choose>
902
            </xsl:with-param>
903
         </xsl:call-template>
904
      </type>
905
  </xsl:template>
906
  <xsl:template name="need-paren">
907
      <xsl:choose>
908
         <xsl:when test="self::rng:optional or  self::rng:zeroOrMore or  self::rng:oneOrMore">
909
            <xsl:if test="count (rng:*) &gt; 1">true</xsl:if>
910
         </xsl:when>
911
         <xsl:when test="parent::rng:element[not (@name)] or   parent::rng:attribute [not (@name)]">
912
            <xsl:if test="count (../rng:*) &gt; 2">true</xsl:if>
913
         </xsl:when>
914
         <xsl:when test="count (../rng:*) &gt; 1">true</xsl:when>
915
         <xsl:when test="count (rng:*) &gt; 1 and (parent::rng:optional or   parent::rng:zeroOrMore or parent::rng:oneOrMore)">true</xsl:when>
916
      </xsl:choose>
917
  </xsl:template>
918
  <xsl:template name="expression-body">
919
      <xsl:param name="patterns" select="rng:*"/>
920
      <xsl:param name="operator"/>
921
      <xsl:param name="is-prefix-operator" select="false ()"/>
922
      <xsl:choose>
923
         <xsl:when test="$is-prefix-operator">
924
            <xsl:for-each select="$patterns">
925
               <xsl:if test="position () != 1">
926
                  <nl size="1"/>
927
                  <op size="{string-length ($operator)}">
928
                     <xsl:value-of select="$operator"/>
929
                  </op>
930
               </xsl:if>
931
               <xsl:apply-templates select="." mode="RNC"/>
932
               <xsl:call-template name="follow-annotations"/>
933
            </xsl:for-each>
934
         </xsl:when>
935
         <xsl:otherwise>
936
            <xsl:for-each select="$patterns">
937
               <xsl:if test="position () != 1">
938
                  <op size="{string-length ($operator)}">
939
                     <xsl:value-of select="$operator"/>
940
                  </op>
941
                  <nl size="1"/>
942
               </xsl:if>
943
               <xsl:apply-templates select="." mode="RNC"/>
944
               <xsl:call-template name="follow-annotations"/>
945
            </xsl:for-each>
946
         </xsl:otherwise>
947
      </xsl:choose>
948
  </xsl:template>
949
  <xsl:template name="group-body">
950
      <xsl:param name="patterns" select="rng:*"/>
951
      <xsl:call-template name="expression-body">
952
         <xsl:with-param name="patterns" select="$patterns"/>
953
         <xsl:with-param name="operator" select="','"/>
954
      </xsl:call-template>
955
  </xsl:template>
956
   <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
957
<!-- Name class -->
958
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
959
  <xsl:template name="name-class">
960
      <xsl:param name="is-attr" select="false ()"/>
961
      <nc>
962
         <xsl:call-template name="text">
963
            <xsl:with-param name="text">
964
               <xsl:choose>
965
                  <xsl:when test="@name">
966
                     <xsl:choose>
967
                        <xsl:when test="not ($is-attr) and not (contains (@name, ':'))">
968
                           <xsl:call-template name="make-name-nc">
969
                              <xsl:with-param name="name" select="@name"/>
970
                              <xsl:with-param name="is-attr" select="false ()"/>
971
                           </xsl:call-template>
972
                        </xsl:when>
973
                        <xsl:when test="@ns">
974
                           <xsl:text>[</xsl:text>
975
                           <xsl:value-of select="@ns"/>
976
                           <xsl:text>]</xsl:text>
977
                           <xsl:value-of select="@name"/>
978
                        </xsl:when>
979
                        <xsl:otherwise>
980
                           <xsl:value-of select="@name"/>
981
                        </xsl:otherwise>
982
                     </xsl:choose>
983
                  </xsl:when>
984
                  <xsl:otherwise>
985
                     <xsl:apply-templates select="rng:*[1]" mode="name-class">
986
                        <xsl:with-param name="is-attr" select="$is-attr"/>
987
                     </xsl:apply-templates>
988
                  </xsl:otherwise>
989
               </xsl:choose>
990
            </xsl:with-param>
991
         </xsl:call-template>
992
      </nc>
993
  </xsl:template>
994
  <xsl:template match="rng:name" mode="name-class">
995
      <xsl:param name="is-attr"/>
996
      <xsl:call-template name="make-name-nc">
997
         <xsl:with-param name="name" select="."/>
998
         <xsl:with-param name="is-attr" select="$is-attr"/>
999
      </xsl:call-template>
1000
  </xsl:template>
1001
  <xsl:template match="rng:nsName" mode="name-class">
1002
      <xsl:param name="is-attr"/>
1003
      <xsl:variable name="ns" select="ancestor-or-self::rng:*[@ns][1]/@ns"/>
1004
      <xsl:choose>
1005
         <xsl:when test="$has-default-ns and $ns = $default-ns">
1006
            <xsl:value-of select="$default-ns-id"/>
1007
         </xsl:when>
1008
         <xsl:when test="$has-local and $ns = ''">local</xsl:when>
1009
         <xsl:when test="not($ns='')">
1010
            <xsl:call-template name="get-prefix">
1011
               <xsl:with-param name="nd" select="$ns"/>
1012
            </xsl:call-template>
1013
         </xsl:when>
1014
         <xsl:otherwise>
1015
            <xsl:text>inh</xsl:text>
1016
         </xsl:otherwise>
1017
      </xsl:choose>
1018
      <xsl:text>:*</xsl:text>
1019
      <xsl:call-template name="nc-except">
1020
         <xsl:with-param name="is-attr" select="$is-attr"/>
1021
      </xsl:call-template>
1022
  </xsl:template>
1023
  <xsl:template match="rng:anyName" mode="name-class">
1024
      <xsl:param name="is-attr"/>
1025
      <xsl:text>*</xsl:text>
1026
      <xsl:call-template name="nc-except">
1027
         <xsl:with-param name="is-attr" select="$is-attr"/>
1028
      </xsl:call-template>
1029
  </xsl:template>
1030
  <xsl:template name="nc-except">
1031
      <xsl:param name="is-attr"/>
1032
      <xsl:for-each select="rng:except">
1033
         <xsl:text> - </xsl:text>
1034
         <xsl:choose>
1035
            <xsl:when test="count (rng:*) &gt; 1">
1036
               <xsl:text>(</xsl:text>
1037
               <xsl:call-template name="make-nc-choice">
1038
                  <xsl:with-param name="is-attr" select="$is-attr"/>
1039
               </xsl:call-template>
1040
               <xsl:text>)</xsl:text>
1041
            </xsl:when>
1042
            <xsl:otherwise>
1043
               <xsl:apply-templates select="rng:*" mode="name-class">
1044
                  <xsl:with-param name="is-attr" select="$is-attr"/>
1045
               </xsl:apply-templates>
1046
            </xsl:otherwise>
1047
         </xsl:choose>
1048
      </xsl:for-each>
1049
  </xsl:template>
1050
  <xsl:template match="rng:attribute/rng:choice | rng:element/rng:choice" mode="name-class"
1051
                 name="make-nc-choice">
1052
      <xsl:param name="is-attr"/>
1053
      <xsl:for-each select="rng:*">
1054
         <xsl:if test="position () != 1">
1055
            <xsl:text> | </xsl:text>
1056
         </xsl:if>
1057
         <xsl:apply-templates select="." mode="name-class">
1058
            <xsl:with-param name="is-attr" select="$is-attr"/>
1059
         </xsl:apply-templates>
1060
      </xsl:for-each>
1061
  </xsl:template>
1062
  <xsl:template match="rng:choice" mode="name-class">
1063
      <xsl:param name="is-attr"/>
1064
      <xsl:text>(</xsl:text>
1065
      <xsl:call-template name="make-nc-choice">
1066
         <xsl:with-param name="is-attr" select="$is-attr"/>
1067
      </xsl:call-template>
1068
      <xsl:text>)</xsl:text>
1069
  </xsl:template>
1070
  <xsl:template name="make-name-nc">
1071
      <xsl:param name="is-attr"/>
1072
      <xsl:param name="name"/>
1073
      <xsl:variable name="ns" select="ancestor-or-self::rng:*[@ns][1]/@ns"/>
1074
      <xsl:choose>
1075
         <xsl:when test="not ($is-attr) and $has-default-ns and  $ns = $default-ns"/>
1076
         <xsl:when test="$is-attr and $ns = ''"/>
1077
         <xsl:when test="$is-attr and not($default-ns-id='') and $has-default-ns and $ns = $default-ns">
1078
            <xsl:value-of select="$default-ns-id"/>
1079
            <xsl:text>:</xsl:text>
1080
         </xsl:when>
1081
         <xsl:when test="string-length($ns)&gt;0">
1082
            <xsl:call-template name="get-prefix">
1083
               <xsl:with-param name="nd" select="$ns"/>
1084
            </xsl:call-template>
1085
            <xsl:text>:</xsl:text>
1086
         </xsl:when>
1087
         <!--
1088
      <xsl:when test="$is-attr or $has-default-ns">
1089
        <xsl:text>inh:</xsl:text>
1090
      </xsl:when>
1091
-->
1092
    </xsl:choose>
1093
      <xsl:value-of select="$name"/>
1094
  </xsl:template>
1095
   <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
1096
<!-- Util -->
1097
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
1098
  <xsl:template name="last-token">
1099
      <xsl:param name="str"/>
1100
      <xsl:param name="pos" select="string-length ($str)"/>
1101
      <xsl:choose>
1102
         <xsl:when test="$pos = 0">
1103
            <xsl:value-of select="$str"/>
1104
         </xsl:when>
1105
         <xsl:when test="substring ($str, $pos, 1) = ' '">
1106
            <xsl:value-of select="substring ($str, $pos + 1)"/>
1107
         </xsl:when>
1108
         <xsl:otherwise>
1109
            <xsl:call-template name="last-token">
1110
               <xsl:with-param name="str" select="$str"/>
1111
               <xsl:with-param name="pos" select="$pos - 1"/>
1112
            </xsl:call-template>
1113
         </xsl:otherwise>
1114
      </xsl:choose>
1115
  </xsl:template>
1116
  <xsl:variable name="simple-prefix-map" select="concat (' ', $prefixes, ' ')"/>
1117
  <xsl:template name="mapped-prefix">
1118
      <xsl:param name="ns"/>
1119
      <xsl:variable name="simple-mapped"
1120
                    select="substring-before ($simple-prefix-map, concat ('=', $ns, ' '))"/>
1121
      <xsl:choose>
1122
         <xsl:when test="$simple-mapped">
1123
            <xsl:call-template name="last-token">
1124
               <xsl:with-param name="str" select="$simple-mapped"/>
1125
            </xsl:call-template>
1126
         </xsl:when>
1127
         <xsl:otherwise>
1128
            <xsl:variable name="mapped" select="$prefix-map [string (.) = $ns][1]"/>
1129
            <xsl:if test="$mapped">
1130
               <xsl:value-of select="$mapped/@prefix"/>
1131
            </xsl:if>
1132
         </xsl:otherwise>
1133
      </xsl:choose>
1134
  </xsl:template>
1135
  <xsl:template name="get-prefix">
1136
      <xsl:param name="nd" select="."/>
1137
      <xsl:variable name="ns" select="string ($nd)"/>
1138
      <xsl:variable name="mapped">
1139
         <xsl:call-template name="mapped-prefix">
1140
            <xsl:with-param name="ns" select="$ns"/>
1141
         </xsl:call-template>
1142
      </xsl:variable>
1143
      <xsl:choose>
1144
         <xsl:when test="string ($mapped)">
1145
            <xsl:value-of select="$mapped"/>
1146
         </xsl:when>
1147
         <xsl:when test="not ($retain-prefixes)">
1148
            <xsl:value-of select="generate-id ($nd)"/>
1149
         </xsl:when>
1150
         <xsl:when test="key ('ns', $ns)">
1151
           <xsl:variable name="pfx" select="name ( (key ('ns', $ns)/namespace::*[. = $ns])[1] )"/>
1152
           <xsl:choose>
1153
               <xsl:when test="$pfx">
1154
                  <xsl:value-of select="$pfx"/>
1155
               </xsl:when>
1156
               <xsl:otherwise>
1157
                  <xsl:value-of select="generate-id (key ('ns', $ns)[1])"/>
1158
               </xsl:otherwise>
1159
            </xsl:choose>
1160
         </xsl:when>
1161
         <xsl:otherwise>
1162
            <xsl:value-of select="generate-id ($nd)"/>
1163
         </xsl:otherwise>
1164
      </xsl:choose>
1165
  </xsl:template>
1166
  <xsl:template name="spacer">
1167
      <xsl:if test="following-sibling::*">
1168
         <nl size="1"/>
1169
         <xsl:if test="not (parent::rng:include) and  not (parent::rng:grammar/../..) and   not (self::rng:include and   following-sibling::*[1][self::rng:include])">
1170
            <nl size="1"/>
1171
         </xsl:if>
1172
      </xsl:if>
1173
  </xsl:template>
1174
  <keywords xmlns="http://www.example.com">
1175
      <kw>attribute</kw>
1176
      <kw>default</kw>
1177
      <kw>datatypes</kw>
1178
      <kw>div</kw>
1179
      <kw>element</kw>
1180
      <kw>empty</kw>
1181
      <kw>external</kw>
1182
      <kw>grammar</kw>
1183
      <kw>include</kw>
1184
      <kw>inherit</kw>
1185
      <kw>list</kw>
1186
      <kw>mixed</kw>
1187
      <kw>namespace</kw>
1188
      <kw>notAllowed</kw>
1189
      <kw>parent</kw>
1190
      <kw>start</kw>
1191
      <kw>string</kw>
1192
      <kw>text</kw>
1193
      <kw>token</kw>
1194
  </keywords>
1195
  <xsl:variable xmlns:foo="http://www.example.com" name="keywords"  select="document ('')/*/foo:keywords/*"/>
1196
  <xsl:template name="quote-keyword">
1197
      <xsl:param name="name"/>
1198
      <xsl:if test="$name = $keywords">\</xsl:if>
1199
      <xsl:value-of select="$name"/>
1200
  </xsl:template>
1201
   <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
1202
<!-- Annotations -->
1203
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
1204
  <xsl:template name="leading-documentation">
1205
      <xsl:param name="nd"/>
1206
      <!--
1207
    <xsl:choose>
1208
      <xsl:when test="not ($nd) or $nd/self::rng:*"/>
1209

    
1210
      <xsl:when test="$nd/self::a:documentation">
1211
	<xsl:call-template name="doc-comment">
1212
	  <xsl:with-param name="comment" select="string ($nd)"/>
1213
	</xsl:call-template>
1214
	<nl size="1"/>
1215
	<xsl:call-template name="leading-documentation">
1216
	  <xsl:with-param name="nd" select="$nd/following-sibling::*[1]"/>
1217
	</xsl:call-template>
1218
      </xsl:when>
1219

    
1220
      <xsl:otherwise>
1221
	<group>
1222
	  <t size="1">[</t>
1223
	  <indent>
1224
	    <xsl:call-template name="annotation-attributes"/>
1225

    
1226
	    <xsl:call-template name="leading-annotations">
1227
	      <xsl:with-param name="nd" select="$nd"/>
1228
	    </xsl:call-template>
1229
	  </indent>
1230
	  <nl size="1"/>
1231
	  <t size="1">]</t>
1232
	</group>
1233
	<nl size="1"/>
1234
      </xsl:otherwise>
1235
    </xsl:choose>
1236
-->
1237
  </xsl:template>
1238
  <xsl:template name="leading-annotations">
1239
      <xsl:param name="nd"/>
1240
      <xsl:if test="$nd and not ($nd/self::rng:*)">
1241
         <xsl:for-each select="$nd">
1242
            <xsl:call-template name="annotation-element"/>
1243
         </xsl:for-each>
1244
         <xsl:call-template name="leading-annotations">
1245
            <xsl:with-param name="nd" select="$nd/following-sibling::*[1]"/>
1246
         </xsl:call-template>
1247
      </xsl:if>
1248
  </xsl:template>
1249
  <xsl:template name="annotations">
1250
<!--
1251
    <xsl:choose>
1252
      <xsl:when test="(self::rng:value or self::rng:param) and
1253
	following-sibling::*[1][not (self::rng:*)]">
1254
	<xsl:call-template name="leading-documentation">
1255
	  <xsl:with-param name="nd" select="following-sibling::*[1]"/>
1256
	</xsl:call-template>
1257
      </xsl:when>
1258
      <xsl:when test="*[1][not (self::rng:*)]">
1259
	<xsl:call-template name="leading-documentation">
1260
	  <xsl:with-param name="nd" select="*[1]"/>
1261
	</xsl:call-template>
1262
      </xsl:when>
1263
      <xsl:when test="@*[namespace-uri () != '']">
1264
	<group>
1265
	  <t size="1">[</t>
1266
	  <indent>
1267
	    <xsl:call-template name="annotation-attributes"/>
1268
	  </indent>
1269
	  <nl size="1"/>
1270
	  <t size="1">]</t>
1271
	</group>
1272
	<nl size="1"/>
1273
      </xsl:when>
1274
    </xsl:choose>
1275
-->
1276
  </xsl:template>
1277
  <xsl:template name="doc-comment">
1278
      <xsl:param name="comment"/>
1279
      <!--
1280
    <xsl:if test="$comment">
1281
      <xsl:variable name="head" select="substring-before ($comment, '&#10;')"/>
1282
      <xsl:variable name="line">
1283
	<xsl:text>## </xsl:text>
1284
	<xsl:choose>
1285
	  <xsl:when test="$head">
1286
	    <xsl:value-of select="$head"/>
1287
	  </xsl:when>
1288
	  <xsl:otherwise>
1289
	    <xsl:value-of select="$comment"/>
1290
	  </xsl:otherwise>
1291
	</xsl:choose>
1292
      </xsl:variable>
1293
      
1294
      <doc>
1295
	<xsl:call-template name="text">
1296
	  <xsl:with-param name="text" select="$line"/>
1297
	</xsl:call-template>
1298
      </doc>
1299
      <nl size="1"/>
1300
      <xsl:call-template name="doc-comment">
1301
	<xsl:with-param name="comment"
1302
	  select="substring-after ($comment, '&#10;')"/>
1303
      </xsl:call-template>
1304
    </xsl:if>
1305
-->
1306
  </xsl:template>
1307
  <xsl:template match="rng:grammar/a:documentation | rng:div/a:documentation |     rng:include/a:documentation"
1308
                 priority="-15">
1309
      <xsl:if test="not (../../..) or        preceding-sibling::*[not (self::a:documentation)]">
1310
         <xsl:call-template name="annotation-element"/>
1311
      </xsl:if>
1312
  </xsl:template>
1313
  <xsl:template match="rng:grammar/* | rng:div/* | rng:include/*" priority="-20">
1314
      <xsl:call-template name="annotation-element"/>
1315
  </xsl:template>
1316
  <xsl:template name="follow-annotations">
1317
      <xsl:param name="nd"
1318
                 select="self::*[not (self::rng:value or self::rng:param)]       /following-sibling::*[1][not (self::rng:*)]"/>
1319
      <xsl:if test="$nd">
1320
         <xsl:for-each select="$nd">
1321
            <nl size="1"/>
1322
            <xsl:call-template name="follow-annotation"/>
1323
            <xsl:call-template name="follow-annotations"/>
1324
         </xsl:for-each>
1325
      </xsl:if>
1326
  </xsl:template>
1327
  <xsl:template match="rng:*" priority="-100" name="follow-annotation">
1328
      <xsl:if test="preceding-sibling::rng:*">
1329
         <t size="2">&gt;&gt;</t>
1330
         <xsl:call-template name="annotation-element"/>
1331
      </xsl:if>
1332
  </xsl:template>
1333
  <xsl:template name="annotation-name">
1334
      <annot>
1335
         <xsl:call-template name="text">
1336
            <xsl:with-param name="text">
1337
               <xsl:variable name="ns" select="namespace-uri (.)"/>
1338
               <xsl:if test="not($ns='')">
1339
                  <xsl:variable name="mapped">
1340
                     <xsl:call-template name="mapped-prefix">
1341
                        <xsl:with-param name="ns" select="$ns"/>
1342
                     </xsl:call-template>
1343
                  </xsl:variable>
1344
                  <xsl:choose>
1345
                     <xsl:when test="string ($mapped)">
1346
                        <xsl:value-of select="$mapped"/>
1347
                     </xsl:when>
1348
                     <xsl:when test="$retain-prefixes">
1349
                       <xsl:variable name="pfx" select="name ( (key ('ns', $ns)/namespace::*[. = $ns])[1] )"/>
1350
                       <xsl:choose>
1351
                           <xsl:when test="$pfx">
1352
                              <xsl:value-of select="$pfx"/>
1353
                           </xsl:when>
1354
                           <xsl:otherwise>
1355
                              <xsl:value-of select="generate-id (key ('ns', $ns)[1])"/>
1356
                           </xsl:otherwise>
1357
                        </xsl:choose>
1358
                     </xsl:when>
1359
                     <xsl:otherwise>
1360
                        <xsl:value-of select="generate-id (key ('annot-ns', $ns)[1])"/>
1361
                     </xsl:otherwise>
1362
                  </xsl:choose>
1363
                  <xsl:text>:</xsl:text>
1364
               </xsl:if>
1365
               <xsl:value-of select="local-name (.)"/>
1366
            </xsl:with-param>
1367
         </xsl:call-template>
1368
      </annot>
1369
  </xsl:template>
1370
  <xsl:template name="annotation-attributes">
1371
      <xsl:for-each select="@*[namespace-uri () != '']">
1372
         <nl size="1"/>
1373
         <xsl:call-template name="annotation-name"/>
1374
         <t size="3"> = </t>
1375
         <xsl:call-template name="make-string-literal"/>
1376
      </xsl:for-each>
1377
  </xsl:template>
1378
  <xsl:template match="rng:*" name="annotation-element" mode="annot">
1379
      <nl size="1"/>
1380
      <group>
1381
         <xsl:call-template name="annotation-name"/>
1382
         <nl size="1"/>
1383
         <t size="1">[</t>
1384
         <indent>
1385
            <xsl:for-each select="@*">
1386
               <nl size="1"/>
1387
               <xsl:call-template name="annotation-name"/>
1388
               <t size="3"> = </t>
1389
               <xsl:call-template name="make-string-literal"/>
1390
            </xsl:for-each>
1391
            <xsl:apply-templates mode="annot"/>
1392
         </indent>
1393
         <nl size="1"/>
1394
         <t size="1">]</t>
1395
      </group>
1396
      <xsl:if test="following-sibling::*">
1397
         <xsl:if test="parent::rng:grammar or parent::rng:div or   parent::rng:include">
1398
            <nl size="1"/>
1399
            <xsl:if test="following-sibling::*[1][self::rng:*]">
1400
               <nl size="1"/>
1401
            </xsl:if>
1402
         </xsl:if>
1403
      </xsl:if>
1404
  </xsl:template>
1405
  <xsl:template match="text ()" mode="annot">
1406
      <nl size="1"/>
1407
      <xsl:call-template name="make-string-literal"/>
1408
  </xsl:template>
1409
  <xsl:template name="make-body-from-r-t-f">
1410
      <xsl:param name="schema"/>
1411
      <xsl:for-each select="$schema">
1412
         <xsl:call-template name="make-body"/>
1413
      </xsl:for-each>
1414
  </xsl:template>
1415
  <xsl:template name="make-body">
1416
      <xsl:apply-templates mode="keep"/>
1417
  </xsl:template>
1418
  <xsl:template match="group" mode="keep">
1419
      <xsl:choose>
1420
         <xsl:when test="  not ($collapse-lines) or   @collapse = 'no' or  .//doc or   .//group [@collapse = 'no']">
1421
            <xsl:apply-templates mode="keep"/>
1422
         </xsl:when>
1423
         <xsl:otherwise>
1424
            <xsl:variable name="size" select="sum (.//*/@size)"/>
1425
            <xsl:variable name="level" select="count (ancestor::indent)"/>
1426
            <xsl:choose>
1427
               <xsl:when test="$line-width &gt; ($size + ($level * $indent-width))">
1428
                  <xsl:apply-templates mode="flatten"/>
1429
               </xsl:when>
1430
               <xsl:otherwise>
1431
                  <xsl:apply-templates mode="keep"/>
1432
               </xsl:otherwise>
1433
            </xsl:choose>
1434
         </xsl:otherwise>
1435
      </xsl:choose>
1436
  </xsl:template>
1437
  <xsl:template match="sp" mode="keep">
1438
      <xsl:text> </xsl:text>
1439
  </xsl:template>
1440
  <xsl:variable name="spaces"
1441
                 select="concat (     '                                        ',     '                                        '     )"/>
1442
  <xsl:template match="nl" mode="keep">
1443
      <xsl:text>&#10;</xsl:text>
1444
      <xsl:variable name="level" select="count (ancestor::indent)"/>
1445
      <xsl:variable name="following-op" select="following-sibling::*[1][self::op]"/>
1446
      <xsl:choose>
1447
         <xsl:when test="$following-op">
1448
            <xsl:value-of select="substring ($spaces, 1,     $level * $indent-width - $following-op/@size)"/>
1449
         </xsl:when>
1450
         <xsl:otherwise>
1451
            <xsl:value-of select="substring ($spaces, 1, $level * $indent-width)"/>
1452
         </xsl:otherwise>
1453
      </xsl:choose>
1454
  </xsl:template>
1455
  <xsl:template match="sp | nl" mode="flatten">
1456
      <xsl:text> </xsl:text>
1457
  </xsl:template>
1458
  <xsl:template match="ref" mode="flatten">
1459
      <xsl:variable name="me">
1460
         <xsl:choose>
1461
            <xsl:when test="contains(@name,'.attributes')">
1462
               <xsl:value-of select="substring-before(@name,'.attributes')"/>
1463
            </xsl:when>
1464
            <xsl:when test="contains(@name,'.content')">
1465
               <xsl:value-of select="substring-before(.,'.content')"/>
1466
            </xsl:when>
1467
            <xsl:when test="contains(@name,'.attribute.')">
1468
               <xsl:value-of select="substring-before(@name,'.attribute.')"/>
1469
            </xsl:when>
1470
            <xsl:otherwise>
1471
               <xsl:value-of select="@name"/>
1472
            </xsl:otherwise>
1473
         </xsl:choose>
1474
      </xsl:variable>
1475
      <!--
1476
<xsl:message>FROM <xsl:value-of select="@name"/> to <xsl:value-of
1477
    select="$me"/>, <xsl:for-each select="$top"><xsl:value-of 
1478
select="count(key('IDENTS',$me))"/></xsl:for-each></xsl:message>
1479
-->
1480
    <xsl:variable name="n" select="@name"/>
1481
      <xsl:choose>
1482
         <xsl:when test="contains(.,'.localattributes')">
1483
            <xsl:value-of select="$n"/>
1484
         </xsl:when>
1485
         <xsl:when test="contains(@name,'.content')">
1486
            <xsl:value-of select="$n"/>
1487
         </xsl:when>
1488
         <xsl:otherwise>
1489
            <xsl:for-each select="$top">
1490
               <xsl:call-template name="linkTogether">
1491
                  <xsl:with-param name="name">
1492
                     <xsl:value-of select="$me"/>
1493
                  </xsl:with-param>
1494
                  <xsl:with-param name="reftext">
1495
                     <xsl:value-of select="$n"/>
1496
                  </xsl:with-param>
1497
               </xsl:call-template>
1498
            </xsl:for-each>
1499
         </xsl:otherwise>
1500
      </xsl:choose>
1501
  </xsl:template>
1502
</xsl:transform>