Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / tei / docx / utils / maths / mml2omml.xsl @ 187

History | View | Annotate | Download (164.7 kB)

1
<?xml version="1.0" encoding="utf-8"?>
2
<!-- Beta Version 070708 --><xsl:stylesheet xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
3
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4
                xmlns:mml="http://www.w3.org/1998/Math/MathML"
5
                xmlns="http://schemas.openxmlformats.org/officeDocument/2006/math"
6
                xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
7
                version="2.0"
8
                exclude-result-prefixes="mml m w">
9
  <xsl:output method="xml" encoding="UTF-8"/>
10

    
11

    
12
  <xsl:variable name="StrUCAlphabet">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
13
  <xsl:variable name="StrLCAlphabet">abcdefghijklmnopqrstuvwxyz</xsl:variable>
14

    
15

    
16
<!--
17
  <xsl:template match="/">
18
    <oMath>
19
      <xsl:apply-templates mode="mml"  />
20
    </oMath>
21
  </xsl:template>
22
-->
23

    
24
  <!-- %%Template: SReplace
25

    
26
		Replace all occurences of sOrig in sInput with sReplacement
27
		and return the resulting string. -->
28
  <xsl:template name="SReplace">
29
      <xsl:param name="sInput"/>
30
      <xsl:param name="sOrig"/>
31
      <xsl:param name="sReplacement"/>
32

    
33
      <xsl:choose>
34
         <xsl:when test="not(contains($sInput, $sOrig))">
35
            <xsl:value-of select="$sInput"/>
36
         </xsl:when>
37
         <xsl:otherwise>
38
            <xsl:variable name="sBefore" select="substring-before($sInput, $sOrig)"/>
39
            <xsl:variable name="sAfter" select="substring-after($sInput, $sOrig)"/>
40
            <xsl:variable name="sAfterProcessed">
41
               <xsl:call-template name="SReplace">
42
                  <xsl:with-param name="sInput" select="$sAfter"/>
43
                  <xsl:with-param name="sOrig" select="$sOrig"/>
44
                  <xsl:with-param name="sReplacement" select="$sReplacement"/>
45
               </xsl:call-template>
46
            </xsl:variable>
47

    
48
            <xsl:value-of select="concat($sBefore, concat($sReplacement, $sAfterProcessed))"/>
49
         </xsl:otherwise>
50
      </xsl:choose>
51
  </xsl:template>
52

    
53
  <!-- %%Template: OutputText
54

    
55
		Post processing on the string given and otherwise do
56
		a xsl:value-of on it -->
57
  <xsl:template name="OutputText">
58
      <xsl:param name="sInput"/>
59

    
60
      <!-- Add local Variable as you add new post processing tasks -->
61

    
62
    <!-- 1. Remove any unwanted characters -->
63
    <xsl:variable name="sCharStrip">
64
         <xsl:value-of select="translate($sInput, '⁢​', '')"/>
65
      </xsl:variable>
66

    
67
      <!-- 2. Replace any characters as needed -->
68
    <!--	Replace &#x2A75; <-> ==			 -->
69
    <xsl:variable name="sCharReplace">
70
         <xsl:call-template name="SReplace">
71
            <xsl:with-param name="sInput" select="$sCharStrip"/>
72
            <xsl:with-param name="sOrig" select="'⩵'"/>
73
            <xsl:with-param name="sReplacement" select="'=='"/>
74
         </xsl:call-template>
75
      </xsl:variable>
76

    
77
      <!-- Finally, return the last value -->
78
    <xsl:value-of select="$sCharReplace"/>
79
  </xsl:template>
80

    
81

    
82
  <!-- Template that determines whether or the given node 
83
	     ndCur is a token element that doesn't have an mglyph as 
84
			 a child.
85
	-->
86
  <xsl:template name="FNonGlyphToken">
87
      <xsl:param name="ndCur" select="."/>
88
      <xsl:choose>
89
         <xsl:when test="$ndCur/self::mml:mi[not(child::mml:mglyph)] |                        $ndCur/self::mml:mn[not(child::mml:mglyph)] |                        $ndCur/self::mml:mo[not(child::mml:mglyph)] |                        $ndCur/self::mml:ms[not(child::mml:mglyph)] |                        $ndCur/self::mml:mtext[not(child::mml:mglyph)]">1</xsl:when>
90
         <xsl:otherwise>0</xsl:otherwise>
91
      </xsl:choose>
92
  </xsl:template>
93

    
94

    
95
  <!-- Template used to determine if the current token element (ndCur) is the beginning of a run. 
96
			 A token element is the beginning of if:
97
			 
98
			 the count of preceding elements is 0 
99
			 or 
100
			 the directory preceding element is not a non-glyph token.
101
	-->
102
  <xsl:template name="FStartOfRun">
103
      <xsl:param name="ndCur" select="."/>
104
      <xsl:variable name="fPrecSibNonGlyphToken">
105
         <xsl:call-template name="FNonGlyphToken">
106
            <xsl:with-param name="ndCur" select="$ndCur/preceding-sibling::*[1]"/>
107
         </xsl:call-template>
108
      </xsl:variable>
109
      <xsl:choose>
110
         <xsl:when test="count($ndCur/preceding-sibling::*)=0             or $fPrecSibNonGlyphToken=0">1</xsl:when>
111
         <xsl:otherwise>0</xsl:otherwise>
112
      </xsl:choose>
113
  </xsl:template>
114

    
115
  <!-- Template that determines if ndCur is the argument of an nary expression. 
116
			 
117
			 ndCur is the argument of an nary expression if:
118
			 
119
			 1.  The preceding sibling is one of the following:  munder, mover, msub, msup, munder, msubsup, munderover
120
			 and
121
			 2.  The preceding sibling's child is an nary char as specified by the template "isNary"
122
	-->
123
  <xsl:template name="FIsNaryArgument">
124
      <xsl:param name="ndCur" select="."/>
125

    
126
      <xsl:variable name="fNary">
127
         <xsl:call-template name="isNary">
128
            <xsl:with-param name="ndCur" select="$ndCur/preceding-sibling::*[1]/child::*[1]"/>
129
         </xsl:call-template>
130
      </xsl:variable>
131
      <xsl:choose>
132
         <xsl:when test="preceding-sibling::*[1][self::mml:munder or self::mml:mover or self::mml:munderover or                                                     self::mml:msub or self::mml:msup or self::mml:msubsup]              and $fNary='true'">1</xsl:when>
133
         <xsl:otherwise>0</xsl:otherwise>
134
      </xsl:choose>
135
  </xsl:template>
136

    
137
  <!-- %%Template: mml:mrow | mml:mstyle
138

    
139
		 if this row is the next sibling of an n-ary (i.e. any of 
140
         mover, munder, munderover, msupsub, msup, or msub with 
141
         the base being an n-ary operator) then ignore this. Otherwise
142
         pass through -->
143
  <xsl:template mode="mml" match="mml:mrow|mml:mstyle">
144
      <xsl:variable name="fNaryArgument">
145
         <xsl:call-template name="FIsNaryArgument">
146
            <xsl:with-param name="ndCur" select="."/>
147
         </xsl:call-template>
148
      </xsl:variable>
149
      <xsl:if test="$fNaryArgument=0">
150
         <xsl:variable name="fLinearFrac">
151
            <xsl:call-template name="FLinearFrac">
152
               <xsl:with-param name="ndCur" select="."/>
153
            </xsl:call-template>
154
         </xsl:variable>
155
         <xsl:choose>
156
            <xsl:when test="$fLinearFrac=1">
157
               <xsl:call-template name="MakeLinearFraction">
158
                  <xsl:with-param name="ndCur" select="."/>
159
               </xsl:call-template>
160
            </xsl:when>
161
            <xsl:otherwise>
162
               <xsl:variable name="fFunc">
163
                  <xsl:call-template name="FIsFunc">
164
                     <xsl:with-param name="ndCur" select="."/>
165
                  </xsl:call-template>
166
               </xsl:variable>
167
               <xsl:choose>
168
                  <xsl:when test="$fFunc=1">
169
                     <xsl:call-template name="WriteFunc">
170
                        <xsl:with-param name="ndCur" select="."/>
171
                     </xsl:call-template>
172
                  </xsl:when>
173
                  <xsl:otherwise>
174
                     <xsl:apply-templates mode="mml"/>
175
                  </xsl:otherwise>
176
               </xsl:choose>
177
            </xsl:otherwise>
178
         </xsl:choose>
179
      </xsl:if>
180
  </xsl:template>
181
  <xsl:template mode="mml"
182
                 match="mml:mi[not(child::mml:mglyph)] |                        mml:mn[not(child::mml:mglyph)] |                        mml:mo[not(child::mml:mglyph)] |                        mml:ms[not(child::mml:mglyph)] |                        mml:mtext[not(child::mml:mglyph)]">
183

    
184
    <!-- tokens with mglyphs as children are tranformed
185
			 in a different manner than "normal" token elements.  
186
			 Where normal token elements are token elements that
187
			 contain only text -->
188
    <xsl:variable name="fStartOfRun">
189
         <xsl:call-template name="FStartOfRun">
190
            <xsl:with-param name="ndCur" select="."/>
191
         </xsl:call-template>
192
      </xsl:variable>
193

    
194
      <!--In MathML, successive characters that are all part of one string are sometimes listed as separate 
195
			tags based on their type (identifier (mi), name (mn), operator (mo), quoted (ms), literal text (mtext)), 
196
			where said tags act to link one another into one logical run.  In order to wrap the text of successive mi's, 
197
			mn's, and mo's into one m:t, we need to denote where a run begins.  The beginning of a run is the first mi, mn, 
198
			or mo whose immediately preceding sibling either doesn't exist or is something other than a "normal" mi, mn, mo, 
199
			ms, or mtext tag-->
200

    
201
    <!-- If this mi/mo/mn/ms . . . is part the numerator or denominator of a linear fraction, then don't collect. -->
202
    <xsl:variable name="fLinearFracParent">
203
         <xsl:call-template name="FLinearFrac">
204
            <xsl:with-param name="ndCur" select="parent::*"/>
205
         </xsl:call-template>
206
      </xsl:variable>
207
      <!-- If this mi/mo/mn/ms . . . is part of the name of a function, then don't collect. -->
208
    <xsl:variable name="fFunctionName">
209
         <xsl:call-template name="FIsFunc">
210
            <xsl:with-param name="ndCur" select="parent::*"/>
211
         </xsl:call-template>
212
      </xsl:variable>
213
      <xsl:variable name="fShouldCollect"
214
                    select="($fLinearFracParent=0 and $fFunctionName=0) and (parent::mml:mrow or parent::mml:mstyle or       parent::mml:msqrt or parent::mml:menclose or      parent::mml:math or parent::mml:mphantom or       parent::mml:mtd or parent::mml:maction)"/>
215

    
216
      <!--In MathML, the meaning of the different parts that make up mathematical structures, such as a fraction 
217
			having a numerator and a denominator, is determined by the relative order of those different parts.  
218
			For instance, In a fraction, the numerator is the first child and the denominator is the second child.  
219
			To allow for more complex structures, MathML allows one to link a group of mi, mn, and mo's together 
220
			using the mrow, or mstyle tags.  The mi, mn, and mo's found within any of the above tags are considered 
221
			one run.  Therefore, if the parent of any mi, mn, or mo is found to be an mrow or mstyle, then the contiguous 
222
			mi, mn, and mo's will be considered one run.-->
223
    <xsl:choose>
224
         <xsl:when test="$fShouldCollect">
225
            <xsl:choose>
226
               <xsl:when test="$fStartOfRun=1">
227
            <!--If this is the beginning of the run, pass all run attributes to CreateRunWithSameProp.-->
228
            <xsl:call-template name="CreateRunWithSameProp">
229
                     <xsl:with-param name="mathbackground">
230
								<!-- Look for the unqualified mathml attribute mathbackground.
231
										 Fall back to the qualified mathml attribute if necessary.
232
										 This priority of unqualified over qualified will be 
233
										 followed throughout this xslt. -->
234
                <xsl:choose>
235
                           <xsl:when test="@mathbackground">
236
                              <xsl:value-of select="@mathbackground"/>
237
                           </xsl:when>
238
                           <xsl:otherwise>
239
                              <xsl:value-of select="@mml:mathbackground"/>
240
                           </xsl:otherwise>
241
                        </xsl:choose>
242
                     </xsl:with-param>
243
                     <xsl:with-param name="mathcolor">
244
                        <xsl:choose>
245
                           <xsl:when test="@mathcolor">
246
                              <xsl:value-of select="@mathcolor"/>
247
                           </xsl:when>
248
                           <xsl:otherwise>
249
                              <xsl:value-of select="@mml:mathcolor"/>
250
                           </xsl:otherwise>
251
                        </xsl:choose>
252
                     </xsl:with-param>
253
                     <xsl:with-param name="mathvariant">
254
                        <xsl:choose>
255
                           <xsl:when test="@mathvariant">
256
                              <xsl:value-of select="@mathvariant"/>
257
                           </xsl:when>
258
                           <xsl:otherwise>
259
                              <xsl:value-of select="@mml:mathvariant"/>
260
                           </xsl:otherwise>
261
                        </xsl:choose>
262
                     </xsl:with-param>
263
                     <xsl:with-param name="color">
264
                        <xsl:choose>
265
                           <xsl:when test="@color">
266
                              <xsl:value-of select="@color"/>
267
                           </xsl:when>
268
                           <xsl:otherwise>
269
                              <xsl:value-of select="@mml:color"/>
270
                           </xsl:otherwise>
271
                        </xsl:choose>
272
                     </xsl:with-param>
273
                     <xsl:with-param name="font-family">
274
                        <xsl:choose>
275
                           <xsl:when test="@font-family">
276
                              <xsl:value-of select="@font-family"/>
277
                           </xsl:when>
278
                           <xsl:otherwise>
279
                              <xsl:value-of select="@mml:font-family"/>
280
                           </xsl:otherwise>
281
                        </xsl:choose>
282
                     </xsl:with-param>
283
                     <xsl:with-param name="fontsize">
284
                        <xsl:choose>
285
                           <xsl:when test="@fontsize">
286
                              <xsl:value-of select="@fontsize"/>
287
                           </xsl:when>
288
                           <xsl:otherwise>
289
                              <xsl:value-of select="@mml:fontsize"/>
290
                           </xsl:otherwise>
291
                        </xsl:choose>
292
                     </xsl:with-param>
293
                     <xsl:with-param name="fontstyle">
294
                        <xsl:choose>
295
                           <xsl:when test="@fontstyle">
296
                              <xsl:value-of select="@fontstyle"/>
297
                           </xsl:when>
298
                           <xsl:otherwise>
299
                              <xsl:value-of select="@mml:fontstyle"/>
300
                           </xsl:otherwise>
301
                        </xsl:choose>
302
                     </xsl:with-param>
303
                     <xsl:with-param name="fontweight">
304
                        <xsl:choose>
305
                           <xsl:when test="@fontweight">
306
                              <xsl:value-of select="@fontweight"/>
307
                           </xsl:when>
308
                           <xsl:otherwise>
309
                              <xsl:value-of select="@mml:fontweight"/>
310
                           </xsl:otherwise>
311
                        </xsl:choose>
312
                     </xsl:with-param>
313
                     <xsl:with-param name="mathsize">
314
                        <xsl:choose>
315
                           <xsl:when test="@mathsize">
316
                              <xsl:value-of select="@mathsize"/>
317
                           </xsl:when>
318
                           <xsl:otherwise>
319
                              <xsl:value-of select="@mml:mathsize"/>
320
                           </xsl:otherwise>
321
                        </xsl:choose>
322
                     </xsl:with-param>
323
                     <xsl:with-param name="ndTokenFirst" select="."/>
324
                  </xsl:call-template>
325
               </xsl:when>
326
            </xsl:choose>
327
         </xsl:when>
328
         <xsl:otherwise>
329
        <!--Only one element will be part of run-->
330
        <r>
331
          <!--Create Run Properties based on current node's attributes-->
332
          <xsl:call-template name="CreateRunProp">
333
                  <xsl:with-param name="mathvariant">
334
                     <xsl:choose>
335
                        <xsl:when test="@mathvariant">
336
                           <xsl:value-of select="@mathvariant"/>
337
                        </xsl:when>
338
                        <xsl:otherwise>
339
                           <xsl:value-of select="@mml:mathvariant"/>
340
                        </xsl:otherwise>
341
                     </xsl:choose>
342
                  </xsl:with-param>
343
                  <xsl:with-param name="fontstyle">
344
                     <xsl:choose>
345
                        <xsl:when test="@fontstyle">
346
                           <xsl:value-of select="@fontstyle"/>
347
                        </xsl:when>
348
                        <xsl:otherwise>
349
                           <xsl:value-of select="@mml:fontstyle"/>
350
                        </xsl:otherwise>
351
                     </xsl:choose>
352
                  </xsl:with-param>
353
                  <xsl:with-param name="fontweight">
354
                     <xsl:choose>
355
                        <xsl:when test="@fontweight">
356
                           <xsl:value-of select="@fontweight"/>
357
                        </xsl:when>
358
                        <xsl:otherwise>
359
                           <xsl:value-of select="@mml:fontweight"/>
360
                        </xsl:otherwise>
361
                     </xsl:choose>
362
                  </xsl:with-param>
363
                  <xsl:with-param name="mathcolor">
364
                     <xsl:choose>
365
                        <xsl:when test="@mathcolor">
366
                           <xsl:value-of select="@mathcolor"/>
367
                        </xsl:when>
368
                        <xsl:otherwise>
369
                           <xsl:value-of select="@mml:mathcolor"/>
370
                        </xsl:otherwise>
371
                     </xsl:choose>
372
                  </xsl:with-param>
373
                  <xsl:with-param name="mathsize">
374
                     <xsl:choose>
375
                        <xsl:when test="@mathsize">
376
                           <xsl:value-of select="@mathsize"/>
377
                        </xsl:when>
378
                        <xsl:otherwise>
379
                           <xsl:value-of select="@mml:mathsize"/>
380
                        </xsl:otherwise>
381
                     </xsl:choose>
382
                  </xsl:with-param>
383
                  <xsl:with-param name="color">
384
                     <xsl:choose>
385
                        <xsl:when test="@color">
386
                           <xsl:value-of select="@color"/>
387
                        </xsl:when>
388
                        <xsl:otherwise>
389
                           <xsl:value-of select="@mml:color"/>
390
                        </xsl:otherwise>
391
                     </xsl:choose>
392
                  </xsl:with-param>
393
                  <xsl:with-param name="fontsize">
394
                     <xsl:choose>
395
                        <xsl:when test="@fontsize">
396
                           <xsl:value-of select="@fontsize"/>
397
                        </xsl:when>
398
                        <xsl:otherwise>
399
                           <xsl:value-of select="@mml:fontsize"/>
400
                        </xsl:otherwise>
401
                     </xsl:choose>
402
                  </xsl:with-param>
403
                  <xsl:with-param name="ndCur" select="."/>
404
                  <xsl:with-param name="fNor">
405
                     <xsl:call-template name="FNor">
406
                        <xsl:with-param name="ndCur" select="."/>
407
                     </xsl:call-template>
408
                  </xsl:with-param>
409
                  <xsl:with-param name="fLit">
410
                     <xsl:call-template name="FLit">
411
                        <xsl:with-param name="ndCur" select="."/>
412
                     </xsl:call-template>
413
                  </xsl:with-param>
414
               </xsl:call-template>
415
               <t>
416
                  <xsl:call-template name="OutputText">
417
                     <xsl:with-param name="sInput" select="normalize-space(.)"/>
418
                  </xsl:call-template>
419
               </t>
420
            </r>
421
         </xsl:otherwise>
422
      </xsl:choose>
423
  </xsl:template>
424

    
425
  <!-- %%Template: CreateRunWithSameProp
426
	-->
427
  <xsl:template name="CreateRunWithSameProp">
428
      <xsl:param name="mathbackground"/>
429
      <xsl:param name="mathcolor"/>
430
      <xsl:param name="mathvariant"/>
431
      <xsl:param name="color"/>
432
      <xsl:param name="font-family"/>
433
      <xsl:param name="fontsize"/>
434
      <xsl:param name="fontstyle"/>
435
      <xsl:param name="fontweight"/>
436
      <xsl:param name="mathsize"/>
437
      <xsl:param name="ndTokenFirst"/>
438

    
439
      <!--Given mathcolor, color, mstyle's (ancestor) color, and precedence of 
440
			said attributes, determine the actual color of the current run-->
441
    <xsl:variable name="sColorPropCur">
442
         <xsl:choose>
443
            <xsl:when test="$mathcolor!=''">
444
               <xsl:value-of select="$mathcolor"/>
445
            </xsl:when>
446
            <xsl:when test="$color!=''">
447
               <xsl:value-of select="$color"/>
448
            </xsl:when>
449
            <xsl:when test="$ndTokenFirst/ancestor::mml:mstyle[@color][1]/@color!=''">
450
               <xsl:value-of select="$ndTokenFirst/ancestor::mml:mstyle[@color][1]/@color"/>
451
            </xsl:when>
452
            <xsl:when test="$ndTokenFirst/ancestor::mml:mstyle[@mml:color][1]/@mml:color!=''">
453
               <xsl:value-of select="$ndTokenFirst/ancestor::mml:mstyle[@color][1]/@mml:color"/>
454
            </xsl:when>
455
            <xsl:otherwise>
456
               <xsl:value-of select="''"/>
457
            </xsl:otherwise>
458
         </xsl:choose>
459
      </xsl:variable>
460

    
461
      <!--Given mathsize, and fontsize and precedence of said attributes, 
462
			determine the actual font size of the current run-->
463
    <xsl:variable name="sSzCur">
464
         <xsl:choose>
465
            <xsl:when test="$mathsize!=''">
466
               <xsl:value-of select="$mathsize"/>
467
            </xsl:when>
468
            <xsl:when test="$fontsize!=''">
469
               <xsl:value-of select="$fontsize"/>
470
            </xsl:when>
471
            <xsl:otherwise>
472
               <xsl:value-of select="''"/>
473
            </xsl:otherwise>
474
         </xsl:choose>
475
      </xsl:variable>
476

    
477
      <!--Given mathvariant, fontstyle, and fontweight, and precedence of 
478
			the attributes, determine the actual font of the current run-->
479
    <xsl:variable name="sFontCur">
480
         <xsl:call-template name="GetFontCur">
481
            <xsl:with-param name="mathvariant" select="$mathvariant"/>
482
            <xsl:with-param name="fontstyle" select="$fontstyle"/>
483
            <xsl:with-param name="fontweight" select="$fontweight"/>
484
            <xsl:with-param name="ndCur" select="$ndTokenFirst"/>
485
         </xsl:call-template>
486
      </xsl:variable>
487

    
488
      <!-- The omml equivalent structure for mml:mtext is an omml run with the run property m:nor (normal) set.
489
         Therefore, we can only collect mtexts with  other mtext elements.  Suppose the $ndTokenFirst is an 
490
         mml:mtext, then if any of its following siblings are to be grouped, they must also be mml:text elements.  
491
         The inverse is also true, suppose the $ndTokenFirst isn't an mml:mtext, then if any of its following siblings 
492
         are to be grouped with $ndTokenFirst, they can't be mml:mtext elements-->
493
    <xsl:variable name="fNdTokenFirstIsMText">
494
         <xsl:choose>
495
            <xsl:when test="$ndTokenFirst/self::mml:mtext">1</xsl:when>
496
            <xsl:otherwise>0</xsl:otherwise>
497
         </xsl:choose>
498
      </xsl:variable>
499

    
500
      <!--In order to determine the length of the run, we will find the number of nodes before the inital node in the run and
501
			the number of nodes before the first node that DOES NOT belong to the current run.  The number of nodes that will
502
			be printed is One Less than the difference between the latter and the former-->
503

    
504
    <!--Find index of current node-->
505
    <xsl:variable name="nndBeforeFirst" select="count($ndTokenFirst/preceding-sibling::*)"/>
506

    
507
      <!--Find index of next change in run properties.
508
		
509
		    The basic idea is that we want to find the position of the last node in the longest 
510
				sequence of nodes, starting from ndTokenFirst, that can be grouped into a run.  For
511
				example, nodes A and B can be grouped together into the same run iff they have the same 
512
				props.
513
				
514
				To accomplish this grouping, we want to find the next sibling to ndTokenFirst that shouldn't be 
515
				included in the run of text.  We do this by counting the number of elements that precede the first
516
				such element that doesn't belong.  The xpath that accomplishes this is below.
517
				    
518
						Count the number of siblings the precede the first element after ndTokenFirst that shouldn't belong.
519
						count($ndTokenFirst/following-sibling::*[ . . . ][1]/preceding-sibling::*)
520
						
521
				Now, the hard part to this is what is represented by the '. . .' above.  This conditional expression is 
522
				defining what elements *don't* belong to the current run.  The conditions are as follows:
523
				
524
				The element is not a token element (mi, mn, mo, ms, or mtext)
525
				
526
				or
527
				
528
				The token element contains a glyph child (this is handled separately).
529
				
530
				or
531
				
532
				The token is an mtext and the run didn't start with an mtext, or the token isn't an mtext and the run started 
533
				with an mtext.  We do this check because mtext transforms into an omml m:nor property, and thus, these mtext
534
				token elements need to be grouped separately from other token elements.
535
				
536
				// We do an or not( . . . ), because it was easier to define what token elements match than how they don't match.
537
				// Thus, this inner '. . .' defines how token attributes equate to one another.  We add the 'not' outside of to accomplish
538
				// the goal of the outer '. . .', which is the find the next element that *doesn't* match.
539
				or not(
540
				   The background colors match.
541
					 
542
					 and
543
					 
544
							The current font (sFontCur) matches the mathvariant
545
					 
546
							or
547
							
548
							sFontCur is normal and matches the current font characteristics
549
							
550
							or 
551
							
552
							sFontCur is italic and matches the current font characteristics
553
							
554
							or 
555
							
556
							. . .
557
				
558
					 and
559
					 
560
					 The font family matches the current font family.
561
					 ) // end of not().-->
562
    <xsl:variable name="nndBeforeLim"
563
                    select="count($ndTokenFirst/following-sibling::*      [(not(self::mml:mi) and not(self::mml:mn) and not(self::mml:mo) and not(self::mml:ms) and not(self::mml:mtext))      or      (self::mml:mi[child::mml:mglyph] or self::mml:mn[child::mml:mglyph] or self::mml:mo[child::mml:mglyph] or self::mml:ms[child::mml:mglyph] or self::mml:mtext[child::mml:mglyph])      or      (($fNdTokenFirstIsMText=1 and not(self::mml:mtext)) or ($fNdTokenFirstIsMText=0 and self::mml:mtext))      or        not(       ((($sFontCur=@mathvariant or $sFontCur=@mml:mathvariant)        or        ($sFontCur='normal'         and ((@mathvariant='normal' or @mml:mathvariant='normal')            or (((not(@mathvariant) or @mathvariant='') and (not(@mml:mathvariant) or @mml:mathvariant=''))              and (                     ((@fontstyle='normal' or @mml:fontstyle='normal') and (not(@fontweight='bold') and not(@mml:fontweight='bold')))                     or (self::mml:mi and string-length(normalize-space(.)) &gt; 1)                    )               )           )        )        or        ($sFontCur='italic'          and ((@mathvariant='italic' or @mml:mathvariant='italic')            or (((not(@mathvariant) or @mathvariant='') and (not(@mml:mathvariant) or @mml:mathvariant=''))             and (                     ((@fontstyle='italic' or @mml:fontstyle='italic') and (not(@fontweight='bold') and not(@mml:fontweight='bold')))                    or                  (self::mml:mn                  or self::mml:mo                 or (self::mml:mi and string-length(normalize-space(.)) &lt;= 1))                    )               )           )        )         or        ($sFontCur='bold'         and ((@mathvariant='bold' or @mml:mathvariant='bold')            or (((not(@mathvariant) or @mathvariant='') and (not(@mml:mathvariant) or @mml:mathvariant=''))                and (                     ((@fontweight='bold' or @mml:fontweight='bold')                     and ((@fontstyle='normal' or @mml:fontstyle='normal') or (self::mml:mi and string-length(normalize-space(.)) &lt;= 1))                    )               )             )             )        )         or        (($sFontCur='bi' or $sFontCur='bold-italic')         and (            (@mathvariant='bold-italic' or @mml:mathvariant='bold-italic')            or (((not(@mathvariant) or @mathvariant='') and (not(@mml:mathvariant) or @mml:mathvariant=''))             and (                ((@fontweight='bold' or @mml:fontweight='bold') and (@fontstyle='italic' or @mml:fontstyle='italic'))                or ((@fontweight='bold' or @mml:fontweight='bold')                   and (self::mml:mn                       or self::mml:mo                     or (self::mml:mi and string-length(normalize-space(.)) &lt;= 1)))                    )               )           )        )               or               (($sFontCur=''                   and (                      ((not(@mathvariant) or @mathvariant='')                         and (not(@mml:mathvariant) or @mml:mathvariant='')                         and (not(@fontstyle) or @fontstyle='')                         and (not(@mml:fontstyle) or @mml:fontstyle='')                         and (not(@fontweight)or @fontweight='')                         and (not(@mml:fontweight) or @mml:fontweight='')                 )                        or                          (@mathvariant='italic' or @mml:mathvariant='italic')                         or (                            ((not(@mathvariant) or @mathvariant='') and (not(@mml:mathvariant) or @mml:mathvariant=''))                                and (                                   (((@fontweight='normal' or @mml:fontweight='normal')                                    and (@fontstyle='italic' or @mml:fontstyle='italic'))                                   )                                   or                                   ((not(@fontweight) or @fontweight='') and (not(@mml:fontweight) or @mml:fontweight=''))                                    and (@fontstyle='italic' or @mml:fontstyle='italic')                                   or                                   ((not(@fontweight) or @fontweight='') and (not(@mml:fontweight) or @mml:fontweight=''))                                    and (not(@fontstyle) or @fontstyle='')                                    and (not(@mml:fontstyle) or @mml:fontstyle=''))                             )                 )                ))        or               ($sFontCur='normal'                 and ((self::mml:mi                       and (not(@mathvariant) or @mathvariant='')                   and (not(@mml:mathvariant) or @mml:mathvariant)                   and (not(@fontstyle) or @fontstyle='')                    and (not(@mml:fontstyle) or @mml:fontstyle='')                   and (not(@fontweight) or @fontweight='')                    and (not(@mml:fontweight) or @mml:fontweight='')                   and (string-length(normalize-space(.)) &gt; 1)                   )                  or ((self::mml:ms or self::mml:mtext)                    and (not(@mathvariant) or @mathvariant='')                   and (not(@mml:mathvariant) or @mml:mathvariant)                   and (not(@fontstyle) or @fontstyle)                   and (not(@fontstyle) or @fontstyle='')                   and (not(@fontweight) or @fontweight)                   and (not(@mml:fontweight) or @mml:fontweight='')                   )                  )               )           )       and             (($font-family = @font-family or $font-family = @mml:font-family)               or (($font-family='' or not($font-family))                and (not(@font-family) or @font-family='')                and (not(@mml:font-family) or @mml:font-family='')               )              )      ))      ][1]/preceding-sibling::*)"/>
564

    
565
      <xsl:variable name="cndRun" select="$nndBeforeLim - $nndBeforeFirst"/>
566

    
567
      <!--Contiguous groups of like-property mi, mn, and mo's are separated by non- mi, mn, mo tags, or mi,mn, or mo
568
			tags with different properties.  nndBeforeLim is the number of nodes before the next tag which separates contiguous 
569
			groups of like-property mi, mn, and mo's.  Knowing this delimiting tag allows for the aggregation of the correct 
570
			number of mi, mn, and mo tags.-->
571
    <r>
572

    
573
      <!--The beginning and ending of the current run has been established. Now we should open a run element-->
574
      <xsl:choose>
575

    
576
        <!--If cndRun > 0, then there is a following diffrent prop, or non- Token, 
577
						although there may or may not have been a preceding different prop, or non-
578
						Token-->
579
        <xsl:when test="$cndRun &gt; 0">
580
               <xsl:call-template name="CreateRunProp">
581
                  <xsl:with-param name="mathvariant" select="$mathvariant"/>
582
                  <xsl:with-param name="fontstyle" select="$fontstyle"/>
583
                  <xsl:with-param name="fontweight" select="$fontweight"/>
584
                  <xsl:with-param name="mathcolor" select="$mathcolor"/>
585
                  <xsl:with-param name="mathsize" select="$mathsize"/>
586
                  <xsl:with-param name="color" select="$color"/>
587
                  <xsl:with-param name="fontsize" select="$fontsize"/>
588
                  <xsl:with-param name="ndCur" select="$ndTokenFirst"/>
589
                  <xsl:with-param name="fNor">
590
                     <xsl:call-template name="FNor">
591
                        <xsl:with-param name="ndCur" select="$ndTokenFirst"/>
592
                     </xsl:call-template>
593
                  </xsl:with-param>
594
                  <xsl:with-param name="fLit">
595
                     <xsl:call-template name="FLit">
596
                        <xsl:with-param name="ndCur" select="$ndTokenFirst"/>
597
                     </xsl:call-template>
598
                  </xsl:with-param>
599
               </xsl:call-template>
600
               <t>
601
                  <xsl:call-template name="OutputText">
602
                     <xsl:with-param name="sInput">
603
                        <xsl:choose>
604
                           <xsl:when test="namespace-uri($ndTokenFirst) = 'http://www.w3.org/1998/Math/MathML' and local-name($ndTokenFirst) = 'ms'">
605
                              <xsl:call-template name="OutputMs">
606
                                 <xsl:with-param name="msCur" select="$ndTokenFirst"/>
607
                              </xsl:call-template>
608
                           </xsl:when>
609
                           <xsl:otherwise>
610
                              <xsl:value-of select="normalize-space($ndTokenFirst)"/>
611
                           </xsl:otherwise>
612
                        </xsl:choose>
613
                        <xsl:for-each select="$ndTokenFirst/following-sibling::*[position() &lt; $cndRun]">
614
                           <xsl:choose>
615
                              <xsl:when test="namespace-uri(.) = 'http://www.w3.org/1998/Math/MathML' and               local-name(.) = 'ms'">
616
                                 <xsl:call-template name="OutputMs">
617
                                    <xsl:with-param name="msCur" select="."/>
618
                                 </xsl:call-template>
619
                              </xsl:when>
620
                              <xsl:otherwise>
621
                                 <xsl:value-of select="normalize-space(.)"/>
622
                              </xsl:otherwise>
623
                           </xsl:choose>
624
                        </xsl:for-each>
625
                     </xsl:with-param>
626
                  </xsl:call-template>
627
               </t>
628
            </xsl:when>
629
            <xsl:otherwise>
630

    
631
          <!--if cndRun lt;= 0, then iNextNonToken = 0, 
632
						and iPrecNonToken gt;= 0.  In either case, b/c there 
633
						is no next different property or non-Token 
634
						(which is implied by the nndBeforeLast being equal to 0) 
635
						you can put all the remaining mi, mn, and mo's into one 
636
						group.-->
637
          <xsl:call-template name="CreateRunProp">
638
                  <xsl:with-param name="mathvariant" select="$mathvariant"/>
639
                  <xsl:with-param name="fontstyle" select="$fontstyle"/>
640
                  <xsl:with-param name="fontweight" select="$fontweight"/>
641
                  <xsl:with-param name="mathcolor" select="$mathcolor"/>
642
                  <xsl:with-param name="mathsize" select="$mathsize"/>
643
                  <xsl:with-param name="color" select="$color"/>
644
                  <xsl:with-param name="fontsize" select="$fontsize"/>
645
                  <xsl:with-param name="ndCur" select="$ndTokenFirst"/>
646
                  <xsl:with-param name="fNor">
647
                     <xsl:call-template name="FNor">
648
                        <xsl:with-param name="ndCur" select="$ndTokenFirst"/>
649
                     </xsl:call-template>
650
                  </xsl:with-param>
651
                  <xsl:with-param name="fLit">
652
                     <xsl:call-template name="FLit">
653
                        <xsl:with-param name="ndCur" select="$ndTokenFirst"/>
654
                     </xsl:call-template>
655
                  </xsl:with-param>
656
               </xsl:call-template>
657
               <t>
658

    
659
            <!--Create the Run, first output current, then in a 
660
							for-each, because all the following siblings are
661
							mn, mi, and mo's that conform to the run's properties,
662
							group them together-->
663
            <xsl:call-template name="OutputText">
664
                     <xsl:with-param name="sInput">
665
                        <xsl:choose>
666
                           <xsl:when test="namespace-uri($ndTokenFirst) = 'http://www.w3.org/1998/Math/MathML' and               local-name($ndTokenFirst) = 'ms'">
667
                              <xsl:call-template name="OutputMs">
668
                                 <xsl:with-param name="msCur" select="$ndTokenFirst"/>
669
                              </xsl:call-template>
670
                           </xsl:when>
671
                           <xsl:otherwise>
672
                              <xsl:value-of select="normalize-space($ndTokenFirst)"/>
673
                           </xsl:otherwise>
674
                        </xsl:choose>
675
                        <xsl:for-each select="$ndTokenFirst/following-sibling::*[self::mml:mi or self::mml:mn or self::mml:mo or self::mml:ms or self::mml:mtext]">
676
                           <xsl:choose>
677
                              <xsl:when test="namespace-uri(.) = 'http://www.w3.org/1998/Math/MathML' and               local-name(.) = 'ms'">
678
                                 <xsl:call-template name="OutputMs">
679
                                    <xsl:with-param name="msCur" select="."/>
680
                                 </xsl:call-template>
681
                              </xsl:when>
682
                              <xsl:otherwise>
683
                                 <xsl:value-of select="normalize-space(.)"/>
684
                              </xsl:otherwise>
685
                           </xsl:choose>
686
                        </xsl:for-each>
687
                     </xsl:with-param>
688
                  </xsl:call-template>
689
               </t>
690
            </xsl:otherwise>
691
         </xsl:choose>
692
      </r>
693

    
694
      <!--The run was terminated by an mi, mn, mo, ms, or mtext with different properties, 
695
				therefore, call-template CreateRunWithSameProp, using cndRun+1 node as new start node-->
696
    <xsl:if test="$nndBeforeLim!=0             and ($ndTokenFirst/following-sibling::*[$cndRun]/self::mml:mi or             $ndTokenFirst/following-sibling::*[$cndRun]/self::mml:mn or            $ndTokenFirst/following-sibling::*[$cndRun]/self::mml:mo or            $ndTokenFirst/following-sibling::*[$cndRun]/self::mml:ms or                 $ndTokenFirst/following-sibling::*[$cndRun]/self::mml:mtext)              and (count($ndTokenFirst/following-sibling::*[$cndRun]/mml:mglyph) = 0)">
697
         <xsl:call-template name="CreateRunWithSameProp">
698
            <xsl:with-param name="mathbackground">
699
               <xsl:choose>
700
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@mathbackground">
701
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mathbackground"/>
702
                  </xsl:when>
703
                  <xsl:otherwise>
704
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:mathbackground"/>
705
                  </xsl:otherwise>
706
               </xsl:choose>
707
            </xsl:with-param>
708
            <xsl:with-param name="mathcolor">
709
               <xsl:choose>
710
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@mathcolor">
711
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mathcolor"/>
712
                  </xsl:when>
713
                  <xsl:otherwise>
714
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:mathcolor"/>
715
                  </xsl:otherwise>
716
               </xsl:choose>
717
            </xsl:with-param>
718
            <xsl:with-param name="mathvariant">
719
               <xsl:choose>
720
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@mathvariant">
721
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mathvariant"/>
722
                  </xsl:when>
723
                  <xsl:otherwise>
724
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:mathvariant"/>
725
                  </xsl:otherwise>
726
               </xsl:choose>
727
            </xsl:with-param>
728
            <xsl:with-param name="color">
729
               <xsl:choose>
730
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@color">
731
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@color"/>
732
                  </xsl:when>
733
                  <xsl:otherwise>
734
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:color"/>
735
                  </xsl:otherwise>
736
               </xsl:choose>
737
            </xsl:with-param>
738
            <xsl:with-param name="font-family">
739
               <xsl:choose>
740
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@font-family">
741
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@font-family"/>
742
                  </xsl:when>
743
                  <xsl:otherwise>
744
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:font-family"/>
745
                  </xsl:otherwise>
746
               </xsl:choose>
747
            </xsl:with-param>
748
            <xsl:with-param name="fontsize">
749
               <xsl:choose>
750
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@fontsize">
751
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@fontsize"/>
752
                  </xsl:when>
753
                  <xsl:otherwise>
754
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:fontsize"/>
755
                  </xsl:otherwise>
756
               </xsl:choose>
757
            </xsl:with-param>
758
            <xsl:with-param name="fontstyle">
759
               <xsl:choose>
760
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@fontstyle">
761
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@fontstyle"/>
762
                  </xsl:when>
763
                  <xsl:otherwise>
764
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:fontstyle"/>
765
                  </xsl:otherwise>
766
               </xsl:choose>
767
            </xsl:with-param>
768
            <xsl:with-param name="fontweight">
769
               <xsl:choose>
770
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@fontweight">
771
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@fontweight"/>
772
                  </xsl:when>
773
                  <xsl:otherwise>
774
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:fontweight"/>
775
                  </xsl:otherwise>
776
               </xsl:choose>
777
            </xsl:with-param>
778
            <xsl:with-param name="mathsize">
779
               <xsl:choose>
780
                  <xsl:when test="$ndTokenFirst/following-sibling::*[$cndRun]/@mathsize">
781
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mathsize"/>
782
                  </xsl:when>
783
                  <xsl:otherwise>
784
                     <xsl:value-of select="$ndTokenFirst/following-sibling::*[$cndRun]/@mml:mathsize"/>
785
                  </xsl:otherwise>
786
               </xsl:choose>
787
            </xsl:with-param>
788
            <xsl:with-param name="ndTokenFirst" select="$ndTokenFirst/following-sibling::*[$cndRun]"/>
789
         </xsl:call-template>
790
      </xsl:if>
791
  </xsl:template>
792

    
793
  <!-- %%Template: FNor
794
				 Given the context of ndCur, determine if ndCur should be omml's normal style.
795
	-->
796
  <xsl:template name="FNor">
797
      <xsl:param name="ndCur" select="."/>
798
      <xsl:choose>
799
      <!-- Is the current node an mml:mtext, or if this is an mglyph whose parent is 
800
             an mml:mtext. -->
801
      <xsl:when test="$ndCur/self::mml:mtext or ($ndCur/self::mml:mglyph and parent::mml:mtext)">1</xsl:when>
802
         <xsl:otherwise>0</xsl:otherwise>
803
      </xsl:choose>
804
  </xsl:template>
805

    
806
  <!-- %%Template: FLit
807
				 Given the context of ndCur, determine if ndCur should have the 
808
				 run omml property of lit (literal, no build up).
809
	-->
810
  <xsl:template name="FLit">
811
      <xsl:param name="ndCur" select="."/>
812
      <xsl:variable name="sLowerActiontype">
813
         <xsl:choose>
814
            <xsl:when test="$ndCur/ancestor::mml:maction[@actiontype='lit']/@actiontype">
815
               <xsl:value-of select="translate($ndCur/ancestor::mml:maction[@actiontype='lit']/@actiontype, $StrUCAlphabet,                                                                                                       $StrLCAlphabet)"/>
816
            </xsl:when>
817
            <xsl:otherwise>
818
               <xsl:value-of select="translate($ndCur/ancestor::mml:maction[@actiontype='lit']/@mml:actiontype, $StrUCAlphabet,                                                                                                       $StrLCAlphabet)"/>
819
            </xsl:otherwise>
820
         </xsl:choose>
821
      </xsl:variable>
822
      <xsl:choose>
823
         <xsl:when test="$sLowerActiontype='lit'">1</xsl:when>
824
         <xsl:otherwise>0</xsl:otherwise>
825
      </xsl:choose>
826
  </xsl:template>
827

    
828
  <!-- %%Template: CreateRunProp
829
	-->
830
  <xsl:template name="CreateRunProp">
831
      <xsl:param name="mathbackground"/>
832
      <xsl:param name="mathcolor"/>
833
      <xsl:param name="mathvariant"/>
834
      <xsl:param name="color"/>
835
      <xsl:param name="font-family">Cambria Math</xsl:param>
836
      <xsl:param name="fontsize"/>
837
      <xsl:param name="fontstyle"/>
838
      <xsl:param name="fontweight"/>
839
      <xsl:param name="mathsize"/>
840
      <xsl:param name="ndCur"/>
841
      <xsl:param name="fNor"/>
842
      <xsl:param name="fLit"/>
843
      <w:rPr>
844
	<xsl:call-template name="mathrRpHook"/>
845
         <w:rFonts w:ascii="{$font-family}" w:eastAsia="{$font-family}" w:hAnsi="{$font-family}"
846
                   w:cs="{$font-family}"/>
847
	 <xsl:choose>
848
	   <xsl:when test="$fontweight=''"/>
849
	   <xsl:when test="$fontweight='bold'">
850
	     <w:b/>
851
	   </xsl:when>
852
	   <xsl:when test="$fontweight='normal'">
853
	     <w:b w:val="0"/>
854
	   </xsl:when>
855
	 </xsl:choose>
856
	 <xsl:choose>
857
	   <xsl:when test="$fontstyle=''"/>
858
	   <xsl:when test="$fontstyle='italic'">
859
	     <w:i/>
860
	   </xsl:when>
861
	   <xsl:when test="$fontstyle='normal'">
862
	     <w:i w:val="0"/>
863
	   </xsl:when>
864
	 </xsl:choose>
865
      </w:rPr>
866
      <xsl:variable name="mstyleColor">
867
         <xsl:if test="not(not($ndCur))">
868
            <xsl:choose>
869
               <xsl:when test="$ndCur/ancestor::mml:mstyle[@color][1]/@color">
870
                  <xsl:value-of select="$ndCur/ancestor::mml:mstyle[@color][1]/@color"/>
871
               </xsl:when>
872
               <xsl:otherwise>
873
                  <xsl:value-of select="$ndCur/ancestor::mml:mstyle[@color][1]/@mml:color"/>
874
               </xsl:otherwise>
875
            </xsl:choose>
876
         </xsl:if>
877
      </xsl:variable>
878
      <xsl:call-template name="CreateMathRPR">
879
         <xsl:with-param name="mathvariant" select="$mathvariant"/>
880
         <xsl:with-param name="fontstyle" select="$fontstyle"/>
881
         <xsl:with-param name="fontweight" select="$fontweight"/>
882
         <xsl:with-param name="ndCur" select="$ndCur"/>
883
         <xsl:with-param name="fNor" select="$fNor"/>
884
         <xsl:with-param name="fLit" select="$fLit"/>
885
      </xsl:call-template>
886
  </xsl:template>
887

    
888
  <!-- %%Template: CreateMathRPR
889
	-->
890
  <xsl:template name="CreateMathRPR">
891
      <xsl:param name="mathvariant"/>
892
      <xsl:param name="fontstyle"/>
893
      <xsl:param name="fontweight"/>
894
      <xsl:param name="ndCur"/>
895
      <xsl:param name="fNor"/>
896
      <xsl:param name="fLit"/>
897
      <xsl:variable name="sFontCur">
898
         <xsl:call-template name="GetFontCur">
899
            <xsl:with-param name="mathvariant" select="$mathvariant"/>
900
            <xsl:with-param name="fontstyle" select="$fontstyle"/>
901
            <xsl:with-param name="fontweight" select="$fontweight"/>
902
            <xsl:with-param name="ndCur" select="$ndCur"/>
903
         </xsl:call-template>
904
      </xsl:variable>
905
      <xsl:if test="$fLit=1 or $fNor=1 or ($sFontCur!='italic' and $sFontCur!='')">
906
         <rPr>
907
            <xsl:if test="$fNor=1">
908
               <nor/>
909
            </xsl:if>
910
            <xsl:if test="$fLit=1">
911
               <lit/>
912
            </xsl:if>
913
            <xsl:call-template name="CreateMathScrStyProp">
914
               <xsl:with-param name="font" select="$sFontCur"/>
915
               <xsl:with-param name="fNor" select="$fNor"/>
916
            </xsl:call-template>
917
         </rPr>
918
      </xsl:if>
919
  </xsl:template>
920

    
921
  <!-- %%Template: GetFontCur
922
	-->
923
  <xsl:template name="GetFontCur">
924
      <xsl:param name="ndCur"/>
925
      <xsl:param name="mathvariant"/>
926
      <xsl:param name="fontstyle"/>
927
      <xsl:param name="fontweight"/>
928
      <xsl:choose>
929
         <xsl:when test="$mathvariant!=''">
930
            <xsl:value-of select="$mathvariant"/>
931
         </xsl:when>
932
         <xsl:when test="not($ndCur)">
933
            <xsl:value-of select="'italic'"/>
934
         </xsl:when>
935
         <xsl:when test="$ndCur/self::mml:mi and (string-length(normalize-space($ndCur)) &lt;= 1)               or $ndCur/self::mml:mn and string(number($ndCur/text()))!='NaN'               or $ndCur/self::mml:mo">
936

    
937
	   <!-- The default for the above three cases is fontstyle=italic fontweight=normal.-->
938
	   <xsl:choose>
939
	     <xsl:when test="$fontstyle='normal' and $fontweight='bold'">
940
	       <!-- In omml, a sty of 'b' (which is what bold is translated into)
941
		    implies a normal fontstyle -->
942
	       <xsl:value-of select="'bold'"/>
943
	     </xsl:when>
944
	     <xsl:when test="$fontstyle='normal'">
945
	       <xsl:value-of select="'normal'"/>
946
	     </xsl:when>
947
	     <xsl:when test="$fontweight='bold'">
948
	       <xsl:value-of select="'bi'"/>
949
	     </xsl:when>
950
	     <xsl:when test="$fontweight='normal'">
951
	       <xsl:value-of select="'i'"/>
952
	     </xsl:when>
953
	     <xsl:otherwise>
954
	       <xsl:value-of select="'italic'"/>
955
	     </xsl:otherwise>
956
	   </xsl:choose>
957
	 </xsl:when>
958
	 <xsl:otherwise>
959
	   <!--Default is fontweight = 'normal' and fontstyle='normal'-->
960
        <xsl:choose>
961
	  <xsl:when test="$fontstyle='italic' and $fontweight='bold'">
962
	    <xsl:value-of select="'bi'"/>
963
	  </xsl:when>
964
	  <xsl:when test="$fontstyle='italic'">
965
	    <xsl:value-of select="'italic'"/>
966
	  </xsl:when>
967
	     <xsl:when test="$fontweight='normal'">
968
	       <xsl:value-of select="'italic'"/>
969
	     </xsl:when>
970
	  <xsl:when test="$fontweight='bold'">
971
	    <xsl:value-of select="'bold'"/>
972
	  </xsl:when>
973
	  <xsl:otherwise>
974
	    <xsl:value-of select="'normal'"/>
975
	  </xsl:otherwise>
976
            </xsl:choose>
977
         </xsl:otherwise>
978
      </xsl:choose>
979
  </xsl:template>
980

    
981

    
982
  <!-- %%Template: CreateMathScrStyProp
983
	-->
984
  <xsl:template name="CreateMathScrStyProp">
985
      <xsl:param name="font"/>
986
      <xsl:param name="fNor" select="0"/>
987
      <xsl:choose>
988
         <xsl:when test="$font='normal' and $fNor=0">
989
            <sty>
990
               <xsl:attribute name="m:val">p</xsl:attribute>
991
            </sty>
992
         </xsl:when>
993
         <xsl:when test="$font='bold'">
994
            <sty>
995
               <xsl:attribute name="m:val">b</xsl:attribute>
996
            </sty>
997
         </xsl:when>
998
         <xsl:when test="$font='italic'">
999
      </xsl:when>
1000
         <xsl:when test="$font='script'">
1001
            <scr>
1002
               <xsl:attribute name="m:val">script</xsl:attribute>
1003
            </scr>
1004
         </xsl:when>
1005
         <xsl:when test="$font='bold-script'">
1006
            <scr>
1007
               <xsl:attribute name="m:val">script</xsl:attribute>
1008
            </scr>
1009
            <sty>
1010
               <xsl:attribute name="m:val">b</xsl:attribute>
1011
            </sty>
1012
         </xsl:when>
1013
         <xsl:when test="$font='double-struck'">
1014
            <scr>
1015
               <xsl:attribute name="m:val">double-struck</xsl:attribute>
1016
            </scr>
1017
            <sty>
1018
               <xsl:attribute name="m:val">p</xsl:attribute>
1019
            </sty>
1020
         </xsl:when>
1021
         <xsl:when test="$font='fraktur'">
1022
            <scr>
1023
               <xsl:attribute name="m:val">fraktur</xsl:attribute>
1024
            </scr>
1025
            <sty>
1026
               <xsl:attribute name="m:val">p</xsl:attribute>
1027
            </sty>
1028
         </xsl:when>
1029
         <xsl:when test="$font='bold-fraktur'">
1030
            <scr>
1031
               <xsl:attribute name="m:val">fraktur</xsl:attribute>
1032
            </scr>
1033
            <sty>
1034
               <xsl:attribute name="m:val">b</xsl:attribute>
1035
            </sty>
1036
         </xsl:when>
1037
         <xsl:when test="$font='sans-serif'">
1038
            <scr>
1039
               <xsl:attribute name="m:val">sans-serif</xsl:attribute>
1040
            </scr>
1041
            <sty>
1042
               <xsl:attribute name="m:val">p</xsl:attribute>
1043
            </sty>
1044
         </xsl:when>
1045
         <xsl:when test="$font='bold-sans-serif'">
1046
            <scr>
1047
               <xsl:attribute name="m:val">sans-serif</xsl:attribute>
1048
            </scr>
1049
            <sty>
1050
               <xsl:attribute name="m:val">b</xsl:attribute>
1051
            </sty>
1052
         </xsl:when>
1053
         <xsl:when test="$font='sans-serif-italic'">
1054
            <scr>
1055
               <xsl:attribute name="m:val">sans-serif</xsl:attribute>
1056
            </scr>
1057
         </xsl:when>
1058
         <xsl:when test="$font='sans-serif-bold-italic'">
1059
            <scr>
1060
               <xsl:attribute name="m:val">sans-serif</xsl:attribute>
1061
            </scr>
1062
            <sty>
1063
               <xsl:attribute name="m:val">bi</xsl:attribute>
1064
            </sty>
1065
         </xsl:when>
1066
         <xsl:when test="$font='monospace'"/>
1067
         <!-- We can't do monospace, so leave empty -->
1068
      <xsl:when test="$font='bold'">
1069
            <sty>
1070
               <xsl:attribute name="m:val">b</xsl:attribute>
1071
            </sty>
1072
         </xsl:when>
1073
         <xsl:when test="$font='bi' or $font='bold-italic'">
1074
            <sty>
1075
               <xsl:attribute name="m:val">bi</xsl:attribute>
1076
            </sty>
1077
         </xsl:when>
1078
      </xsl:choose>
1079
  </xsl:template>
1080

    
1081
  <xsl:template name="FBar">
1082
      <xsl:param name="sLineThickness"/>
1083
      <xsl:variable name="sLowerLineThickness"
1084
                    select="translate($sLineThickness, $StrUCAlphabet, $StrLCAlphabet)"/>
1085
      <xsl:choose>
1086
         <xsl:when test="string-length($sLowerLineThickness)=0                       or $sLowerLineThickness='thin'                        or $sLowerLineThickness='medium'                        or $sLowerLineThickness='thick'">1</xsl:when>
1087
         <xsl:otherwise>
1088
            <xsl:variable name="fStrContainsNonZeroDigit">
1089
               <xsl:call-template name="FStrContainsNonZeroDigit">
1090
                  <xsl:with-param name="s" select="$sLowerLineThickness"/>
1091
               </xsl:call-template>
1092
            </xsl:variable>
1093
            <xsl:choose>
1094
               <xsl:when test="$fStrContainsNonZeroDigit=1">1</xsl:when>
1095
               <xsl:otherwise>0</xsl:otherwise>
1096
            </xsl:choose>
1097
         </xsl:otherwise>
1098
      </xsl:choose>
1099
  </xsl:template>
1100

    
1101

    
1102
  <!-- %%Template: match mfrac 
1103
		-->
1104
  <xsl:template mode="mml" match="mml:mfrac">
1105
      <xsl:variable name="fBar">
1106
         <xsl:call-template name="FBar">
1107
            <xsl:with-param name="sLineThickness">
1108
               <xsl:choose>
1109
                  <xsl:when test="@linethickness">
1110
                     <xsl:value-of select="@linethickness"/>
1111
                  </xsl:when>
1112
                  <xsl:otherwise>
1113
                     <xsl:value-of select="@mml:linethickness"/>
1114
                  </xsl:otherwise>
1115
               </xsl:choose>
1116
            </xsl:with-param>
1117
         </xsl:call-template>
1118
      </xsl:variable>
1119

    
1120
      <f>
1121
         <fPr>
1122
            <type>
1123
               <xsl:attribute name="m:val">
1124
                  <xsl:choose>
1125
                     <xsl:when test="$fBar=0">noBar</xsl:when>
1126
                     <xsl:when test="@bevelled='true' or @mml:bevelled='true'">skw</xsl:when>
1127
                     <xsl:otherwise>bar</xsl:otherwise>
1128
                  </xsl:choose>
1129
               </xsl:attribute>
1130
            </type>
1131
         </fPr>
1132
         <num>
1133
            <xsl:call-template name="CreateArgProp"/>
1134
            <xsl:apply-templates mode="mml" select="child::*[1]"/>
1135
         </num>
1136
         <den>
1137
            <xsl:call-template name="CreateArgProp"/>
1138
            <xsl:apply-templates mode="mml" select="child::*[2]"/>
1139
         </den>
1140
      </f>
1141
  </xsl:template>
1142

    
1143
  <!-- %%Template: match menclose msqrt
1144
	-->
1145
  <xsl:template mode="mml" match="mml:menclose | mml:msqrt">
1146
      <xsl:variable name="sLowerCaseNotation">
1147
         <xsl:choose>
1148
            <xsl:when test="@notation">
1149
               <xsl:value-of select="translate(@notation, $StrUCAlphabet, $StrLCAlphabet)"/>
1150
            </xsl:when>
1151
            <xsl:otherwise>
1152
               <xsl:value-of select="translate(@mml:notation, $StrUCAlphabet, $StrLCAlphabet)"/>
1153
            </xsl:otherwise>
1154
         </xsl:choose>
1155
      </xsl:variable>
1156
      <xsl:choose>
1157
      <!-- Take care of default -->
1158
      <xsl:when test="$sLowerCaseNotation='radical'                        or not($sLowerCaseNotation)                        or $sLowerCaseNotation=''                        or self::mml:msqrt">
1159
            <rad>
1160
               <radPr>
1161
                  <degHide>
1162
                     <xsl:attribute name="m:val">on</xsl:attribute>
1163
                  </degHide>
1164
               </radPr>
1165
               <deg>
1166
                  <xsl:call-template name="CreateArgProp"/>
1167
               </deg>
1168
               <e>
1169
                  <xsl:call-template name="CreateArgProp"/>
1170
                  <xsl:apply-templates mode="mml"/>
1171
               </e>
1172
            </rad>
1173
         </xsl:when>
1174
         <xsl:otherwise>
1175
            <xsl:choose>
1176
               <xsl:when test="$sLowerCaseNotation='actuarial' or $sLowerCaseNotation='longdiv'"/>
1177
               <xsl:otherwise>
1178
                  <borderBox>
1179
              <!-- Dealing with more complex notation attribute -->
1180
              <xsl:variable name="fBox">
1181
                        <xsl:choose>
1182
                  <!-- Word doesn't have circle and roundedbox concepts, therefore, map both to a 
1183
                       box. -->
1184
                  <xsl:when test="contains($sLowerCaseNotation, 'box')                                   or contains($sLowerCaseNotation, 'circle')                                   or contains($sLowerCaseNotation, 'roundedbox')">1</xsl:when>
1185
                           <xsl:otherwise>0</xsl:otherwise>
1186
                        </xsl:choose>
1187
                     </xsl:variable>
1188
                     <xsl:variable name="fTop">
1189
                        <xsl:choose>
1190
                           <xsl:when test="contains($sLowerCaseNotation, 'top')">1</xsl:when>
1191
                           <xsl:otherwise>0</xsl:otherwise>
1192
                        </xsl:choose>
1193
                     </xsl:variable>
1194
                     <xsl:variable name="fBot">
1195
                        <xsl:choose>
1196
                           <xsl:when test="contains($sLowerCaseNotation, 'bottom')">1</xsl:when>
1197
                           <xsl:otherwise>0</xsl:otherwise>
1198
                        </xsl:choose>
1199
                     </xsl:variable>
1200
                     <xsl:variable name="fLeft">
1201
                        <xsl:choose>
1202
                           <xsl:when test="contains($sLowerCaseNotation, 'left')">1</xsl:when>
1203
                           <xsl:otherwise>0</xsl:otherwise>
1204
                        </xsl:choose>
1205
                     </xsl:variable>
1206
                     <xsl:variable name="fRight">
1207
                        <xsl:choose>
1208
                           <xsl:when test="contains($sLowerCaseNotation, 'right')">1</xsl:when>
1209
                           <xsl:otherwise>0</xsl:otherwise>
1210
                        </xsl:choose>
1211
                     </xsl:variable>
1212
                     <xsl:variable name="fStrikeH">
1213
                        <xsl:choose>
1214
                           <xsl:when test="contains($sLowerCaseNotation, 'horizontalstrike')">1</xsl:when>
1215
                           <xsl:otherwise>0</xsl:otherwise>
1216
                        </xsl:choose>
1217
                     </xsl:variable>
1218
                     <xsl:variable name="fStrikeV">
1219
                        <xsl:choose>
1220
                           <xsl:when test="contains($sLowerCaseNotation, 'verticalstrike')">1</xsl:when>
1221
                           <xsl:otherwise>0</xsl:otherwise>
1222
                        </xsl:choose>
1223
                     </xsl:variable>
1224
                     <xsl:variable name="fStrikeBLTR">
1225
                        <xsl:choose>
1226
                           <xsl:when test="contains($sLowerCaseNotation, 'updiagonalstrike')">1</xsl:when>
1227
                           <xsl:otherwise>0</xsl:otherwise>
1228
                        </xsl:choose>
1229
                     </xsl:variable>
1230
                     <xsl:variable name="fStrikeTLBR">
1231
                        <xsl:choose>
1232
                           <xsl:when test="contains($sLowerCaseNotation, 'downdiagonalstrike')">1</xsl:when>
1233
                           <xsl:otherwise>0</xsl:otherwise>
1234
                        </xsl:choose>
1235
                     </xsl:variable>
1236

    
1237
                     <!-- Should we create borderBoxPr? 
1238
                   We should if the enclosure isn't Word's default, which is
1239
                   a plain box -->
1240
              <xsl:if test="$fStrikeH=1                            or $fStrikeV=1                           or $fStrikeBLTR=1                            or $fStrikeTLBR=1                           or ($fBox=0                               and not($fTop=1                                        and $fBot=1                                       and $fLeft=1                                       and $fRight=1)                               )">
1241
                        <borderBoxPr>
1242
                           <xsl:if test="$fBox=0">
1243
                              <xsl:if test="$fTop=0">
1244
                                 <hideTop>
1245
                                    <xsl:attribute name="m:val">on</xsl:attribute>
1246
                                 </hideTop>
1247
                              </xsl:if>
1248
                              <xsl:if test="$fBot=0">
1249
                                 <hideBot>
1250
                                    <xsl:attribute name="m:val">on</xsl:attribute>
1251
                                 </hideBot>
1252
                              </xsl:if>
1253
                              <xsl:if test="$fLeft=0">
1254
                                 <hideLeft>
1255
                                    <xsl:attribute name="m:val">on</xsl:attribute>
1256
                                 </hideLeft>
1257
                              </xsl:if>
1258
                              <xsl:if test="$fRight=0">
1259
                                 <hideRight>
1260
                                    <xsl:attribute name="m:val">on</xsl:attribute>
1261
                                 </hideRight>
1262
                              </xsl:if>
1263
                           </xsl:if>
1264
                           <xsl:if test="$fStrikeH=1">
1265
                              <strikeH>
1266
                                 <xsl:attribute name="m:val">on</xsl:attribute>
1267
                              </strikeH>
1268
                           </xsl:if>
1269
                           <xsl:if test="$fStrikeV=1">
1270
                              <strikeV>
1271
                                 <xsl:attribute name="m:val">on</xsl:attribute>
1272
                              </strikeV>
1273
                           </xsl:if>
1274
                           <xsl:if test="$fStrikeBLTR=1">
1275
                              <strikeBLTR>
1276
                                 <xsl:attribute name="m:val">on</xsl:attribute>
1277
                              </strikeBLTR>
1278
                           </xsl:if>
1279
                           <xsl:if test="$fStrikeTLBR=1">
1280
                              <strikeTLBR>
1281
                                 <xsl:attribute name="m:val">on</xsl:attribute>
1282
                              </strikeTLBR>
1283
                           </xsl:if>
1284
                        </borderBoxPr>
1285
                     </xsl:if>
1286
                     <e>
1287
                        <xsl:call-template name="CreateArgProp"/>
1288
                        <xsl:apply-templates mode="mml"/>
1289
                     </e>
1290
                  </borderBox>
1291
               </xsl:otherwise>
1292
            </xsl:choose>
1293
         </xsl:otherwise>
1294
      </xsl:choose>
1295
  </xsl:template>
1296

    
1297
  <!-- %%Template: CreateArgProp
1298
	-->
1299
  <xsl:template name="CreateArgProp">
1300
      <xsl:if test="not(count(ancestor-or-self::mml:mstyle[@scriptlevel='0' or @scriptlevel='1' or @scriptlevel='2'])=0)                   or not(count(ancestor-or-self::mml:mstyle[@mml:scriptlevel='0' or @mml:scriptlevel='1' or @mml:scriptlevel='2'])=0)">
1301
         <argPr>
1302
            <scrLvl>
1303
               <xsl:attribute name="m:val">
1304
                  <xsl:choose>
1305
                     <xsl:when test="ancestor-or-self::mml:mstyle[@scriptlevel][1]/@scriptlevel">
1306
                        <xsl:value-of select="ancestor-or-self::mml:mstyle[@scriptlevel][1]/@scriptlevel"/>
1307
                     </xsl:when>
1308
                     <xsl:otherwise>
1309
                        <xsl:value-of select="ancestor-or-self::mml:mstyle[@scriptlevel][1]/@mml:scriptlevel"/>
1310
                     </xsl:otherwise>
1311
                  </xsl:choose>
1312
               </xsl:attribute>
1313
            </scrLvl>
1314
         </argPr>
1315
      </xsl:if>
1316
  </xsl:template>
1317

    
1318
  <!-- %%Template: match mroot
1319
	-->
1320
  <xsl:template mode="mml" match="mml:mroot">
1321
      <rad>
1322
         <radPr>
1323
            <degHide>
1324
               <xsl:attribute name="m:val">off</xsl:attribute>
1325
            </degHide>
1326
         </radPr>
1327
         <deg>
1328
            <xsl:call-template name="CreateArgProp"/>
1329
            <xsl:apply-templates mode="mml" select="child::*[2]"/>
1330
         </deg>
1331
         <e>
1332
            <xsl:call-template name="CreateArgProp"/>
1333
            <xsl:apply-templates mode="mml" select="child::*[1]"/>
1334
         </e>
1335
      </rad>
1336
  </xsl:template>
1337

    
1338
  <!-- MathML has no concept of a linear fraction.  When transforming a linear fraction
1339
       from Omml to MathML, we create the following MathML:
1340
       
1341
       <mml:mrow>
1342
         <mml:mrow>
1343
            // numerator
1344
         </mml:mrow>
1345
         <mml:mo>/</mml:mo>
1346
         <mml:mrow>
1347
            // denominator
1348
         </mml:mrow>
1349
       </mml:mrow>
1350
       
1351
       This template looks for four things:
1352
          1.  ndCur is an mml:mrow
1353
          2.  ndCur has three children
1354
          3.  The second child is an <mml:mo>
1355
          4.  The second child's text is '/'
1356
       
1357
       -->
1358
  <xsl:template name="FLinearFrac">
1359
      <xsl:param name="ndCur" select="."/>
1360
      <xsl:variable name="sNdText">
1361
         <xsl:value-of select="normalize-space($ndCur/*[2])"/>
1362
      </xsl:variable>
1363

    
1364
      <xsl:choose>
1365
      <!-- I spy a linear fraction -->
1366
      <xsl:when test="$ndCur/self::mml:mrow                       and count($ndCur/*)=3                       and $ndCur/*[2][self::mml:mo]                       and $sNdText='/'">1</xsl:when>
1367
         <xsl:otherwise>0</xsl:otherwise>
1368
      </xsl:choose>
1369
  </xsl:template>
1370

    
1371

    
1372
  <!-- Though presentation mathml can certainly typeset any generic function with the
1373
	     appropriate function operator spacing, presentation MathML has no concept of 
1374
			 a function structure like omml does.  In order to preserve the omml <func> 
1375
			 element, we must establish how an omml <func> element looks in mml.  This 
1376
			 is shown below:
1377
       
1378
       <mml:mrow>
1379
         <mml:mrow>
1380
            // function name
1381
         </mml:mrow>
1382
         <mml:mo>&#x02061;</mml:mo>
1383
         <mml:mrow>
1384
            // function argument
1385
         </mml:mrow>
1386
       </mml:mrow>
1387
       
1388
       This template looks for six things to be true:
1389
					1.  ndCur is an mml:mrow
1390
					2.  ndCur has three children
1391
					3.  The first child is an <mml:mrow>
1392
					4.  The second child is an <mml:mo>
1393
					5.  The third child is an <mml:mrow>
1394
					6.  The second child's text is '&#x02061;'
1395
       -->
1396
  <xsl:template name="FIsFunc">
1397
      <xsl:param name="ndCur" select="."/>
1398
      <xsl:variable name="sNdText">
1399
         <xsl:value-of select="normalize-space($ndCur/*[2])"/>
1400
      </xsl:variable>
1401

    
1402
      <xsl:choose>
1403
      <!-- Is this an omml function -->
1404
      <xsl:when test="count($ndCur/*)=3               and $ndCur/self::*[self::mml:mrow]                       and $ndCur/*[2][self::mml:mo]                       and $sNdText='⁡'">1</xsl:when>
1405
         <xsl:otherwise>0</xsl:otherwise>
1406
      </xsl:choose>
1407
  </xsl:template>
1408

    
1409

    
1410
  <!-- Given the node of the linear fraction's parent mrow, 
1411
       make a linear fraction -->
1412
  <xsl:template name="MakeLinearFraction">
1413
      <xsl:param name="ndCur" select="."/>
1414
      <f>
1415
         <fPr>
1416
            <type>
1417
               <xsl:attribute name="m:val">lin</xsl:attribute>
1418
            </type>
1419
         </fPr>
1420
         <num>
1421
            <xsl:call-template name="CreateArgProp"/>
1422
            <xsl:apply-templates mode="mml" select="$ndCur/*[1]"/>
1423
         </num>
1424
         <den>
1425
            <xsl:call-template name="CreateArgProp"/>
1426
            <xsl:apply-templates mode="mml" select="$ndCur/*[3]"/>
1427
         </den>
1428
      </f>
1429
  </xsl:template>
1430

    
1431

    
1432
  <!-- Given the node of the function's parent mrow, 
1433
       make an omml function -->
1434
  <xsl:template name="WriteFunc">
1435
      <xsl:param name="ndCur" select="."/>
1436

    
1437
      <func>
1438
         <fName>
1439
            <xsl:apply-templates mode="mml" select="$ndCur/child::*[1]"/>
1440
         </fName>
1441
         <e>
1442
            <xsl:apply-templates mode="mml" select="$ndCur/child::*[3]"/>
1443
         </e>
1444
      </func>
1445
  </xsl:template>
1446

    
1447

    
1448
  <!-- MathML doesn't have the concept of nAry structures.  The best approximation
1449
       to these is to have some under/over or sub/sup followed by an mrow or mstyle.
1450
       
1451
       In the case that we've come across some under/over or sub/sup that contains an 
1452
       nAry operator, this function handles the following sibling to the nAry structure.
1453
       
1454
       If the following sibling is:
1455
       
1456
          mml:mstyle, then apply templates to the children of this mml:mstyle
1457
          
1458
          mml:mrow, determine if this mrow is a linear fraction 
1459
          (see comments for FlinearFrac template).
1460
              If so, make an Omml linear fraction.
1461
              If not, apply templates as was done for mml:mstyle.
1462
       
1463
       -->
1464
  <xsl:template name="NaryHandleMrowMstyle">
1465
      <xsl:param name="ndCur" select="."/>
1466
      <!-- if the next sibling is an mrow, pull it in by 
1467
							doing whatever we would have done to its children. 
1468
							The mrow itself will be skipped, see template above. -->
1469
    <xsl:choose>
1470
         <xsl:when test="$ndCur[self::mml:mrow]">
1471
        <!-- Check for linear fraction -->
1472
        <xsl:variable name="fLinearFrac">
1473
               <xsl:call-template name="FLinearFrac">
1474
                  <xsl:with-param name="ndCur" select="$ndCur"/>
1475
               </xsl:call-template>
1476
            </xsl:variable>
1477
            <xsl:choose>
1478
               <xsl:when test="$fLinearFrac=1">
1479
                  <xsl:call-template name="MakeLinearFraction">
1480
                     <xsl:with-param name="ndCur" select="$ndCur"/>
1481
                  </xsl:call-template>
1482
               </xsl:when>
1483
               <xsl:otherwise>
1484
                  <xsl:variable name="fFunc">
1485
                     <xsl:call-template name="FIsFunc">
1486
                        <xsl:with-param name="ndCur" select="."/>
1487
                     </xsl:call-template>
1488
                  </xsl:variable>
1489
                  <xsl:choose>
1490
                     <xsl:when test="$fFunc=1">
1491
                        <xsl:call-template name="WriteFunc">
1492
                           <xsl:with-param name="ndCur" select="."/>
1493
                        </xsl:call-template>
1494
                     </xsl:when>
1495
                     <xsl:otherwise>
1496
                        <xsl:apply-templates mode="mml" select="$ndCur/*"/>
1497
                     </xsl:otherwise>
1498
                  </xsl:choose>
1499
               </xsl:otherwise>
1500
            </xsl:choose>
1501
         </xsl:when>
1502
         <xsl:when test="$ndCur[self::mml:mstyle]">
1503
            <xsl:apply-templates mode="mml" select="$ndCur/*"/>
1504
         </xsl:when>
1505
      </xsl:choose>
1506
  </xsl:template>
1507

    
1508

    
1509
  <!-- MathML munder/mover can represent several Omml constructs 
1510
       (m:bar, m:limLow, m:limUpp, m:acc, m:groupChr, etc.).  The following 
1511
       templates (FIsBar, FIsAcc, and FIsGroupChr) are used to determine 
1512
			 which of these Omml constructs an munder/mover should be translated into. -->
1513

    
1514
  <!-- Note:  ndCur should only be an munder/mover MathML element.
1515
  
1516
       ndCur should be interpretted as an m:bar if
1517
          1)  its respective accent attribute is not true
1518
          2)  its second child is an mml:mo
1519
          3)  the character of the mml:mo is the correct under/over bar. -->
1520
  <xsl:template name="FIsBar">
1521
      <xsl:param name="ndCur"/>
1522
      <xsl:variable name="fUnder">
1523
         <xsl:choose>
1524
            <xsl:when test="$ndCur[self::mml:munder]">1</xsl:when>
1525
            <xsl:otherwise>0</xsl:otherwise>
1526
         </xsl:choose>
1527
      </xsl:variable>
1528
      <xsl:variable name="sLowerCaseAccent">
1529
         <xsl:choose>
1530
            <xsl:when test="$fUnder=1">
1531
               <xsl:choose>
1532
                  <xsl:when test="$ndCur/@accentunder">
1533
                     <xsl:value-of select="translate($ndCur/@accentunder, $StrUCAlphabet, $StrLCAlphabet)"/>
1534
                  </xsl:when>
1535
                  <xsl:otherwise>
1536
                     <xsl:value-of select="translate($ndCur/@mml:accentunder, $StrUCAlphabet, $StrLCAlphabet)"/>
1537
                  </xsl:otherwise>
1538
               </xsl:choose>
1539
            </xsl:when>
1540
            <xsl:otherwise>
1541
               <xsl:choose>
1542
                  <xsl:when test="$ndCur/@accent">
1543
                     <xsl:value-of select="translate($ndCur/@accent, $StrUCAlphabet, $StrLCAlphabet)"/>
1544
                  </xsl:when>
1545
                  <xsl:otherwise>
1546
                     <xsl:value-of select="translate($ndCur/@mml:accent, $StrUCAlphabet, $StrLCAlphabet)"/>
1547
                  </xsl:otherwise>
1548
               </xsl:choose>
1549
            </xsl:otherwise>
1550
         </xsl:choose>
1551
      </xsl:variable>
1552
      <xsl:variable name="fAccent">
1553
         <xsl:choose>
1554
            <xsl:when test="$sLowerCaseAccent='true'">1</xsl:when>
1555
            <xsl:otherwise>0</xsl:otherwise>
1556
         </xsl:choose>
1557
      </xsl:variable>
1558

    
1559
      <xsl:choose>
1560
      <!-- The script is unaccented and the second child is an mo -->
1561
      <xsl:when test="$fAccent = 0                        and $ndCur/child::*[2]/self::mml:mo">
1562
            <xsl:variable name="sOperator">
1563
               <xsl:value-of select="$ndCur/child::*[2]"/>
1564
            </xsl:variable>
1565
            <xsl:choose>
1566
          <!-- Should we write an underbar? -->
1567
          <xsl:when test="$fUnder = 1">
1568
                  <xsl:choose>
1569
                     <xsl:when test="$sOperator = '̲'">1</xsl:when>
1570
                     <xsl:otherwise>0</xsl:otherwise>
1571
                  </xsl:choose>
1572
               </xsl:when>
1573
               <!-- Should we write an overbar? -->
1574
          <xsl:otherwise>
1575
                  <xsl:choose>
1576
                     <xsl:when test="$sOperator = '¯'">1</xsl:when>
1577
                     <xsl:otherwise>0</xsl:otherwise>
1578
                  </xsl:choose>
1579
               </xsl:otherwise>
1580
            </xsl:choose>
1581
         </xsl:when>
1582
         <xsl:otherwise>0</xsl:otherwise>
1583
      </xsl:choose>
1584
  </xsl:template>
1585

    
1586
  <!-- Note:  ndCur should only be an mover MathML element.
1587
  
1588
       ndCur should be interpretted as an m:acc if
1589
          1)  its accent attribute is true
1590
          2)  its second child is an mml:mo
1591
          3)  there is only one character in the mml:mo -->
1592
  <xsl:template name="FIsAcc">
1593
      <xsl:param name="ndCur" select="."/>
1594

    
1595
      <xsl:variable name="sLowerCaseAccent">
1596
         <xsl:choose>
1597
            <xsl:when test="$ndCur/@accent">
1598
               <xsl:value-of select="translate($ndCur/@accent, $StrUCAlphabet, $StrLCAlphabet)"/>
1599
            </xsl:when>
1600
            <xsl:otherwise>
1601
               <xsl:value-of select="translate($ndCur/@mml:accent, $StrUCAlphabet, $StrLCAlphabet)"/>
1602
            </xsl:otherwise>
1603
         </xsl:choose>
1604
      </xsl:variable>
1605
      <xsl:variable name="fAccent">
1606
         <xsl:choose>
1607
            <xsl:when test="$sLowerCaseAccent='true'">1</xsl:when>
1608
            <xsl:otherwise>0</xsl:otherwise>
1609
         </xsl:choose>
1610
      </xsl:variable>
1611

    
1612
      <xsl:choose>
1613
      <!-- The script is accented and the second child is an mo -->
1614
      <xsl:when test="$fAccent = 1                        and $ndCur/child::*[2] = mml:mo">
1615
            <xsl:variable name="sOperator">
1616
               <xsl:value-of select="$ndCur/child::*[2]"/>
1617
            </xsl:variable>
1618
            <xsl:choose>
1619
          <!-- There is only one operator, this is a valid Omml accent! -->
1620
          <xsl:when test="string-length($sOperator) = 1">1</xsl:when>
1621
               <!-- More than one accented operator.  This isn't a valid
1622
               omml accent -->
1623
          <xsl:otherwise>0</xsl:otherwise>
1624
            </xsl:choose>
1625
         </xsl:when>
1626
         <!-- Not accented, not an operator, or both, but in any case, this is
1627
           not an Omml accent. -->
1628
      <xsl:otherwise>0</xsl:otherwise>
1629
      </xsl:choose>
1630
  </xsl:template>
1631

    
1632

    
1633
  <!-- Is ndCur a groupChr? 
1634
			 ndCur is a groupChr if:
1635
			 
1636
				 1.  The accent is false (note:  accent attribute 
1637
						 for munder is accentunder). 
1638
				 2.  ndCur is an munder or mover.
1639
				 3.  ndCur has two children
1640
				 4.  Of these two children, one is an mml:mo and the other is an mml:mrow
1641
				 5.  The number of characters in the mml:mo is 1.
1642
			 
1643
			 If all of the above are true, then return 1, else return 0.
1644
	-->
1645
  <xsl:template name="FIsGroupChr">
1646
      <xsl:param name="ndCur" select="."/>
1647
      <xsl:variable name="fUnder">
1648
         <xsl:choose>
1649
            <xsl:when test="$ndCur[self::mml:munder]">1</xsl:when>
1650
            <xsl:otherwise>0</xsl:otherwise>
1651
         </xsl:choose>
1652
      </xsl:variable>
1653
      <xsl:variable name="sLowerCaseAccent">
1654
         <xsl:choose>
1655
            <xsl:when test="$fUnder=1">
1656
               <xsl:choose>
1657
                  <xsl:when test="$ndCur/@accentunder">
1658
                     <xsl:value-of select="translate($ndCur/@accentunder, $StrUCAlphabet, $StrLCAlphabet)"/>
1659
                  </xsl:when>
1660
                  <xsl:otherwise>
1661
                     <xsl:value-of select="translate($ndCur/@mml:accentunder, $StrUCAlphabet, $StrLCAlphabet)"/>
1662
                  </xsl:otherwise>
1663
               </xsl:choose>
1664
            </xsl:when>
1665
            <xsl:otherwise>
1666
               <xsl:choose>
1667
                  <xsl:when test="$ndCur/@accent">
1668
                     <xsl:value-of select="translate($ndCur/@accent, $StrUCAlphabet, $StrLCAlphabet)"/>
1669
                  </xsl:when>
1670
                  <xsl:otherwise>
1671
                     <xsl:value-of select="translate($ndCur/@mml:accent, $StrUCAlphabet, $StrLCAlphabet)"/>
1672
                  </xsl:otherwise>
1673
               </xsl:choose>
1674
            </xsl:otherwise>
1675
         </xsl:choose>
1676
      </xsl:variable>
1677

    
1678
      <xsl:variable name="fAccentFalse">
1679
         <xsl:choose>
1680
            <xsl:when test="$sLowerCaseAccent='false'">1</xsl:when>
1681
            <xsl:otherwise>0</xsl:otherwise>
1682
         </xsl:choose>
1683
      </xsl:variable>
1684

    
1685
      <xsl:choose>
1686
         <xsl:when test="$fAccentFalse=1                       and $ndCur[self::mml:munder or self::mml:mover]                        and count($ndCur/child::*)=2                        and (($ndCur/child::*[1][self::mml:mrow] and $ndCur/child::*[2][self::mml:mo])                              or ($ndCur/child::*[1][self::mml:mo] and $ndCur/child::*[2][self::mml:mrow]))">
1687
            <xsl:variable name="sOperator">
1688
               <xsl:value-of select="$ndCur/child::mml:mo"/>
1689
            </xsl:variable>
1690
            <xsl:choose>
1691
               <xsl:when test="string-length($sOperator) &lt;= 1">1</xsl:when>
1692
               <xsl:otherwise>0</xsl:otherwise>
1693
            </xsl:choose>
1694
         </xsl:when>
1695

    
1696
         <xsl:otherwise>0</xsl:otherwise>
1697
      </xsl:choose>
1698
  </xsl:template>
1699

    
1700

    
1701
  <!-- %%Template: match munder
1702
	-->
1703
  <xsl:template mode="mml" match="mml:munder">
1704
      <xsl:variable name="fNary">
1705
         <xsl:call-template name="isNary">
1706
            <xsl:with-param name="ndCur" select="child::*[1]"/>
1707
         </xsl:call-template>
1708
      </xsl:variable>
1709
      <xsl:choose>
1710
         <xsl:when test="$fNary='true'">
1711
            <nary>
1712
               <xsl:call-template name="CreateNaryProp">
1713
                  <xsl:with-param name="chr">
1714
                     <xsl:value-of select="normalize-space(child::*[1])"/>
1715
                  </xsl:with-param>
1716
                  <xsl:with-param name="sMathmlType" select="'munder'"/>
1717
               </xsl:call-template>
1718
               <sub>
1719
                  <xsl:call-template name="CreateArgProp"/>
1720
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
1721
               </sub>
1722
               <sup>
1723
                  <xsl:call-template name="CreateArgProp"/>
1724
               </sup>
1725
               <e>
1726
                  <xsl:call-template name="CreateArgProp"/>
1727
                  <xsl:call-template name="NaryHandleMrowMstyle">
1728
                     <xsl:with-param name="ndCur" select="following-sibling::*[1]"/>
1729
                  </xsl:call-template>
1730
               </e>
1731
            </nary>
1732
         </xsl:when>
1733
         <xsl:otherwise>
1734
        <!-- Should this munder be interpreted as an OMML m:bar? -->
1735
        <xsl:variable name="fIsBar">
1736
               <xsl:call-template name="FIsBar">
1737
                  <xsl:with-param name="ndCur" select="."/>
1738
               </xsl:call-template>
1739
            </xsl:variable>
1740
            <xsl:choose>
1741
               <xsl:when test="$fIsBar=1">
1742
                  <bar>
1743
                     <barPr>
1744
                        <pos m:val="bot"/>
1745
                     </barPr>
1746
                     <e>
1747
                        <xsl:call-template name="CreateArgProp"/>
1748
                        <xsl:apply-templates mode="mml" select="child::*[1]"/>
1749
                     </e>
1750
                  </bar>
1751
               </xsl:when>
1752
               <xsl:otherwise>
1753
            <!-- It isn't an integral or underbar, is this a groupChr? -->
1754
            <xsl:variable name="fGroupChr">
1755
                     <xsl:call-template name="FIsGroupChr">
1756
                        <xsl:with-param name="ndCur" select="."/>
1757
                     </xsl:call-template>
1758
                  </xsl:variable>
1759
                  <xsl:choose>
1760
                     <xsl:when test="$fGroupChr=1">
1761
                        <groupChr>
1762
                           <xsl:call-template name="CreateGroupChrPr">
1763
                              <xsl:with-param name="chr">
1764
                                 <xsl:value-of select="mml:mo"/>
1765
                              </xsl:with-param>
1766
                              <xsl:with-param name="pos">
1767
                                 <xsl:choose>
1768
                                    <xsl:when test="child::*[1][self::mml:mrow]">bot</xsl:when>
1769
                                    <xsl:otherwise>top</xsl:otherwise>
1770
                                 </xsl:choose>
1771
                              </xsl:with-param>
1772
                              <xsl:with-param name="vertJc">top</xsl:with-param>
1773
                           </xsl:call-template>
1774
                           <e>
1775
                              <xsl:apply-templates mode="mml" select="mml:mrow"/>
1776
                           </e>
1777
                        </groupChr>
1778
                     </xsl:when>
1779
                     <xsl:otherwise>
1780
                <!-- Generic munder -->
1781
                <limLow>
1782
                           <e>
1783
                              <xsl:call-template name="CreateArgProp"/>
1784
                              <xsl:apply-templates mode="mml" select="child::*[1]"/>
1785
                           </e>
1786
                           <lim>
1787
                              <xsl:call-template name="CreateArgProp"/>
1788
                              <xsl:apply-templates mode="mml" select="child::*[2]"/>
1789
                           </lim>
1790
                        </limLow>
1791
                     </xsl:otherwise>
1792
                  </xsl:choose>
1793
               </xsl:otherwise>
1794
            </xsl:choose>
1795
         </xsl:otherwise>
1796
      </xsl:choose>
1797
  </xsl:template>
1798

    
1799

    
1800
  <!-- Given the values for chr, pos, and vertJc, create an omml
1801
	     groupChr's groupChrPr -->
1802
  <xsl:template name="CreateGroupChrPr">
1803
      <xsl:param name="chr">⏟</xsl:param>
1804
      <xsl:param name="pos" select="bot"/>
1805
      <xsl:param name="vertJc" select="top"/>
1806
      <groupChrPr>
1807
         <chr>
1808
            <xsl:attribute name="m:val">
1809
               <xsl:value-of select="$chr"/>
1810
            </xsl:attribute>
1811
         </chr>
1812
         <pos>
1813
            <xsl:attribute name="m:val">
1814
               <xsl:value-of select="$pos"/>
1815
            </xsl:attribute>
1816
         </pos>
1817
         <vertJc>
1818
            <xsl:attribute name="m:val">
1819
               <xsl:value-of select="$vertJc"/>
1820
            </xsl:attribute>
1821
         </vertJc>
1822
      </groupChrPr>
1823
  </xsl:template>
1824

    
1825

    
1826
  <!-- %%Template: match mover
1827
	-->
1828
  <xsl:template mode="mml" match="mml:mover">
1829
      <xsl:variable name="fNary">
1830
         <xsl:call-template name="isNary">
1831
            <xsl:with-param name="ndCur" select="child::*[1]"/>
1832
         </xsl:call-template>
1833
      </xsl:variable>
1834
      <xsl:choose>
1835
         <xsl:when test="$fNary='true'">
1836
            <nary>
1837
               <xsl:call-template name="CreateNaryProp">
1838
                  <xsl:with-param name="chr">
1839
                     <xsl:value-of select="normalize-space(child::*[1])"/>
1840
                  </xsl:with-param>
1841
                  <xsl:with-param name="sMathmlType" select="'mover'"/>
1842
               </xsl:call-template>
1843
               <sub>
1844
                  <xsl:call-template name="CreateArgProp"/>
1845
               </sub>
1846
               <sup>
1847
                  <xsl:call-template name="CreateArgProp"/>
1848
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
1849
               </sup>
1850
               <e>
1851
                  <xsl:call-template name="CreateArgProp"/>
1852
                  <xsl:call-template name="NaryHandleMrowMstyle">
1853
                     <xsl:with-param name="ndCur" select="following-sibling::*[1]"/>
1854
                  </xsl:call-template>
1855
               </e>
1856
            </nary>
1857
         </xsl:when>
1858
         <xsl:otherwise>
1859
        <!-- Should this munder be interpreted as an OMML m:bar or m:acc? -->
1860

    
1861
        <!-- Check to see if this is an m:bar -->
1862
        <xsl:variable name="fIsBar">
1863
               <xsl:call-template name="FIsBar">
1864
                  <xsl:with-param name="ndCur" select="."/>
1865
               </xsl:call-template>
1866
            </xsl:variable>
1867
            <xsl:choose>
1868
               <xsl:when test="$fIsBar = 1">
1869
                  <bar>
1870
                     <barPr>
1871
                        <pos m:val="top"/>
1872
                     </barPr>
1873
                     <e>
1874
                        <xsl:call-template name="CreateArgProp"/>
1875
                        <xsl:apply-templates mode="mml" select="child::*[1]"/>
1876
                     </e>
1877
                  </bar>
1878
               </xsl:when>
1879
               <xsl:otherwise>
1880
            <!-- Not an m:bar, should it be an m:acc? -->
1881
            <xsl:variable name="fIsAcc">
1882
                     <xsl:call-template name="FIsAcc">
1883
                        <xsl:with-param name="ndCur" select="."/>
1884
                     </xsl:call-template>
1885
                  </xsl:variable>
1886
                  <xsl:choose>
1887
                     <xsl:when test="$fIsAcc=1">
1888
                        <acc>
1889
                           <accPr>
1890
                              <chr>
1891
                                 <xsl:attribute name="m:val">
1892
                                    <xsl:value-of select="child::*[2]"/>
1893
                                 </xsl:attribute>
1894
                              </chr>
1895
                           </accPr>
1896
                           <e>
1897
                              <xsl:call-template name="CreateArgProp"/>
1898
                              <xsl:apply-templates mode="mml" select="child::*[1]"/>
1899
                           </e>
1900
                        </acc>
1901
                     </xsl:when>
1902
                     <xsl:otherwise>
1903
                <!-- This isn't an integral, overbar or accent, 
1904
								     could it be a groupChr? -->
1905
                <xsl:variable name="fGroupChr">
1906
                           <xsl:call-template name="FIsGroupChr">
1907
                              <xsl:with-param name="ndCur" select="."/>
1908
                           </xsl:call-template>
1909
                        </xsl:variable>
1910
                        <xsl:choose>
1911
                           <xsl:when test="$fGroupChr=1">
1912
                              <groupChr>
1913
                                 <xsl:call-template name="CreateGroupChrPr">
1914
                                    <xsl:with-param name="chr">
1915
                                       <xsl:value-of select="mml:mo"/>
1916
                                    </xsl:with-param>
1917
                                    <xsl:with-param name="pos">
1918
                                       <xsl:choose>
1919
                                          <xsl:when test="child::*[1][self::mml:mrow]">top</xsl:when>
1920
                                          <xsl:otherwise>bot</xsl:otherwise>
1921
                                       </xsl:choose>
1922
                                    </xsl:with-param>
1923
                                    <xsl:with-param name="vertJc">bot</xsl:with-param>
1924
                                 </xsl:call-template>
1925
                                 <e>
1926
                                    <xsl:apply-templates mode="mml" select="mml:mrow"/>
1927
                                 </e>
1928
                              </groupChr>
1929
                           </xsl:when>
1930
                           <xsl:otherwise>
1931
                    <!-- Generic mover -->
1932
                    <limUpp>
1933
                                 <e>
1934
                                    <xsl:call-template name="CreateArgProp"/>
1935
                                    <xsl:apply-templates mode="mml" select="child::*[1]"/>
1936
                                 </e>
1937
                                 <lim>
1938
                                    <xsl:call-template name="CreateArgProp"/>
1939
                                    <xsl:apply-templates mode="mml" select="child::*[2]"/>
1940
                                 </lim>
1941
                              </limUpp>
1942
                           </xsl:otherwise>
1943
                        </xsl:choose>
1944
                     </xsl:otherwise>
1945
                  </xsl:choose>
1946
               </xsl:otherwise>
1947
            </xsl:choose>
1948
         </xsl:otherwise>
1949
      </xsl:choose>
1950
  </xsl:template>
1951

    
1952

    
1953
  <!-- %%Template: match munderover
1954
	-->
1955
  <xsl:template mode="mml" match="mml:munderover">
1956
      <xsl:variable name="fNary">
1957
         <xsl:call-template name="isNary">
1958
            <xsl:with-param name="ndCur" select="child::*[1]"/>
1959
         </xsl:call-template>
1960
      </xsl:variable>
1961
      <xsl:choose>
1962
         <xsl:when test="$fNary='true'">
1963
            <nary>
1964
               <xsl:call-template name="CreateNaryProp">
1965
                  <xsl:with-param name="chr">
1966
                     <xsl:value-of select="normalize-space(child::*[1])"/>
1967
                  </xsl:with-param>
1968
                  <xsl:with-param name="sMathmlType" select="'munderover'"/>
1969
               </xsl:call-template>
1970
               <sub>
1971
                  <xsl:call-template name="CreateArgProp"/>
1972
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
1973
               </sub>
1974
               <sup>
1975
                  <xsl:call-template name="CreateArgProp"/>
1976
                  <xsl:apply-templates mode="mml" select="child::*[3]"/>
1977
               </sup>
1978
               <e>
1979
                  <xsl:call-template name="CreateArgProp"/>
1980
                  <xsl:call-template name="NaryHandleMrowMstyle">
1981
                     <xsl:with-param name="ndCur" select="following-sibling::*[1]"/>
1982
                  </xsl:call-template>
1983
               </e>
1984
            </nary>
1985
         </xsl:when>
1986
         <xsl:otherwise>
1987
            <limUpp>
1988
               <e>
1989
                  <xsl:call-template name="CreateArgProp"/>
1990
                  <limLow>
1991
                     <e>
1992
                        <xsl:call-template name="CreateArgProp"/>
1993
                        <xsl:apply-templates mode="mml" select="child::*[1]"/>
1994
                     </e>
1995
                     <lim>
1996
                        <xsl:call-template name="CreateArgProp"/>
1997
                        <xsl:apply-templates mode="mml" select="child::*[2]"/>
1998
                     </lim>
1999
                  </limLow>
2000
               </e>
2001
               <lim>
2002
                  <xsl:call-template name="CreateArgProp"/>
2003
                  <xsl:apply-templates mode="mml" select="child::*[3]"/>
2004
               </lim>
2005
            </limUpp>
2006
         </xsl:otherwise>
2007
      </xsl:choose>
2008
  </xsl:template>
2009

    
2010
  <!-- %%Template: match mfenced -->
2011
  <xsl:template mode="mml" match="mml:mfenced">
2012
      <d>
2013
         <xsl:call-template name="CreateDelimProp">
2014
            <xsl:with-param name="fChOpenValid">
2015
               <xsl:choose>
2016
                  <xsl:when test="@open">
2017
                     <xsl:value-of select="1"/>
2018
                  </xsl:when>
2019
                  <xsl:when test="@mml:open">
2020
                     <xsl:value-of select="1"/>
2021
                  </xsl:when>
2022
                  <xsl:otherwise>
2023
                     <xsl:value-of select="0"/>
2024
                  </xsl:otherwise>
2025
               </xsl:choose>
2026
            </xsl:with-param>
2027
            <xsl:with-param name="chOpen">
2028
               <xsl:choose>
2029
                  <xsl:when test="@open">
2030
                     <xsl:value-of select="@open"/>
2031
                  </xsl:when>
2032
                  <xsl:otherwise>
2033
                     <xsl:value-of select="@mml:open"/>
2034
                  </xsl:otherwise>
2035
               </xsl:choose>
2036
            </xsl:with-param>
2037
            <xsl:with-param name="fChSeparatorsValid">
2038
               <xsl:choose>
2039
                  <xsl:when test="@separators">
2040
                     <xsl:value-of select="1"/>
2041
                  </xsl:when>
2042
                  <xsl:when test="@mml:separators">
2043
                     <xsl:value-of select="1"/>
2044
                  </xsl:when>
2045
                  <xsl:otherwise>
2046
                     <xsl:value-of select="0"/>
2047
                  </xsl:otherwise>
2048
               </xsl:choose>
2049
            </xsl:with-param>
2050
            <xsl:with-param name="chSeparators">
2051
               <xsl:choose>
2052
                  <xsl:when test="@separators">
2053
                     <xsl:value-of select="@separators"/>
2054
                  </xsl:when>
2055
                  <xsl:otherwise>
2056
                     <xsl:value-of select="@mml:separators"/>
2057
                  </xsl:otherwise>
2058
               </xsl:choose>
2059
            </xsl:with-param>
2060
            <xsl:with-param name="fChCloseValid">
2061
               <xsl:choose>
2062
                  <xsl:when test="@close">
2063
                     <xsl:value-of select="1"/>
2064
                  </xsl:when>
2065
                  <xsl:when test="@mml:close">
2066
                     <xsl:value-of select="1"/>
2067
                  </xsl:when>
2068
                  <xsl:otherwise>
2069
                     <xsl:value-of select="0"/>
2070
                  </xsl:otherwise>
2071
               </xsl:choose>
2072
            </xsl:with-param>
2073
            <xsl:with-param name="chClose">
2074
               <xsl:choose>
2075
                  <xsl:when test="@close">
2076
                     <xsl:value-of select="@close"/>
2077
                  </xsl:when>
2078
                  <xsl:otherwise>
2079
                     <xsl:value-of select="@mml:close"/>
2080
                  </xsl:otherwise>
2081
               </xsl:choose>
2082
            </xsl:with-param>
2083
         </xsl:call-template>
2084
         <xsl:for-each select="*">
2085
            <e>
2086
               <xsl:call-template name="CreateArgProp"/>
2087
               <xsl:apply-templates mode="mml" select="."/>
2088
            </e>
2089
         </xsl:for-each>
2090
      </d>
2091
  </xsl:template>
2092

    
2093
  <!-- %%Template: CreateDelimProp
2094
	
2095
		Given the characters to use as open, close and separators for 
2096
		the delim object, create the m:dPr (delim properties). 		
2097
		
2098
		MathML can have any number of separators in an mfenced object, but 
2099
		OMML can only represent one separator for each d (delim) object.
2100
		So, we pick the first separator specified. 		
2101
	-->
2102
  <xsl:template name="CreateDelimProp">
2103
      <xsl:param name="fChOpenValid"/>
2104
      <xsl:param name="chOpen"/>
2105
      <xsl:param name="fChSeparatorsValid"/>
2106
      <xsl:param name="chSeparators"/>
2107
      <xsl:param name="fChCloseValid"/>
2108
      <xsl:param name="chClose"/>
2109
      <xsl:variable name="chSep" select="substring($chSeparators, 1, 1)"/>
2110

    
2111
      <!-- do we need a dPr at all? If everything's at its default value, then 
2112
			don't bother at all -->
2113
    <xsl:if test="($fChOpenValid=1 and not($chOpen = '(')) or         ($fChCloseValid=1 and not($chClose = ')')) or          not($chSep = '|')">
2114
         <dPr>
2115
        <!-- the default for MathML and OMML is '('. -->
2116
        <xsl:if test="$fChOpenValid=1 and not($chOpen = '(')">
2117
               <begChr>
2118
                  <xsl:attribute name="m:val">
2119
                     <xsl:value-of select="$chOpen"/>
2120
                  </xsl:attribute>
2121
               </begChr>
2122
            </xsl:if>
2123

    
2124
            <!-- the default for MathML is ',' and for OMML is '|' -->
2125

    
2126
        <xsl:choose>
2127
          <!-- matches OMML's default, don't bother to write anything out -->
2128
          <xsl:when test="$chSep = '|'"/>
2129

    
2130
               <!-- Not specified, use MathML's default. We test against 
2131
					the existence of the actual attribute, not the substring -->
2132
          <xsl:when test="$fChSeparatorsValid=0">
2133
                  <sepChr m:val=","/>
2134
               </xsl:when>
2135

    
2136
               <xsl:otherwise>
2137
                  <sepChr>
2138
                     <xsl:attribute name="m:val">
2139
                        <xsl:value-of select="$chSep"/>
2140
                     </xsl:attribute>
2141
                  </sepChr>
2142
               </xsl:otherwise>
2143
            </xsl:choose>
2144

    
2145
            <!-- the default for MathML and OMML is ')'. -->
2146
        <xsl:if test="$fChCloseValid=1 and not($chClose = ')')">
2147
               <endChr>
2148
                  <xsl:attribute name="m:val">
2149
                     <xsl:value-of select="$chClose"/>
2150
                  </xsl:attribute>
2151
               </endChr>
2152
            </xsl:if>
2153
         </dPr>
2154
      </xsl:if>
2155
  </xsl:template>
2156

    
2157
  <!-- %%Template: OutputMs
2158
	-->
2159
  <xsl:template name="OutputMs">
2160
      <xsl:param name="msCur"/>
2161
      <xsl:choose>
2162
         <xsl:when test="(not($msCur/@lquote) or $msCur/@lquote='')                       and (not($msCur/@mml:lquote) or $msCur/@mml:lquote='')">
2163
            <xsl:text>"</xsl:text>
2164
         </xsl:when>
2165
         <xsl:otherwise>
2166
            <xsl:choose>
2167
               <xsl:when test="$msCur/@lquote">
2168
                  <xsl:value-of select="$msCur/@lquote"/>
2169
               </xsl:when>
2170
               <xsl:otherwise>
2171
                  <xsl:value-of select="$msCur/@mml:lquote"/>
2172
               </xsl:otherwise>
2173
            </xsl:choose>
2174
         </xsl:otherwise>
2175
      </xsl:choose>
2176
      <xsl:value-of select="normalize-space($msCur)"/>
2177
      <xsl:choose>
2178
         <xsl:when test="(not($msCur/@rquote) or $msCur/@rquote='')                        and (not($msCur/@mml:rquote) or $msCur/@mml:rquote='')">
2179
            <xsl:text>"</xsl:text>
2180
         </xsl:when>
2181
         <xsl:otherwise>
2182
            <xsl:choose>
2183
               <xsl:when test="$msCur/@rquote">
2184
                  <xsl:value-of select="$msCur/@rquote"/>
2185
               </xsl:when>
2186
               <xsl:otherwise>
2187
                  <xsl:value-of select="$msCur/@mml:rquote"/>
2188
               </xsl:otherwise>
2189
            </xsl:choose>
2190
         </xsl:otherwise>
2191
      </xsl:choose>
2192
  </xsl:template>
2193

    
2194
  <!-- %%Template: match msub
2195
	-->
2196
  <xsl:template mode="mml" match="mml:msub">
2197
      <xsl:variable name="fNary">
2198
         <xsl:call-template name="isNary">
2199
            <xsl:with-param name="ndCur" select="child::*[1]"/>
2200
         </xsl:call-template>
2201
      </xsl:variable>
2202
      <xsl:choose>
2203
         <xsl:when test="$fNary='true'">
2204
            <nary>
2205
               <xsl:call-template name="CreateNaryProp">
2206
                  <xsl:with-param name="chr">
2207
                     <xsl:value-of select="normalize-space(child::*[1])"/>
2208
                  </xsl:with-param>
2209
                  <xsl:with-param name="sMathmlType" select="'msub'"/>
2210
               </xsl:call-template>
2211
               <sub>
2212
                  <xsl:call-template name="CreateArgProp"/>
2213
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
2214
               </sub>
2215
               <sup>
2216
                  <xsl:call-template name="CreateArgProp"/>
2217
               </sup>
2218
               <e>
2219
                  <xsl:call-template name="CreateArgProp"/>
2220
                  <xsl:call-template name="NaryHandleMrowMstyle">
2221
                     <xsl:with-param name="ndCur" select="following-sibling::*[1]"/>
2222
                  </xsl:call-template>
2223
               </e>
2224
            </nary>
2225
         </xsl:when>
2226
         <xsl:otherwise>
2227
            <sSub>
2228
               <e>
2229
                  <xsl:call-template name="CreateArgProp"/>
2230
                  <xsl:apply-templates mode="mml" select="child::*[1]"/>
2231
               </e>
2232
               <sub>
2233
                  <xsl:call-template name="CreateArgProp"/>
2234
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
2235
               </sub>
2236
            </sSub>
2237
         </xsl:otherwise>
2238
      </xsl:choose>
2239
  </xsl:template>
2240

    
2241
  <!-- %%Template: match msup
2242
	-->
2243
  <xsl:template mode="mml" match="mml:msup">
2244
      <xsl:variable name="fNary">
2245
         <xsl:call-template name="isNary">
2246
            <xsl:with-param name="ndCur" select="child::*[1]"/>
2247
         </xsl:call-template>
2248
      </xsl:variable>
2249
      <xsl:choose>
2250
         <xsl:when test="$fNary='true'">
2251
            <nary>
2252
               <xsl:call-template name="CreateNaryProp">
2253
                  <xsl:with-param name="chr">
2254
                     <xsl:value-of select="normalize-space(child::*[1])"/>
2255
                  </xsl:with-param>
2256
                  <xsl:with-param name="sMathmlType" select="'msup'"/>
2257
               </xsl:call-template>
2258
               <sub>
2259
                  <xsl:call-template name="CreateArgProp"/>
2260
               </sub>
2261
               <sup>
2262
                  <xsl:call-template name="CreateArgProp"/>
2263
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
2264
               </sup>
2265
               <e>
2266
                  <xsl:call-template name="CreateArgProp"/>
2267
                  <xsl:call-template name="NaryHandleMrowMstyle">
2268
                     <xsl:with-param name="ndCur" select="following-sibling::*[1]"/>
2269
                  </xsl:call-template>
2270
               </e>
2271
            </nary>
2272
         </xsl:when>
2273
         <xsl:otherwise>
2274
            <sSup>
2275
               <e>
2276
                  <xsl:call-template name="CreateArgProp"/>
2277
                  <xsl:apply-templates mode="mml" select="child::*[1]"/>
2278
               </e>
2279
               <sup>
2280
                  <xsl:call-template name="CreateArgProp"/>
2281
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
2282
               </sup>
2283
            </sSup>
2284
         </xsl:otherwise>
2285
      </xsl:choose>
2286
  </xsl:template>
2287

    
2288
  <!-- %%Template: match msubsup
2289
	-->
2290
  <xsl:template mode="mml" match="mml:msubsup">
2291
      <xsl:variable name="fNary">
2292
         <xsl:call-template name="isNary">
2293
            <xsl:with-param name="ndCur" select="child::*[1]"/>
2294
         </xsl:call-template>
2295
      </xsl:variable>
2296
      <xsl:choose>
2297
         <xsl:when test="$fNary='true'">
2298
            <nary>
2299
               <xsl:call-template name="CreateNaryProp">
2300
                  <xsl:with-param name="chr">
2301
                     <xsl:value-of select="normalize-space(child::*[1])"/>
2302
                  </xsl:with-param>
2303
                  <xsl:with-param name="sMathmlType" select="'msubsup'"/>
2304
               </xsl:call-template>
2305
               <sub>
2306
                  <xsl:call-template name="CreateArgProp"/>
2307
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
2308
               </sub>
2309
               <sup>
2310
                  <xsl:call-template name="CreateArgProp"/>
2311
                  <xsl:apply-templates mode="mml" select="child::*[3]"/>
2312
               </sup>
2313
               <e>
2314
                  <xsl:call-template name="CreateArgProp"/>
2315
                  <xsl:call-template name="NaryHandleMrowMstyle">
2316
                     <xsl:with-param name="ndCur" select="following-sibling::*[1]"/>
2317
                  </xsl:call-template>
2318
               </e>
2319
            </nary>
2320
         </xsl:when>
2321
         <xsl:otherwise>
2322
            <sSubSup>
2323
               <e>
2324
                  <xsl:call-template name="CreateArgProp"/>
2325
                  <xsl:apply-templates mode="mml" select="child::*[1]"/>
2326
               </e>
2327
               <sub>
2328
                  <xsl:call-template name="CreateArgProp"/>
2329
                  <xsl:apply-templates mode="mml" select="child::*[2]"/>
2330
               </sub>
2331
               <sup>
2332
                  <xsl:call-template name="CreateArgProp"/>
2333
                  <xsl:apply-templates mode="mml" select="child::*[3]"/>
2334
               </sup>
2335
            </sSubSup>
2336
         </xsl:otherwise>
2337
      </xsl:choose>
2338
  </xsl:template>
2339

    
2340
  <!-- %%Template: SplitScripts 
2341
	
2342
		Takes an collection of nodes, and splits them
2343
		odd and even into sup and sub scripts. Used for dealing with
2344
		mmultiscript.
2345
		
2346
		This template assumes you want to output both a sub and sup element.
2347
		-->
2348
  <xsl:template name="SplitScripts">
2349
      <xsl:param name="ndScripts"/>
2350
      <sub>
2351
         <xsl:call-template name="CreateArgProp"/>
2352
         <xsl:apply-templates mode="mml" select="$ndScripts[(position() mod 2) = 1]"/>
2353
      </sub>
2354
      <sup>
2355
         <xsl:call-template name="CreateArgProp"/>
2356
         <xsl:apply-templates mode="mml" select="$ndScripts[(position() mod 2) = 0]"/>
2357
      </sup>
2358
  </xsl:template>
2359

    
2360
  <!-- %%Template: match mmultiscripts
2361
	
2362
		There is some subtlety with the mml:mprescripts element. Everything that comes before 
2363
		that is considered a script (as opposed to a pre-script), but it need not be present.
2364
	-->
2365
  <xsl:template mode="mml" match="mml:mmultiscripts">
2366

    
2367
    <!-- count the nodes. Everything that comes after a mml:mprescripts is considered a pre-script;
2368
			Everything that does not have an mml:mprescript as a preceding-sibling (and is not itself 
2369
			mml:mprescript) is a script, except for the first child which is always the base.
2370
			The mml:none element is a place holder for a sub/sup element slot.
2371
			
2372
			mmultisript pattern:
2373
			<mmultiscript>
2374
				(base)
2375
				(sub sup)* // Where <none/> can replace a sub/sup entry to preserve pattern.
2376
				<mprescripts />
2377
				(presub presup)*
2378
			</mmultiscript>
2379
			-->
2380
    <!-- Count of presecript nodes that we'd print (this is essentially anything but the none placeholder. -->
2381
    <xsl:variable name="cndPrescriptStrict"
2382
                    select="count(mml:mprescripts[1]/following-sibling::*[not(self::mml:none)])"/>
2383
      <!-- Count of all super script excluding mml:none -->
2384
    <xsl:variable name="cndSuperScript"
2385
                    select="count(*[not(preceding-sibling::mml:mprescripts)                               and not(self::mml:mprescripts)                               and ((position() mod 2) = 1)                                and not(self::mml:none)]) - 1"/>
2386
      <!-- Count of all sup script excluding mml:none -->
2387
    <xsl:variable name="cndSubScript"
2388
                    select="count(*[not(preceding-sibling::mml:mprescripts)                               and not(self::mml:mprescripts)                               and ((position() mod 2) = 0)                                and not(self::mml:none)])"/>
2389
      <!-- Count of all scripts excluding mml:none -->
2390
    <xsl:variable name="cndScriptStrict" select="$cndSuperScript + $cndSubScript"/>
2391
      <!-- Count of all scripts including mml:none.  This is essentially all nodes before the 
2392
		first mml:mprescripts except the base. -->
2393
    <xsl:variable name="cndScript"
2394
                    select="count(*[not(preceding-sibling::mml:mprescripts) and not(self::mml:mprescripts)]) - 1"/>
2395

    
2396
      <xsl:choose>
2397
      <!-- The easy case first. No prescripts, and no script ... just a base -->
2398
      <xsl:when test="$cndPrescriptStrict &lt;= 0 and $cndScriptStrict &lt;= 0">
2399
            <xsl:apply-templates mode="mml" select="*[1]"/>
2400
         </xsl:when>
2401

    
2402
         <!-- Next, if there are no prescripts -->
2403
      <xsl:when test="$cndPrescriptStrict &lt;= 0">
2404
        <!-- we know we have some scripts or else we would have taken the earlier
2405
					  branch. -->
2406
        <xsl:choose>
2407
          <!-- We have both sub and super scripts-->
2408
          <xsl:when test="$cndSuperScript &gt; 0 and $cndSubScript &gt; 0">
2409
                  <sSubSup>
2410
                     <e>
2411
                        <xsl:call-template name="CreateArgProp"/>
2412
                        <xsl:apply-templates mode="mml" select="child::*[1]"/>
2413
                     </e>
2414

    
2415
                     <!-- Every child except the first is a script.  Do the split -->
2416
              <xsl:call-template name="SplitScripts">
2417
                        <xsl:with-param name="ndScripts" select="*[position() &gt; 1]"/>
2418
                     </xsl:call-template>
2419
                  </sSubSup>
2420
               </xsl:when>
2421
               <!-- Just a sub script -->
2422
          <xsl:when test="$cndSubScript &gt; 0">
2423
                  <sSub>
2424
                     <e>
2425
                        <xsl:call-template name="CreateArgProp"/>
2426
                        <xsl:apply-templates mode="mml" select="child::*[1]"/>
2427
                     </e>
2428

    
2429
                     <!-- No prescripts and no super scripts, therefore, it's a sub. -->
2430
              <sub>
2431
                        <xsl:apply-templates mode="mml" select="*[position() &gt; 1]"/>
2432
                     </sub>
2433
                  </sSub>
2434
               </xsl:when>
2435
               <!-- Just super script -->
2436
          <xsl:otherwise>
2437
                  <sSup>
2438
                     <e>
2439
                        <xsl:call-template name="CreateArgProp"/>
2440
                        <xsl:apply-templates mode="mml" select="child::*[1]"/>
2441
                     </e>
2442

    
2443
                     <!-- No prescripts and no sub scripts, therefore, it's a sup. -->
2444
              <sup>
2445
                        <xsl:apply-templates mode="mml" select="*[position() &gt; 1]"/>
2446
                     </sup>
2447
                  </sSup>
2448
               </xsl:otherwise>
2449
            </xsl:choose>
2450
         </xsl:when>
2451

    
2452
         <!-- Next, if there are no scripts -->
2453
      <xsl:when test="$cndScriptStrict &lt;= 0">
2454
        <!-- we know we have some prescripts or else we would have taken the earlier
2455
					  branch. So, create an sPre and split the elements -->
2456
        <sPre>
2457
               <e>
2458
                  <xsl:call-template name="CreateArgProp"/>
2459
                  <xsl:apply-templates mode="mml" select="child::*[1]"/>
2460
               </e>
2461

    
2462
               <!-- The prescripts come after the mml:mprescript and if we get here
2463
							we know there exists some elements after the mml:mprescript element. 
2464
							
2465
							The prescript element has no sub/subsup variation, therefore, even if
2466
							we're only writing sub, we need to write out both the sub and sup element.
2467
							-->
2468
          <xsl:call-template name="SplitScripts">
2469
                  <xsl:with-param name="ndScripts" select="mml:mprescripts[1]/following-sibling::*"/>
2470
               </xsl:call-template>
2471
            </sPre>
2472
         </xsl:when>
2473

    
2474
         <!-- Finally, the case with both prescripts and scripts. Create an sPre 
2475
				element to house the prescripts, with a sub/sup/subsup element at its base. -->
2476
      <xsl:otherwise>
2477
            <sPre>
2478
               <e>
2479
                  <xsl:choose>
2480
              <!-- We have both sub and super scripts-->
2481
              <xsl:when test="$cndSuperScript &gt; 0 and $cndSubScript &gt; 0">
2482
                        <sSubSup>
2483
                           <e>
2484
                              <xsl:call-template name="CreateArgProp"/>
2485
                              <xsl:apply-templates mode="mml" select="child::*[1]"/>
2486
                           </e>
2487

    
2488
                           <!-- scripts come before the mml:mprescript but after the first child, so their
2489
								 positions will be 2, 3, ... ($nndScript + 1) -->
2490
                  <xsl:call-template name="SplitScripts">
2491
                              <xsl:with-param name="ndScripts"
2492
                                              select="*[(position() &gt; 1) and (position() &lt;= ($cndScript + 1))]"/>
2493
                           </xsl:call-template>
2494
                        </sSubSup>
2495
                     </xsl:when>
2496
                     <!-- Just a sub script -->
2497
              <xsl:when test="$cndSubScript &gt; 0">
2498
                        <sSub>
2499
                           <e>
2500
                              <xsl:call-template name="CreateArgProp"/>
2501
                              <xsl:apply-templates mode="mml" select="child::*[1]"/>
2502
                           </e>
2503

    
2504
                           <!-- We have prescripts but no super scripts, therefore, do a sub 
2505
									and apply templates to all tokens counted by cndScript. -->
2506
                  <sub>
2507
                              <xsl:apply-templates mode="mml" select="*[position() &gt; 1 and (position() &lt;= ($cndScript + 1))]"/>
2508
                           </sub>
2509
                        </sSub>
2510
                     </xsl:when>
2511
                     <!-- Just super script -->
2512
              <xsl:otherwise>
2513
                        <sSup>
2514
                           <e>
2515
                              <xsl:call-template name="CreateArgProp"/>
2516
                              <xsl:apply-templates mode="mml" select="child::*[1]"/>
2517
                           </e>
2518

    
2519
                           <!-- We have prescripts but no sub scripts, therefore, do a sub 
2520
									and apply templates to all tokens counted by cndScript. -->
2521
                  <sup>
2522
                              <xsl:apply-templates mode="mml" select="*[position() &gt; 1 and (position() &lt;= ($cndScript + 1))]"/>
2523
                           </sup>
2524
                        </sSup>
2525
                     </xsl:otherwise>
2526
                  </xsl:choose>
2527
               </e>
2528

    
2529
               <!-- The prescripts come after the mml:mprescript and if we get here
2530
							we know there exists one such element -->
2531
          <xsl:call-template name="SplitScripts">
2532
                  <xsl:with-param name="ndScripts" select="mml:mprescripts[1]/following-sibling::*"/>
2533
               </xsl:call-template>
2534
            </sPre>
2535
         </xsl:otherwise>
2536
      </xsl:choose>
2537
  </xsl:template>
2538

    
2539
  <!-- Template that determines if ndCur is an equation array.
2540
				
2541
			 ndCur is an equation array if:
2542
			 
2543
			 0.  The table has a columnalign other than the default (center) 
2544
			 1.  There are are no frame lines
2545
			 2.  There are no column lines
2546
			 3.  There are no row lines
2547
			 4.  There is no row with more than 1 column  
2548
			 5.  There is no row with fewer than 1 column
2549
			 6.  There are no labeled rows.			 
2550
			 
2551
	-->
2552
  <xsl:template name="FIsEqArray">
2553
      <xsl:param name="ndCur" select="."/>
2554

    
2555
      <!-- There should be no frame, columnlines, or rowlines -->
2556
    <xsl:choose>
2557
         <xsl:when test="@columnalign!='center'">0</xsl:when>
2558
         <xsl:when test="(not($ndCur/@frame) or $ndCur/@frame='' or $ndCur/@frame='none')                       
2559
			 and (not($ndCur/@mml:frame) or $ndCur/@mml:frame='' or $ndCur/@mml:frame='none')
2560
			 and (not($ndCur/@columnlines) or $ndCur/@columnlines='' or $ndCur/@columnlines='none')                       
2561
			 and (not($ndCur/@mml:columnlines) or $ndCur/@mml:columnlines='' or $ndCur/@mml:columnlines='none')               
2562
			 and (not($ndCur/@rowlines) or $ndCur/@rowlines='' or $ndCur/@rowlines='none')                       
2563
			 and (not($ndCur/@mml:rowlines) or $ndCur/@mml:rowlines='' or $ndCur/@mml:rowlines='none')               
2564
			 and not($ndCur/mml:mtr[count(mml:mtd) &gt; 1])            
2565
			 and not($ndCur/mml:mtr[count(mml:mtd) &lt; 1])
2566
			 and not($ndCur/mml:mlabeledtr)">1</xsl:when>
2567
         <xsl:otherwise>0</xsl:otherwise>
2568
      </xsl:choose>
2569
  </xsl:template>
2570

    
2571
  <!-- Template used to determine if we should ignore a collection when iterating through 
2572
	     a mathml equation array row.
2573
	
2574
			 So far, the only thing that needs to be ignored is the argument of an nary.  We
2575
			 can ignore this since it is output when we apply-templates to the munder[over]/msub[sup].
2576
	-->
2577
  <xsl:template name="FIgnoreCollection">
2578
      <xsl:param name="ndCur" select="."/>
2579

    
2580
      <xsl:variable name="fNaryArgument">
2581
         <xsl:call-template name="FIsNaryArgument">
2582
            <xsl:with-param name="ndCur" select="$ndCur"/>
2583
         </xsl:call-template>
2584
      </xsl:variable>
2585

    
2586
      <xsl:choose>
2587
         <xsl:when test="$fNaryArgument=1">1</xsl:when>
2588
         <xsl:otherwise>0</xsl:otherwise>
2589
      </xsl:choose>
2590
  </xsl:template>
2591

    
2592
  <!-- Template used to determine if we've already encountered an maligngroup or malignmark.
2593
	
2594
			 This is needed because omml has an implicit spacing alignment (omml spacing alignment = 
2595
			 mathml's maligngroup element) at the beginning of each equation array row.  Therefore, 
2596
			 the first maligngroup (implied or explicit) we encounter does not need to be output.  
2597
			 This template recursively searches up the xml tree and looks at previous siblings to see 
2598
			 if they have a descendant that is an maligngroup or malignmark.  We look for the malignmark 
2599
			 to find the implicit maligngroup.
2600
	-->
2601
  <xsl:template name="FFirstAlignAlreadyFound">
2602
      <xsl:param name="ndCur" select="."/>
2603

    
2604
      <xsl:choose>
2605
         <xsl:when test="count($ndCur/preceding-sibling::*[descendant-or-self::mml:maligngroup                                                 or descendant-or-self::mml:malignmark]) &gt; 0">1</xsl:when>
2606
         <xsl:when test="not($ndCur/parent::mml:mtd)">
2607
            <xsl:call-template name="FFirstAlignAlreadyFound">
2608
               <xsl:with-param name="ndCur" select="$ndCur/parent::*"/>
2609
            </xsl:call-template>
2610
         </xsl:when>
2611
         <xsl:otherwise>0</xsl:otherwise>
2612
      </xsl:choose>
2613
  </xsl:template>
2614

    
2615
  <!-- This template builds a string that is result of concatenating a given string several times. 
2616
	
2617
			 Given strToRepeat, create a string that has strToRepeat repeated iRepitions times. 
2618
	-->
2619
  <xsl:template name="ConcatStringRepeat">
2620
      <xsl:param name="strToRepeat" select="''"/>
2621
      <xsl:param name="iRepetitions" select="0"/>
2622
      <xsl:param name="strBuilding" select="''"/>
2623

    
2624
      <xsl:choose>
2625
         <xsl:when test="$iRepetitions &lt;= 0">
2626
            <xsl:value-of select="$strBuilding"/>
2627
         </xsl:when>
2628
         <xsl:otherwise>
2629
            <xsl:call-template name="ConcatStringRepeat">
2630
               <xsl:with-param name="strToRepeat" select="$strToRepeat"/>
2631
               <xsl:with-param name="iRepetitions" select="$iRepetitions - 1"/>
2632
               <xsl:with-param name="strBuilding" select="concat($strBuilding, $strToRepeat)"/>
2633
            </xsl:call-template>
2634
         </xsl:otherwise>
2635
      </xsl:choose>
2636
  </xsl:template>
2637

    
2638
  <!-- This template determines if ndCur is a special collection.
2639
			 By special collection, I mean is ndCur the outer element of some special grouping 
2640
			 of mathml elements that actually represents some over all omml structure.
2641
			 
2642
			 For instance, is ndCur a linear fraction, or an omml function.
2643
	-->
2644
  <xsl:template name="FSpecialCollection">
2645
      <xsl:param name="ndCur" select="."/>
2646
      <xsl:choose>
2647
         <xsl:when test="$ndCur/self::mml:mrow">
2648
            <xsl:variable name="fLinearFraction">
2649
               <xsl:call-template name="FLinearFrac">
2650
                  <xsl:with-param name="ndCur" select="$ndCur"/>
2651
               </xsl:call-template>
2652
            </xsl:variable>
2653
            <xsl:variable name="fFunc">
2654
               <xsl:call-template name="FIsFunc">
2655
                  <xsl:with-param name="ndCur" select="$ndCur"/>
2656
               </xsl:call-template>
2657
            </xsl:variable>
2658
            <xsl:choose>
2659
               <xsl:when test="$fLinearFraction=1 or $fFunc=1">1</xsl:when>
2660
               <xsl:otherwise>0</xsl:otherwise>
2661
            </xsl:choose>
2662
         </xsl:when>
2663
         <xsl:otherwise>0</xsl:otherwise>
2664
      </xsl:choose>
2665
  </xsl:template>
2666

    
2667
  <!-- This template iterates through the children of an equation array row (mtr) and outputs
2668
	     the equation.
2669
			 
2670
			 This template does all the work to output ampersands and skip the right elements when needed.
2671
	-->
2672
  <xsl:template name="ProcessEqArrayRow">
2673
      <xsl:param name="ndCur" select="."/>
2674

    
2675
      <xsl:for-each select="$ndCur/*">
2676
         <xsl:variable name="fSpecialCollection">
2677
            <xsl:call-template name="FSpecialCollection">
2678
               <xsl:with-param name="ndCur" select="."/>
2679
            </xsl:call-template>
2680
         </xsl:variable>
2681
         <xsl:variable name="fIgnoreCollection">
2682
            <xsl:call-template name="FIgnoreCollection">
2683
               <xsl:with-param name="ndCur" select="."/>
2684
            </xsl:call-template>
2685
         </xsl:variable>
2686
         <xsl:choose>
2687
        <!-- If we have an alignment element output the ampersand. -->
2688
        <xsl:when test="self::mml:maligngroup or self::mml:malignmark">
2689
          <!-- Omml has an implied spacing alignment at the beginning of each equation.
2690
					     Therefore, if this is the first ampersand to be output, don't actually output. -->
2691
          <xsl:variable name="fFirstAlignAlreadyFound">
2692
                  <xsl:call-template name="FFirstAlignAlreadyFound">
2693
                     <xsl:with-param name="ndCur" select="."/>
2694
                  </xsl:call-template>
2695
               </xsl:variable>
2696
               <!-- Don't output unless it is an malignmark or we have already previously found an alignment point. -->
2697
          <xsl:if test="self::mml:malignmark or $fFirstAlignAlreadyFound=1">
2698
                  <r>
2699
                     <t>&amp;</t>
2700
                  </r>
2701
               </xsl:if>
2702
            </xsl:when>
2703
            <!-- If this node is an non-special mrow or mstyle and we aren't supposed to ignore this collection, then
2704
				     go ahead an apply templates to this node. -->
2705
        <xsl:when test="$fIgnoreCollection=0 and ((self::mml:mrow and $fSpecialCollection=0) or self::mml:mstyle)">
2706
               <xsl:call-template name="ProcessEqArrayRow">
2707
                  <xsl:with-param name="ndCur" select="."/>
2708
               </xsl:call-template>
2709
            </xsl:when>
2710
            <!-- At this point we have some mathml structure (fraction, nary, non-grouping element, etc.) -->
2711
        <!-- If this mathml structure has alignment groups or marks as children, then extract those since
2712
				     omml can't handle that. -->
2713
        <xsl:when test="descendant::mml:maligngroup or descendant::mml:malignmark">
2714
               <xsl:variable name="cMalignGroups">
2715
                  <xsl:value-of select="count(descendant::mml:maligngroup)"/>
2716
               </xsl:variable>
2717
               <xsl:variable name="cMalignMarks">
2718
                  <xsl:value-of select="count(descendant::mml:malignmark)"/>
2719
               </xsl:variable>
2720
               <!-- Output all maligngroups and malignmarks as '&amp;' -->
2721
          <xsl:if test="$cMalignGroups + $cMalignMarks &gt; 0">
2722
                  <xsl:variable name="str">
2723
                     <xsl:call-template name="ConcatStringRepeat">
2724
                        <xsl:with-param name="strToRepeat" select="'&amp;'"/>
2725
                        <xsl:with-param name="iRepetitions" select="$cMalignGroups + $cMalignMarks"/>
2726
                        <xsl:with-param name="strBuilding" select="''"/>
2727
                     </xsl:call-template>
2728
                  </xsl:variable>
2729
                  <r>
2730
                     <t>
2731
                        <xsl:call-template name="OutputText">
2732
                           <xsl:with-param name="sInput" select="$str"/>
2733
                        </xsl:call-template>
2734
                     </t>
2735
                  </r>
2736
               </xsl:if>
2737
               <!-- Now that the '&amp;' have been extracted, just apply-templates to this node.-->
2738
          <xsl:apply-templates mode="mml" select="."/>
2739
            </xsl:when>
2740
            <!-- If there are no alignment points as descendants, then go ahead and output this node. -->
2741
        <xsl:otherwise>
2742
               <xsl:apply-templates mode="mml" select="."/>
2743
            </xsl:otherwise>
2744
         </xsl:choose>
2745
      </xsl:for-each>
2746
  </xsl:template>
2747

    
2748
  <!-- This template transforms mtable into its appropriate omml type.
2749
	
2750
			 There are two possible omml constructs that an mtable can become:  a matrix or 
2751
			 an equation array.
2752
			 
2753
			 Because omml has no generic table construct, the omml matrix is the best approximate
2754
			 for a mathml table.
2755
			 
2756
			 Our equation array transformation is very simple.  The main goal of this transform is to
2757
			 allow roundtripping omml eq arrays through mathml.  The template ProcessEqArrayRow was never
2758
			 intended to account for many of the alignment flexibilities that are present in mathml like 
2759
			 using the alig attribute, using alignmark attribute in token elements, etc.
2760
			 
2761
			 The restrictions on this transform require <malignmark> and <maligngroup> elements to be outside of
2762
			 any non-grouping mathml elements (that is, mrow and mstyle).  Moreover, these elements cannot be the children of
2763
			 mrows that represent linear fractions or functions.  Also, <malignmark> cannot be a child
2764
			 of token attributes.
2765
			 
2766
			 In the case that the above 
2767
	
2768
	-->
2769
  <xsl:template mode="mml" match="mml:mtable">
2770
      <xsl:variable name="fEqArray">
2771
         <xsl:call-template name="FIsEqArray">
2772
            <xsl:with-param name="ndCur" select="."/>
2773
         </xsl:call-template>
2774
      </xsl:variable>
2775
      <xsl:variable name="alignment">
2776
	<xsl:value-of select="@columnalign"/>
2777
      </xsl:variable>
2778
      <xsl:choose>
2779
         <xsl:when test="$fEqArray=1">
2780
            <eqArr>
2781
	      <xsl:if test="$alignment!=''">
2782
		<eqArrPr>
2783
		  <baseJc m:val="{$alignment}"/>
2784
		</eqArrPr>
2785
	      </xsl:if>
2786
               <xsl:for-each select="mml:mtr">
2787
                  <e>
2788
                     <xsl:call-template name="ProcessEqArrayRow">
2789
                        <xsl:with-param name="ndCur" select="mml:mtd"/>
2790
                     </xsl:call-template>
2791
                  </e>
2792
               </xsl:for-each>
2793
            </eqArr>
2794
         </xsl:when>
2795
         <xsl:otherwise>
2796
            <xsl:variable name="cMaxElmtsInRow">
2797
               <xsl:call-template name="CountMaxElmtsInRow">
2798
                  <xsl:with-param name="ndCur" select="*[1]"/>
2799
                  <xsl:with-param name="cMaxElmtsInRow" select="0"/>
2800
               </xsl:call-template>
2801
            </xsl:variable>
2802
            <m>
2803
               <mPr>
2804
                  <baseJc m:val="center"/>
2805
                  <plcHide m:val="on"/>
2806
                  <mcs>
2807
                     <mc>
2808
                        <mcPr>
2809
                           <count>
2810
                              <xsl:attribute name="m:val">
2811
                                 <xsl:value-of select="$cMaxElmtsInRow"/>
2812
                              </xsl:attribute>
2813
                           </count>
2814
                           <mcJc m:val="{$alignment}"/>
2815
                        </mcPr>
2816
                     </mc>
2817
                  </mcs>
2818
               </mPr>
2819
               <xsl:for-each select="*">
2820
                  <xsl:choose>
2821
                     <xsl:when test="self::mml:mtr or self::mml:mlabeledtr">
2822
                        <mr>
2823
                           <xsl:choose>
2824
                              <xsl:when test="self::mml:mtr">
2825
                                 <xsl:for-each select="*">
2826
                                    <e>
2827
                                       <xsl:apply-templates mode="mml" select="."/>
2828
                                    </e>
2829
                                 </xsl:for-each>
2830
                                 <xsl:call-template name="CreateEmptyElmt">
2831
                                    <xsl:with-param name="cEmptyMtd" select="$cMaxElmtsInRow - count(*)"/>
2832
                                 </xsl:call-template>
2833
                              </xsl:when>
2834
                              <xsl:otherwise>
2835
                                 <xsl:for-each select="*[position() &gt; 1]">
2836
                                    <e>
2837
                                       <xsl:apply-templates mode="mml" select="."/>
2838
                                    </e>
2839
                                 </xsl:for-each>
2840
                                 <xsl:call-template name="CreateEmptyElmt">
2841
                                    <xsl:with-param name="cEmptyMtd" select="$cMaxElmtsInRow - (count(*) - 1)"/>
2842
                                 </xsl:call-template>
2843
                              </xsl:otherwise>
2844
                           </xsl:choose>
2845
                        </mr>
2846
                     </xsl:when>
2847
                     <xsl:otherwise>
2848
                        <mr>
2849
                           <e>
2850
                              <xsl:apply-templates mode="mml" select="."/>
2851
                           </e>
2852
                           <xsl:call-template name="CreateEmptyElmt">
2853
                              <xsl:with-param name="cEmptyMtd" select="$cMaxElmtsInRow - 1"/>
2854
                           </xsl:call-template>
2855
                        </mr>
2856
                     </xsl:otherwise>
2857
                  </xsl:choose>
2858
               </xsl:for-each>
2859
            </m>
2860
         </xsl:otherwise>
2861
      </xsl:choose>
2862
  </xsl:template>
2863
  <xsl:template mode="mml" match="m:mtd">
2864
      <xsl:apply-templates mode="mml"/>
2865
  </xsl:template>
2866
  <xsl:template name="CreateEmptyElmt">
2867
      <xsl:param name="cEmptyMtd"/>
2868
      <xsl:if test="$cEmptyMtd &gt; 0">
2869
         <e/>
2870
         <xsl:call-template name="CreateEmptyElmt">
2871
            <xsl:with-param name="cEmptyMtd" select="$cEmptyMtd - 1"/>
2872
         </xsl:call-template>
2873
      </xsl:if>
2874
  </xsl:template>
2875
  <xsl:template name="CountMaxElmtsInRow">
2876
      <xsl:param name="ndCur"/>
2877
      <xsl:param name="cMaxElmtsInRow" select="0"/>
2878
      <xsl:choose>
2879
         <xsl:when test="not($ndCur)">
2880
            <xsl:value-of select="$cMaxElmtsInRow"/>
2881
         </xsl:when>
2882
         <xsl:otherwise>
2883
            <xsl:call-template name="CountMaxElmtsInRow">
2884
               <xsl:with-param name="ndCur" select="$ndCur/following-sibling::*[1]"/>
2885
               <xsl:with-param name="cMaxElmtsInRow">
2886
                  <xsl:choose>
2887
                     <xsl:when test="local-name($ndCur) = 'mlabeledtr' and                      namespace-uri($ndCur) = 'http://www.w3.org/1998/Math/MathML'">
2888
                        <xsl:choose>
2889
                           <xsl:when test="(count($ndCur/*) - 1) &gt; $cMaxElmtsInRow">
2890
                              <xsl:value-of select="count($ndCur/*) - 1"/>
2891
                           </xsl:when>
2892
                           <xsl:otherwise>
2893
                              <xsl:value-of select="$cMaxElmtsInRow"/>
2894
                           </xsl:otherwise>
2895
                        </xsl:choose>
2896
                     </xsl:when>
2897
                     <xsl:when test="local-name($ndCur) = 'mtr' and                      namespace-uri($ndCur) = 'http://www.w3.org/1998/Math/MathML'">
2898
                        <xsl:choose>
2899
                           <xsl:when test="count($ndCur/*) &gt; $cMaxElmtsInRow">
2900
                              <xsl:value-of select="count($ndCur/*)"/>
2901
                           </xsl:when>
2902
                           <xsl:otherwise>
2903
                              <xsl:value-of select="$cMaxElmtsInRow"/>
2904
                           </xsl:otherwise>
2905
                        </xsl:choose>
2906
                     </xsl:when>
2907
                     <xsl:otherwise>
2908
                        <xsl:choose>
2909
                           <xsl:when test="1 &gt; $cMaxElmtsInRow">
2910
                              <xsl:value-of select="1"/>
2911
                           </xsl:when>
2912
                           <xsl:otherwise>
2913
                              <xsl:value-of select="$cMaxElmtsInRow"/>
2914
                           </xsl:otherwise>
2915
                        </xsl:choose>
2916
                     </xsl:otherwise>
2917
                  </xsl:choose>
2918
               </xsl:with-param>
2919
            </xsl:call-template>
2920
         </xsl:otherwise>
2921
      </xsl:choose>
2922
  </xsl:template>
2923
  <xsl:template mode="mml" match="mml:mglyph">
2924
      <xsl:call-template name="CreateMglyph"/>
2925
  </xsl:template>
2926
  <xsl:template mode="mml"
2927
                 match="mml:mi[child::mml:mglyph] |                        mml:mn[child::mml:mglyph] |                        mml:mo[child::mml:mglyph] |                        mml:ms[child::mml:mglyph] |                        mml:mtext[child::mml:mglyph]">
2928
      <xsl:if test="string-length(normalize-space(.)) &gt; 0">
2929
         <r>
2930
            <xsl:call-template name="CreateRunProp">
2931
               <xsl:with-param name="mathvariant">
2932
                  <xsl:choose>
2933
                     <xsl:when test="@mathvariant">
2934
                        <xsl:value-of select="@mathvariant"/>
2935
                     </xsl:when>
2936
                     <xsl:otherwise>
2937
                        <xsl:value-of select="@mml:mathvariant"/>
2938
                     </xsl:otherwise>
2939
                  </xsl:choose>
2940
               </xsl:with-param>
2941
               <xsl:with-param name="fontstyle">
2942
                  <xsl:choose>
2943
                     <xsl:when test="@fontstyle">
2944
                        <xsl:value-of select="@fontstyle"/>
2945
                     </xsl:when>
2946
                     <xsl:otherwise>
2947
                        <xsl:value-of select="@mml:fontstyle"/>
2948
                     </xsl:otherwise>
2949
                  </xsl:choose>
2950
               </xsl:with-param>
2951
               <xsl:with-param name="fontweight">
2952
                  <xsl:choose>
2953
                     <xsl:when test="@fontweight">
2954
                        <xsl:value-of select="@fontweight"/>
2955
                     </xsl:when>
2956
                     <xsl:otherwise>
2957
                        <xsl:value-of select="@mml:fontweight"/>
2958
                     </xsl:otherwise>
2959
                  </xsl:choose>
2960
               </xsl:with-param>
2961
               <xsl:with-param name="mathcolor">
2962
                  <xsl:choose>
2963
                     <xsl:when test="@mathcolor">
2964
                        <xsl:value-of select="@mathcolor"/>
2965
                     </xsl:when>
2966
                     <xsl:otherwise>
2967
                        <xsl:value-of select="@mml:mathcolor"/>
2968
                     </xsl:otherwise>
2969
                  </xsl:choose>
2970
               </xsl:with-param>
2971
               <xsl:with-param name="mathsize">
2972
                  <xsl:choose>
2973
                     <xsl:when test="@mathsize">
2974
                        <xsl:value-of select="@mathsize"/>
2975
                     </xsl:when>
2976
                     <xsl:otherwise>
2977
                        <xsl:value-of select="@mml:mathsize"/>
2978
                     </xsl:otherwise>
2979
                  </xsl:choose>
2980
               </xsl:with-param>
2981
               <xsl:with-param name="color">
2982
                  <xsl:choose>
2983
                     <xsl:when test="@color">
2984
                        <xsl:value-of select="@color"/>
2985
                     </xsl:when>
2986
                     <xsl:otherwise>
2987
                        <xsl:value-of select="@mml:color"/>
2988
                     </xsl:otherwise>
2989
                  </xsl:choose>
2990
               </xsl:with-param>
2991
               <xsl:with-param name="fontsize">
2992
                  <xsl:choose>
2993
                     <xsl:when test="@fontsize">
2994
                        <xsl:value-of select="@fontsize"/>
2995
                     </xsl:when>
2996
                     <xsl:otherwise>
2997
                        <xsl:value-of select="@mml:fontsize"/>
2998
                     </xsl:otherwise>
2999
                  </xsl:choose>
3000
               </xsl:with-param>
3001
               <xsl:with-param name="fNor">
3002
                  <xsl:call-template name="FNor">
3003
                     <xsl:with-param name="ndCur" select="."/>
3004
                  </xsl:call-template>
3005
               </xsl:with-param>
3006
               <xsl:with-param name="fLit">
3007
                  <xsl:call-template name="FLit">
3008
                     <xsl:with-param name="ndCur" select="."/>
3009
                  </xsl:call-template>
3010
               </xsl:with-param>
3011
               <xsl:with-param name="ndCur" select="."/>
3012
            </xsl:call-template>
3013
            <t>
3014
               <xsl:call-template name="OutputText">
3015
                  <xsl:with-param name="sInput">
3016
                     <xsl:choose>
3017
                        <xsl:when test="self::mml:ms">
3018
                           <xsl:call-template name="OutputMs">
3019
                              <xsl:with-param name="msCur" select="."/>
3020
                           </xsl:call-template>
3021
                        </xsl:when>
3022
                        <xsl:otherwise>
3023
                           <xsl:value-of select="normalize-space(.)"/>
3024
                        </xsl:otherwise>
3025
                     </xsl:choose>
3026
                  </xsl:with-param>
3027
               </xsl:call-template>
3028
            </t>
3029
         </r>
3030
      </xsl:if>
3031
      <xsl:for-each select="child::mml:mglyph">
3032
         <xsl:call-template name="CreateMglyph">
3033
            <xsl:with-param name="ndCur" select="."/>
3034
         </xsl:call-template>
3035
      </xsl:for-each>
3036
  </xsl:template>
3037
  <xsl:template name="FGlyphIndexOk">
3038
      <xsl:param name="index"/>
3039
      <xsl:if test="$index != ''">
3040
         <xsl:choose>
3041
            <xsl:when test="string(number(string(floor($index)))) = 'NaN'"/>
3042
            <xsl:when test="number($index) &lt; 32 and not(number($index) = 9 or number($index) = 10 or number($index) = 13)"/>
3043
            <xsl:when test="number($index) = 65534 or number($index) = 65535"/>
3044
            <xsl:otherwise>1</xsl:otherwise>
3045
         </xsl:choose>
3046
      </xsl:if>
3047
  </xsl:template>
3048
  <xsl:template name="CreateMglyph">
3049
      <xsl:param name="ndCur"/>
3050
      <r>
3051
         <xsl:call-template name="CreateRunProp">
3052
            <xsl:with-param name="mathvariant">
3053
               <xsl:choose>
3054
                  <xsl:when test="(not(@mathvariant) or @mathvariant='')                              and (not(@mml:mathvariant) or @mml:mathvariant='')                             and (../@mathvariant!='' or ../@mml:mathvariant!='')">
3055
                     <xsl:choose>
3056
                        <xsl:when test="../@mathvariant">
3057
                           <xsl:value-of select="../@mathvariant"/>
3058
                        </xsl:when>
3059
                        <xsl:otherwise>
3060
                           <xsl:value-of select="../mml:mathvariant"/>
3061
                        </xsl:otherwise>
3062
                     </xsl:choose>
3063
                  </xsl:when>
3064
                  <xsl:otherwise>
3065
                     <xsl:choose>
3066
                        <xsl:when test="@mathvariant">
3067
                           <xsl:value-of select="@mathvariant"/>
3068
                        </xsl:when>
3069
                        <xsl:otherwise>
3070
                           <xsl:value-of select="@mml:mathvariant"/>
3071
                        </xsl:otherwise>
3072
                     </xsl:choose>
3073
                  </xsl:otherwise>
3074
               </xsl:choose>
3075
            </xsl:with-param>
3076
            <xsl:with-param name="fontstyle">
3077
               <xsl:choose>
3078
                  <xsl:when test="(not(@fontstyle) or @fontstyle='')                              and (not(@mml:fontstyle) or @mml:fontstyle='')                             and (../@fontstyle!='' or ../@mml:fontstyle!='')">
3079
                     <xsl:choose>
3080
                        <xsl:when test="../@fontstyle">
3081
                           <xsl:value-of select="../@fontstyle"/>
3082
                        </xsl:when>
3083
                        <xsl:otherwise>
3084
                           <xsl:value-of select="../@mml:fontstyle"/>
3085
                        </xsl:otherwise>
3086
                     </xsl:choose>
3087
                  </xsl:when>
3088
                  <xsl:otherwise>
3089
                     <xsl:choose>
3090
                        <xsl:when test="@fontstyle">
3091
                           <xsl:value-of select="@fontstyle"/>
3092
                        </xsl:when>
3093
                        <xsl:otherwise>
3094
                           <xsl:value-of select="@mml:fontstyle"/>
3095
                        </xsl:otherwise>
3096
                     </xsl:choose>
3097
                  </xsl:otherwise>
3098
               </xsl:choose>
3099
            </xsl:with-param>
3100
            <xsl:with-param name="fontweight">
3101
               <xsl:choose>
3102
                  <xsl:when test="(not(@fontweight) or @fontweight='')                              and (not(@mml:fontweight) or @mml:fontweight='')                             and (../@fontweight!='' or ../@mml:fontweight!='')">
3103
                     <xsl:choose>
3104
                        <xsl:when test="../@fontweight">
3105
                           <xsl:value-of select="../@fontweight"/>
3106
                        </xsl:when>
3107
                        <xsl:otherwise>
3108
                           <xsl:value-of select="../@mml:fontweight"/>
3109
                        </xsl:otherwise>
3110
                     </xsl:choose>
3111
                  </xsl:when>
3112
                  <xsl:otherwise>
3113
                     <xsl:choose>
3114
                        <xsl:when test="@fontweight">
3115
                           <xsl:value-of select="@fontweight"/>
3116
                        </xsl:when>
3117
                        <xsl:otherwise>
3118
                           <xsl:value-of select="@mml:fontweight"/>
3119
                        </xsl:otherwise>
3120
                     </xsl:choose>
3121
                  </xsl:otherwise>
3122
               </xsl:choose>
3123
            </xsl:with-param>
3124
            <xsl:with-param name="mathcolor">
3125
               <xsl:choose>
3126
                  <xsl:when test="(not(@mathcolor) or @mathcolor='')                              and (not(@mml:mathcolor) or @mml:mathcolor='')                             and (../@mathcolor!='' or ../@mml:mathcolor!='')">
3127
                     <xsl:choose>
3128
                        <xsl:when test="../@mathcolor">
3129
                           <xsl:value-of select="../@mathcolor"/>
3130
                        </xsl:when>
3131
                        <xsl:otherwise>
3132
                           <xsl:value-of select="../@mml:mathcolor"/>
3133
                        </xsl:otherwise>
3134
                     </xsl:choose>
3135
                  </xsl:when>
3136
                  <xsl:otherwise>
3137
                     <xsl:choose>
3138
                        <xsl:when test="@mathcolor">
3139
                           <xsl:value-of select="@mathcolor"/>
3140
                        </xsl:when>
3141
                        <xsl:otherwise>
3142
                           <xsl:value-of select="@mml:mathcolor"/>
3143
                        </xsl:otherwise>
3144
                     </xsl:choose>
3145
                  </xsl:otherwise>
3146
               </xsl:choose>
3147
            </xsl:with-param>
3148
            <xsl:with-param name="mathsize">
3149
               <xsl:choose>
3150
                  <xsl:when test="(not(@mathsize) or @mathsize='')                             and (not(@mml:mathsize) or @mml:mathsize='')                             and (../@mathsize!='' or ../@mml:mathsize!='')">
3151
                     <xsl:choose>
3152
                        <xsl:when test="../@mathsize">
3153
                           <xsl:value-of select="../@mathsize"/>
3154
                        </xsl:when>
3155
                        <xsl:otherwise>
3156
                           <xsl:value-of select="../@mathsize"/>
3157
                        </xsl:otherwise>
3158
                     </xsl:choose>
3159
                  </xsl:when>
3160
                  <xsl:otherwise>
3161
                     <xsl:choose>
3162
                        <xsl:when test="@mathsize">
3163
                           <xsl:value-of select="@mathsize"/>
3164
                        </xsl:when>
3165
                        <xsl:otherwise>
3166
                           <xsl:value-of select="@mml:mathsize"/>
3167
                        </xsl:otherwise>
3168
                     </xsl:choose>
3169
                  </xsl:otherwise>
3170
               </xsl:choose>
3171
            </xsl:with-param>
3172
            <xsl:with-param name="color">
3173
               <xsl:choose>
3174
                  <xsl:when test="(not(@color) or @color='')                              and (not(@mml:color) or @mml:color='')                             and (../@color!='' or ../@mml:color!='')">
3175
                     <xsl:choose>
3176
                        <xsl:when test="../@color">
3177
                           <xsl:value-of select="../@color"/>
3178
                        </xsl:when>
3179
                        <xsl:otherwise>
3180
                           <xsl:value-of select="../@mml:color"/>
3181
                        </xsl:otherwise>
3182
                     </xsl:choose>
3183
                  </xsl:when>
3184
                  <xsl:otherwise>
3185
                     <xsl:choose>
3186
                        <xsl:when test="@color">
3187
                           <xsl:value-of select="@color"/>
3188
                        </xsl:when>
3189
                        <xsl:otherwise>
3190
                           <xsl:value-of select="@mml:color"/>
3191
                        </xsl:otherwise>
3192
                     </xsl:choose>
3193
                  </xsl:otherwise>
3194
               </xsl:choose>
3195
            </xsl:with-param>
3196
            <xsl:with-param name="fontsize">
3197
               <xsl:choose>
3198
                  <xsl:when test="(not(@fontsize) or @fontsize='')                             and (not(@mml:fontsize) or @mml:fontsize='')                             and (../@fontsize!='' or ../@mml:fontsize!='')">
3199
                     <xsl:choose>
3200
                        <xsl:when test="../@fontsize">
3201
                           <xsl:value-of select="../@fontsize"/>
3202
                        </xsl:when>
3203
                        <xsl:otherwise>
3204
                           <xsl:value-of select="../@mml:fontsize"/>
3205
                        </xsl:otherwise>
3206
                     </xsl:choose>
3207
                  </xsl:when>
3208
                  <xsl:otherwise>
3209
                     <xsl:choose>
3210
                        <xsl:when test="@fontsize">
3211
                           <xsl:value-of select="@fontsize"/>
3212
                        </xsl:when>
3213
                        <xsl:otherwise>
3214
                           <xsl:value-of select="@mml:fontsize"/>
3215
                        </xsl:otherwise>
3216
                     </xsl:choose>
3217
                  </xsl:otherwise>
3218
               </xsl:choose>
3219
            </xsl:with-param>
3220
            <xsl:with-param name="ndCur" select="."/>
3221
            <xsl:with-param name="font-family">
3222
               <xsl:choose>
3223
                  <xsl:when test="@fontfamily">
3224
                     <xsl:value-of select="@fontfamily"/>
3225
                  </xsl:when>
3226
                  <xsl:otherwise>
3227
                     <xsl:value-of select="@mml:fontfamily"/>
3228
                  </xsl:otherwise>
3229
               </xsl:choose>
3230
            </xsl:with-param>
3231
            <xsl:with-param name="fNor">
3232
               <xsl:call-template name="FNor">
3233
                  <xsl:with-param name="ndCur" select="."/>
3234
               </xsl:call-template>
3235
            </xsl:with-param>
3236
            <xsl:with-param name="fLit">
3237
               <xsl:call-template name="FLit">
3238
                  <xsl:with-param name="ndCur" select="."/>
3239
               </xsl:call-template>
3240
            </xsl:with-param>
3241
         </xsl:call-template>
3242
         <xsl:variable name="shouldGlyphUseIndex">
3243
            <xsl:call-template name="FGlyphIndexOk">
3244
               <xsl:with-param name="index">
3245
                  <xsl:choose>
3246
                     <xsl:when test="@index">
3247
                        <xsl:value-of select="@index"/>
3248
                     </xsl:when>
3249
                     <xsl:otherwise>
3250
                        <xsl:value-of select="@mml:index"/>
3251
                     </xsl:otherwise>
3252
                  </xsl:choose>
3253
               </xsl:with-param>
3254
            </xsl:call-template>
3255
         </xsl:variable>
3256
         <xsl:choose>
3257
            <xsl:when test="not($shouldGlyphUseIndex = '1')">
3258
               <t>
3259
                  <xsl:choose>
3260
                     <xsl:when test="@alt">
3261
                        <xsl:value-of select="@alt"/>
3262
                     </xsl:when>
3263
                     <xsl:otherwise>
3264
                        <xsl:value-of select="@mml:alt"/>
3265
                     </xsl:otherwise>
3266
                  </xsl:choose>
3267
               </t>
3268
            </xsl:when>
3269
            <xsl:otherwise>
3270
               <xsl:variable name="nHexIndex">
3271
                  <xsl:call-template name="ConvertDecToHex">
3272
                     <xsl:with-param name="index">
3273
                        <xsl:choose>
3274
                           <xsl:when test="@index">
3275
                              <xsl:value-of select="@index"/>
3276
                           </xsl:when>
3277
                           <xsl:otherwise>
3278
                              <xsl:value-of select="@mml:index"/>
3279
                           </xsl:otherwise>
3280
                        </xsl:choose>
3281
                     </xsl:with-param>
3282
                  </xsl:call-template>
3283
               </xsl:variable>
3284
               <t>
3285
                  <xsl:text disable-output-escaping="yes">&amp;#x</xsl:text>
3286
                  <xsl:value-of select="$nHexIndex"/>
3287
                  <xsl:text>;</xsl:text>
3288
               </t>
3289
            </xsl:otherwise>
3290
         </xsl:choose>
3291
      </r>
3292
  </xsl:template>
3293
  <xsl:template name="ConvertDecToHex">
3294
      <xsl:param name="index"/>
3295
      <xsl:if test="$index &gt; 0">
3296
         <xsl:call-template name="ConvertDecToHex">
3297
            <xsl:with-param name="index" select="floor($index div 16)"/>
3298
         </xsl:call-template>
3299
         <xsl:choose>
3300
            <xsl:when test="$index mod 16 &lt; 10">
3301
               <xsl:value-of select="$index mod 16"/>
3302
            </xsl:when>
3303
            <xsl:otherwise>
3304
               <xsl:choose>
3305
                  <xsl:when test="$index mod 16 = 10">A</xsl:when>
3306
                  <xsl:when test="$index mod 16 = 11">B</xsl:when>
3307
                  <xsl:when test="$index mod 16 = 12">C</xsl:when>
3308
                  <xsl:when test="$index mod 16 = 13">D</xsl:when>
3309
                  <xsl:when test="$index mod 16 = 14">E</xsl:when>
3310
                  <xsl:when test="$index mod 16 = 15">F</xsl:when>
3311
                  <xsl:otherwise>A</xsl:otherwise>
3312
               </xsl:choose>
3313
            </xsl:otherwise>
3314
         </xsl:choose>
3315
      </xsl:if>
3316
  </xsl:template>
3317

    
3318
  <xsl:template name="FStrContainsNonZeroDigit">
3319
      <xsl:param name="s"/>
3320

    
3321
      <!-- Translate any nonzero digit into a 9 -->
3322
    <xsl:variable name="sNonZeroDigitsToNineDigit" select="translate($s, '12345678', '99999999')"/>
3323
      <xsl:choose>
3324
      <!-- Search for 9s -->
3325
      <xsl:when test="contains($sNonZeroDigitsToNineDigit, '9')">1</xsl:when>
3326
         <xsl:otherwise>0</xsl:otherwise>
3327
      </xsl:choose>
3328
  </xsl:template>
3329

    
3330
  <xsl:template name="FStrContainsDigits">
3331
      <xsl:param name="s"/>
3332

    
3333
      <!-- Translate any digit into a 0 -->
3334
    <xsl:variable name="sDigitsToZeroDigit" select="translate($s, '123456789', '000000000')"/>
3335
      <xsl:choose>
3336
      <!-- Search for 0s -->
3337
      <xsl:when test="contains($sDigitsToZeroDigit, '0')">1</xsl:when>
3338
         <xsl:otherwise>0</xsl:otherwise>
3339
      </xsl:choose>
3340
  </xsl:template>
3341

    
3342

    
3343
  <!-- Used to determine if mpadded attribute {width, height, depth } 
3344
       indicates to show everything. 
3345
       
3346
       Unlike mathml, whose mpadded structure has great flexibility in modifying the 
3347
       bounding box's width, height, and depth, Word can only have zero or full width, height, and depth.
3348
       Thus, if the width, height, or depth attributes indicate any kind of nonzero width, height, 
3349
       or depth, we'll translate that into a show full width, height, or depth for OMML.  Only if the attribute
3350
       indicates a zero width, height, or depth, will we report back FFull as false.
3351
       
3352
       Example:  s=0%    ->  FFull returns 0.
3353
                 s=2%    ->  FFull returns 1.
3354
                 s=0.1em ->  FFull returns 1.     
3355
       
3356
       -->
3357
  <xsl:template name="FFull">
3358
      <xsl:param name="s"/>
3359

    
3360
      <xsl:variable name="fStrContainsNonZeroDigit">
3361
         <xsl:call-template name="FStrContainsNonZeroDigit">
3362
            <xsl:with-param name="s" select="$s"/>
3363
         </xsl:call-template>
3364
      </xsl:variable>
3365

    
3366
      <xsl:variable name="fStrContainsDigits">
3367
         <xsl:call-template name="FStrContainsDigits">
3368
            <xsl:with-param name="s" select="$s"/>
3369
         </xsl:call-template>
3370
      </xsl:variable>
3371

    
3372
      <xsl:choose>
3373
      <!-- String contained non-zero digit -->
3374
      <xsl:when test="$fStrContainsNonZeroDigit=1">1</xsl:when>
3375
         <!-- String didn't contain a non-zero digit, but it did contain digits.
3376
           This must mean that all digits in the string were 0s. -->
3377
      <xsl:when test="$fStrContainsDigits=1">0</xsl:when>
3378
         <!-- Else, no digits, therefore, return true.
3379
           We return true in the otherwise condition to take account for the possibility
3380
           in MathML to say something like width="height". -->
3381
      <xsl:otherwise>1</xsl:otherwise>
3382
      </xsl:choose>
3383
  </xsl:template>
3384

    
3385

    
3386
  <!-- Just outputs phant properties, doesn't do any fancy 
3387
       thinking of its own, just obeys the defaults of 
3388
       phants. -->
3389
  <xsl:template name="CreatePhantPropertiesCore">
3390
      <xsl:param name="fShow" select="1"/>
3391
      <xsl:param name="fFullWidth" select="1"/>
3392
      <xsl:param name="fFullHeight" select="1"/>
3393
      <xsl:param name="fFullDepth" select="1"/>
3394

    
3395
      <xsl:if test="$fShow=0                      or $fFullWidth=0                      or $fFullHeight=0                     or $fFullDepth=0">
3396
         <phantPr>
3397
            <xsl:if test="$fShow=0">
3398
               <show>
3399
                  <xsl:attribute name="m:val">off</xsl:attribute>
3400
               </show>
3401
            </xsl:if>
3402
            <xsl:if test="$fFullWidth=0">
3403
               <zeroWid>
3404
                  <xsl:attribute name="m:val">on</xsl:attribute>
3405
               </zeroWid>
3406
            </xsl:if>
3407
            <xsl:if test="$fFullHeight=0">
3408
               <zeroAsc>
3409
                  <xsl:attribute name="m:val">on</xsl:attribute>
3410
               </zeroAsc>
3411
            </xsl:if>
3412
            <xsl:if test="$fFullDepth=0">
3413
               <zeroDesc>
3414
                  <xsl:attribute name="m:val">on</xsl:attribute>
3415
               </zeroDesc>
3416
            </xsl:if>
3417
         </phantPr>
3418
      </xsl:if>
3419
  </xsl:template>
3420

    
3421
  <!-- Figures out if we should factor in width, height, and depth attributes.  
3422
  
3423
       If so, then it 
3424
       gets these attributes, does some processing to figure out what the attributes indicate, 
3425
       then passes these indications to CreatePhantPropertiesCore.  
3426
       
3427
       If we aren't supposed to factor in width, height, or depth, then we'll just output the show
3428
       attribute. -->
3429
  <xsl:template name="CreatePhantProperties">
3430
      <xsl:param name="ndCur" select="."/>
3431
      <xsl:param name="fShow" select="1"/>
3432

    
3433
      <xsl:choose>
3434
      <!-- In the special case that we have an mphantom with one child which is an mpadded, then we should 
3435
           subsume the mpadded attributes into the mphantom attributes.  The test statement below imples the 
3436
           'one child which is an mpadded'.  The first part, that the parent of mpadded is an mphantom, is implied
3437
           by being in this template, which is only called when we've encountered an mphantom.
3438
           
3439
           Word outputs its invisible phantoms with smashing as 
3440

    
3441
              <mml:mphantom>
3442
                <mml:mpadded . . . >
3443
                  
3444
                </mml:mpadded>
3445
              </mml:mphantom>
3446

    
3447
            This test is used to allow roundtripping smashed invisible phantoms. -->
3448
      <xsl:when test="count($ndCur/child::*)=1 and count($ndCur/mml:mpadded)=1">
3449
            <xsl:variable name="sLowerCaseWidth">
3450
               <xsl:choose>
3451
                  <xsl:when test="$ndCur/mml:mpadded/@width">
3452
                     <xsl:value-of select="translate($ndCur/mml:mpadded/@width, $StrUCAlphabet, $StrLCAlphabet)"/>
3453
                  </xsl:when>
3454
                  <xsl:otherwise>
3455
                     <xsl:value-of select="translate($ndCur/mml:mpadded/@mml:width, $StrUCAlphabet, $StrLCAlphabet)"/>
3456
                  </xsl:otherwise>
3457
               </xsl:choose>
3458
            </xsl:variable>
3459
            <xsl:variable name="sLowerCaseHeight">
3460
               <xsl:choose>
3461
                  <xsl:when test="$ndCur/mml:mpadded/@height">
3462
                     <xsl:value-of select="translate($ndCur/mml:mpadded/@height, $StrUCAlphabet, $StrLCAlphabet)"/>
3463
                  </xsl:when>
3464
                  <xsl:otherwise>
3465
                     <xsl:value-of select="translate($ndCur/mml:mpadded/@mml:height, $StrUCAlphabet, $StrLCAlphabet)"/>
3466
                  </xsl:otherwise>
3467
               </xsl:choose>
3468
            </xsl:variable>
3469
            <xsl:variable name="sLowerCaseDepth">
3470
               <xsl:choose>
3471
                  <xsl:when test="$ndCur/mml:mpadded/@depth">
3472
                     <xsl:value-of select="translate($ndCur/mml:mpadded/@depth, $StrUCAlphabet, $StrLCAlphabet)"/>
3473
                  </xsl:when>
3474
                  <xsl:otherwise>
3475
                     <xsl:value-of select="translate($ndCur/mml:mpadded/@mml:depth, $StrUCAlphabet, $StrLCAlphabet)"/>
3476
                  </xsl:otherwise>
3477
               </xsl:choose>
3478
            </xsl:variable>
3479

    
3480
            <xsl:variable name="fFullWidth">
3481
               <xsl:call-template name="FFull">
3482
                  <xsl:with-param name="s" select="$sLowerCaseWidth"/>
3483
               </xsl:call-template>
3484
            </xsl:variable>
3485
            <xsl:variable name="fFullHeight">
3486
               <xsl:call-template name="FFull">
3487
                  <xsl:with-param name="s" select="$sLowerCaseHeight"/>
3488
               </xsl:call-template>
3489
            </xsl:variable>
3490
            <xsl:variable name="fFullDepth">
3491
               <xsl:call-template name="FFull">
3492
                  <xsl:with-param name="s" select="$sLowerCaseDepth"/>
3493
               </xsl:call-template>
3494
            </xsl:variable>
3495

    
3496
            <xsl:call-template name="CreatePhantPropertiesCore">
3497
               <xsl:with-param name="fShow" select="$fShow"/>
3498
               <xsl:with-param name="fFullWidth" select="$fFullWidth"/>
3499
               <xsl:with-param name="fFullHeight" select="$fFullHeight"/>
3500
               <xsl:with-param name="fFullDepth" select="$fFullDepth"/>
3501
            </xsl:call-template>
3502
         </xsl:when>
3503
         <xsl:otherwise>
3504
            <xsl:call-template name="CreatePhantPropertiesCore">
3505
               <xsl:with-param name="fShow" select="$fShow"/>
3506
            </xsl:call-template>
3507
         </xsl:otherwise>
3508
      </xsl:choose>
3509
  </xsl:template>
3510

    
3511
  <xsl:template mode="mml" match="mml:mpadded">
3512
      <xsl:choose>
3513
         <xsl:when test="count(parent::mml:mphantom)=1 and count(preceding-sibling::*)=0 and count(following-sibling::*)=0">
3514
        <!-- This mpadded is inside an mphantom that has already setup phantom attributes, therefore, just apply templates -->
3515
        <xsl:apply-templates mode="mml"/>
3516
         </xsl:when>
3517
         <xsl:otherwise>
3518
            <xsl:variable name="sLowerCaseWidth">
3519
               <xsl:choose>
3520
                  <xsl:when test="@width">
3521
                     <xsl:value-of select="@width"/>
3522
                  </xsl:when>
3523
                  <xsl:otherwise>
3524
                     <xsl:value-of select="@mml:width"/>
3525
                  </xsl:otherwise>
3526
               </xsl:choose>
3527
            </xsl:variable>
3528
            <xsl:variable name="sLowerCaseHeight">
3529
               <xsl:choose>
3530
                  <xsl:when test="@height">
3531
                     <xsl:value-of select="@height"/>
3532
                  </xsl:when>
3533
                  <xsl:otherwise>
3534
                     <xsl:value-of select="@mml:height"/>
3535
                  </xsl:otherwise>
3536
               </xsl:choose>
3537
            </xsl:variable>
3538
            <xsl:variable name="sLowerCaseDepth">
3539
               <xsl:choose>
3540
                  <xsl:when test="@depth">
3541
                     <xsl:value-of select="@depth"/>
3542
                  </xsl:when>
3543
                  <xsl:otherwise>
3544
                     <xsl:value-of select="@mml:depth"/>
3545
                  </xsl:otherwise>
3546
               </xsl:choose>
3547
            </xsl:variable>
3548

    
3549
            <xsl:variable name="fFullWidth">
3550
               <xsl:call-template name="FFull">
3551
                  <xsl:with-param name="s" select="$sLowerCaseWidth"/>
3552
               </xsl:call-template>
3553
            </xsl:variable>
3554
            <xsl:variable name="fFullHeight">
3555
               <xsl:call-template name="FFull">
3556
                  <xsl:with-param name="s" select="$sLowerCaseHeight"/>
3557
               </xsl:call-template>
3558
            </xsl:variable>
3559
            <xsl:variable name="fFullDepth">
3560
               <xsl:call-template name="FFull">
3561
                  <xsl:with-param name="s" select="$sLowerCaseDepth"/>
3562
               </xsl:call-template>
3563
            </xsl:variable>
3564

    
3565
            <phant>
3566
               <xsl:call-template name="CreatePhantPropertiesCore">
3567
                  <xsl:with-param name="fShow" select="1"/>
3568
                  <xsl:with-param name="fFullWidth" select="$fFullWidth"/>
3569
                  <xsl:with-param name="fFullHeight" select="$fFullHeight"/>
3570
                  <xsl:with-param name="fFullDepth" select="$fFullDepth"/>
3571
               </xsl:call-template>
3572
               <e>
3573
                  <xsl:apply-templates mode="mml"/>
3574
               </e>
3575
            </phant>
3576
         </xsl:otherwise>
3577
      </xsl:choose>
3578
  </xsl:template>
3579

    
3580
  <xsl:template mode="mml" match="mml:mphantom">
3581
      <phant>
3582
         <xsl:call-template name="CreatePhantProperties">
3583
            <xsl:with-param name="ndCur" select="."/>
3584
            <xsl:with-param name="fShow" select="0"/>
3585
         </xsl:call-template>
3586
         <e>
3587
            <xsl:apply-templates mode="mml"/>
3588
         </e>
3589
      </phant>
3590
  </xsl:template>
3591

    
3592
  <xsl:template name="isNaryOper">
3593
      <xsl:param name="sNdCur"/>
3594
      <xsl:value-of select="($sNdCur = '∫' or $sNdCur = '∬' or $sNdCur = '∭' or $sNdCur = '∮' or $sNdCur = '∯' or $sNdCur = '∰' or $sNdCur = '∲' or $sNdCur = '∳' or $sNdCur = '∱' or $sNdCur = '∩' or $sNdCur = '∪' or $sNdCur = '∏' or $sNdCur = '∐' or $sNdCur = '∑')"/>
3595
  </xsl:template>
3596

    
3597

    
3598
  <xsl:template name="isNary">
3599
    <!-- ndCur is the element around the nAry operator -->
3600
    <xsl:param name="ndCur"/>
3601
      <xsl:variable name="sNdCur">
3602
         <xsl:value-of select="normalize-space($ndCur)"/>
3603
      </xsl:variable>
3604

    
3605
      <xsl:variable name="fNaryOper">
3606
         <xsl:call-template name="isNaryOper">
3607
            <xsl:with-param name="sNdCur" select="$sNdCur"/>
3608
         </xsl:call-template>
3609
      </xsl:variable>
3610

    
3611
      <!-- Narys shouldn't be MathML accents.  -->
3612
    <xsl:variable name="fUnder">
3613
         <xsl:choose>
3614
            <xsl:when test="$ndCur/parent::*[self::mml:munder]">1</xsl:when>
3615
            <xsl:otherwise>0</xsl:otherwise>
3616
         </xsl:choose>
3617
      </xsl:variable>
3618

    
3619
      <xsl:variable name="sLowerCaseAccent">
3620
         <xsl:choose>
3621
            <xsl:when test="$fUnder=1">
3622
               <xsl:choose>
3623
                  <xsl:when test="$ndCur/parent::*[self::mml:munder]/@accentunder">
3624
                     <xsl:value-of select="translate($ndCur/parent::*[self::mml:munder]/@accentunder, $StrUCAlphabet, $StrLCAlphabet)"/>
3625
                  </xsl:when>
3626
                  <xsl:otherwise>
3627
                     <xsl:value-of select="translate($ndCur/parent::*[self::mml:munder]/@mml:accentunder, $StrUCAlphabet, $StrLCAlphabet)"/>
3628
                  </xsl:otherwise>
3629
               </xsl:choose>
3630
            </xsl:when>
3631
            <xsl:otherwise>
3632
               <xsl:choose>
3633
                  <xsl:when test="$ndCur/parent::*/@accent">
3634
                     <xsl:value-of select="translate($ndCur/parent::*/@accent, $StrUCAlphabet, $StrLCAlphabet)"/>
3635
                  </xsl:when>
3636
                  <xsl:otherwise>
3637
                     <xsl:value-of select="translate($ndCur/parent::*/@mml:accent, $StrUCAlphabet, $StrLCAlphabet)"/>
3638
                  </xsl:otherwise>
3639
               </xsl:choose>
3640
            </xsl:otherwise>
3641
         </xsl:choose>
3642
      </xsl:variable>
3643

    
3644
      <xsl:variable name="fAccent">
3645
         <xsl:choose>
3646
            <xsl:when test="$sLowerCaseAccent='true'">1</xsl:when>
3647
            <xsl:otherwise>0</xsl:otherwise>
3648
         </xsl:choose>
3649
      </xsl:variable>
3650

    
3651
      <xsl:choose>
3652
      <!-- This ndCur is in fact part of an nAry if
3653
      
3654
           1)  The last descendant of ndCur (which could be ndCur itself) is an operator.
3655
           2)  Along that chain of descendants we only encounter mml:mo, mml:mstyle, and mml:mrow elements.
3656
           3)  the operator in mml:mo is a valid nAry operator
3657
           4)  The nAry is not accented.
3658
           -->
3659
      <xsl:when test="$fNaryOper = 'true'                       and $fAccent=0                       and $ndCur/descendant-or-self::*[last()]/self::mml:mo                       and not($ndCur/descendant-or-self::*[not(self::mml:mo or                                                          self::mml:mstyle or                                                          self::mml:mrow)])">
3660
            <xsl:value-of select="true()"/>
3661
         </xsl:when>
3662
         <xsl:otherwise>
3663
            <xsl:value-of select="false()"/>
3664
         </xsl:otherwise>
3665
      </xsl:choose>
3666
  </xsl:template>
3667

    
3668
  <xsl:template name="CreateNaryProp">
3669
      <xsl:param name="chr"/>
3670
      <xsl:param name="sMathmlType"/>
3671
      <naryPr>
3672
         <chr>
3673
            <xsl:attribute name="m:val">
3674
               <xsl:value-of select="$chr"/>
3675
            </xsl:attribute>
3676
         </chr>
3677
         <limLoc>
3678
            <xsl:attribute name="m:val">
3679
               <xsl:choose>
3680
                  <xsl:when test="$sMathmlType='munder' or           $sMathmlType='mover' or           $sMathmlType='munderover'">
3681
                     <xsl:text>undOvr</xsl:text>
3682
                  </xsl:when>
3683
                  <xsl:when test="$sMathmlType='msub' or                      $sMathmlType='msup' or                      $sMathmlType='msubsup'">
3684
                     <xsl:text>subSup</xsl:text>
3685
                  </xsl:when>
3686
               </xsl:choose>
3687
            </xsl:attribute>
3688
         </limLoc>
3689
         <grow>
3690
            <xsl:attribute name="m:val">
3691
               <xsl:value-of select="'on'"/>
3692
            </xsl:attribute>
3693
         </grow>
3694
         <subHide>
3695
            <xsl:attribute name="m:val">
3696
               <xsl:choose>
3697
                  <xsl:when test="$sMathmlType='mover' or                       $sMathmlType='msup'">
3698
                     <xsl:text>on</xsl:text>
3699
                  </xsl:when>
3700
                  <xsl:otherwise>
3701
                     <xsl:text>off</xsl:text>
3702
                  </xsl:otherwise>
3703
               </xsl:choose>
3704
            </xsl:attribute>
3705
         </subHide>
3706
         <supHide>
3707
            <xsl:attribute name="m:val">
3708
               <xsl:choose>
3709
                  <xsl:when test="$sMathmlType='munder' or                       $sMathmlType='msub'">
3710
                     <xsl:text>on</xsl:text>
3711
                  </xsl:when>
3712
                  <xsl:otherwise>
3713
                     <xsl:text>off</xsl:text>
3714
                  </xsl:otherwise>
3715
               </xsl:choose>
3716
            </xsl:attribute>
3717
         </supHide>
3718
      </naryPr>
3719
  </xsl:template>
3720

    
3721
  <xsl:template name="mathrRpHook"/>
3722
</xsl:stylesheet>