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

<xsl:stylesheet version='1.0'
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns="http://www.w3.org/1998/Math/MathML">

<xsl:template match="mml:fn">
</xsl:template>

<!--
<xsl:template match="mml:apply/lowlimit">
<xsl:variable name="first" select="../child::*[1]"/>
</xsl:template>

<xsl:template match="mml:apply/uplimit">
<xsl:variable name="first" select="../child::*[1]"/>
</xsl:template>

<xsl:template match="mml:apply/degree">
<xsl:variable name="first" select="../child::*[1]"/>
</xsl:template>
-->


<!-- conditions -->
<!-- no support for deprecated reln-->
<xsl:template match="mml:condition">
	<xsl:text>{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text>
</xsl:template>






<!--UNARY 
	  unary arithmetic			exp, factorial, minus, abs, conjugate, arg, real, imaginary
        unary logical				not
        unary functional			inverse, ident
        unary elementary classical
        functions					sin, cos, tan, sec, csc, cot, sinh, cosh, tanh, sech, csch, coth, arcsin,
                                          arccos, arctan, arccosh, arccot, arccoth, arccsc, arccsch, arcsec, arcsech, arcsinh,
                                          arctanh, exp, ln, log
        unary linear algebra			determinant, transpose
        unary calculus and vector calculus
                                          divergence, grad, curl, laplacian
        unary set-theoretic			card
-->

<!-- BINARY
	  binary arithmetic 			quotient, divide, minus, power, rem
        binary logical				implies, equivalent, approx
        binary set operators			setdiff
        binary linear algebra			vectorproduct, scalarproduct, outerproduct
-->

<!-- N-ARY and OTHER
 	 n-ary statistical mean, sdev, variance, median, mode
        n-ary logical and, or, xor
        n-ary linear algebra selector
        n-ary set operator union, intersect
        n-ary functional fn, compose
        integral, sum, product operators int, sum, product
        differential operator diff, partialdiff
        quantifier forall, exists
-->
<!-- Get the first child (operator), and check if its name is a fn , or operator-->
<!-- Is the operator taking qualifiers? -->
<!-- Operators  : int, sum, product, root, diff, partialdiff, limit, log, moment, min, max, forall, exists -->
<!-- Qualifiers : lowlimit, uplimit, bvar, degree, logbase, interval, condition  -->


<!-- apply/apply -->
<xsl:template match="mml:apply[mml:apply]">  <!-- when the function itself is defined by other functions: (F+G)(x) -->
	<xsl:choose>
		<xsl:when test="count(child::*)>=2">
			<xsl:text>{</xsl:text><xsl:apply-templates select="child::*[position()=1]"/><xsl:text>}</xsl:text>	
			<xsl:text>{</xsl:text><xsl:apply-templates select="child::*[position()!=1]"/><xsl:text>}</xsl:text>	
		</xsl:when>
	<xsl:otherwise> <!-- apply only contains apply, no operand-->
			<xsl:text>{</xsl:text><xsl:apply-templates select="child::*"/><xsl:text>}</xsl:text>	
	</xsl:otherwise>
	</xsl:choose>
</xsl:template>


<!-- force function or operator MathML 1.0 deprecated-->
<!-- partial support for func/operators defined using presentation markup-->
<xsl:template match="mml:apply[mml:fn]">
<mrow>
<xsl:choose>
<xsl:when test="*[position()=1 and self::mml:fn]/mml:mo/mml:mchar/@name='PlusMinus'">
  <!--if operator is infix (we assume this to be the default when we have mchars(for instance PlusMinus); perhaps we should test further the name attribute)-->
  <xsl:choose>
  <xsl:when test="count(child::*)>=3">
    <mrow>
      <xsl:for-each select="child::*[position()!=last() and  position()!=1]">
        <xsl:apply-templates select="."/><xsl:copy-of select="preceding-sibling::mml:fn/*"/>
      </xsl:for-each>
      <xsl:apply-templates select="child::*[position()!=1 and position()=last()]"/>
    </mrow>
  </xsl:when>
  <xsl:when test="count(child::*)=2">
    <mrow><xsl:copy-of select="child::mml:fn[position()=1]/*"/><xsl:apply-templates select="child::*[position()=2]"/></mrow>
  </xsl:when>
  <xsl:otherwise> <!-- apply only contains fn, no operand-->
    <mrow><xsl:apply-templates select="child::mml:fn/*"/></mrow>
  </xsl:otherwise>
  </xsl:choose>
</xsl:when>  
<xsl:otherwise>  <!-- if operator is prefix (common case)-->
  <xsl:choose>
    <xsl:when test="name(mml:fn/*[position()=1])='apply'"> <!-- fn definition is complex, surround with brackets, but only one child-->
      <mfenced separators=""><mrow><xsl:apply-templates select="mml:fn/*"/></mrow></mfenced>
    </xsl:when>
    <xsl:otherwise>
      <mi><xsl:apply-templates select="mml:fn/*"/></mi>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="count(*)>1">  <!-- if no operands, don't put empty parentheses-->
    <mo><mchar name="ApplyFunction"/></mo>
    <mfenced>
      <xsl:apply-templates select="*[position()!=1]"/>
    </mfenced>
  </xsl:if>
</xsl:otherwise>
</xsl:choose>
</mrow>
</xsl:template>




<!-- quotient -->
<xsl:template match="mml:apply[mml:quotient]">
  <mrow>  <!-- the third notation uses UNICODE chars x0230A and x0230B -->
    <mo>integer part of</mo>
    <mrow>
      <xsl:choose> <!-- surround with brackets if operands are composed-->
      <xsl:when test="child::*[position()=2] and name()='mml:apply'">
        <mfenced separators=""><xsl:apply-templates select="*[position()=2]"/></mfenced>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*[position()=2]"/>
      </xsl:otherwise>
      </xsl:choose>
      <mo>/</mo>
      <xsl:choose>
      <xsl:when test="child::*[position()=3] and name()='mml:apply'">
        <mfenced separators=""><xsl:apply-templates select="*[position()=3]"/></mfenced>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*[position()=3]"/>
      </xsl:otherwise>
      </xsl:choose>
    </mrow>
  </mrow>
</xsl:template>


<!-- factorial -->
<xsl:template match="mml:apply[mml:factorial]">
	<xsl:text>{</xsl:text>
	<xsl:choose> 
		<xsl:when test="name(*[position()=2])='mml:apply'">
			<xsl:text>{</xsl:text><xsl:apply-templates select="*[position()=2]"/><xsl:text>}</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="*[position()=2]"/>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>!(fact)</xsl:text>
	<xsl:text>}</xsl:text>
</xsl:template>


<!-- divide -->
<xsl:template match="mml:apply[mml:divide]">
	<xsl:text>{ \frac</xsl:text>
	<xsl:text>{ </xsl:text>
	<xsl:apply-templates select="child::*[position()=2]"/>
	<xsl:text>}</xsl:text>
	<xsl:text>{ </xsl:text>
	<xsl:apply-templates select="child::*[position()=3]"/>
	<xsl:text>}</xsl:text>
	<xsl:text>}</xsl:text>
</xsl:template>


<!-- APPLY CONTAINING MAX	-->
<xsl:template match="mml:apply/mml:max"/>
<xsl:template match="mml:apply[mml:max]">
	<xsl:text>{</xsl:text>
	<xsl:choose>
		<xsl:when test="mml:bvar"> <!-- if there are bvars-->
			<xsl:text>\max_{</xsl:text>
		<!-- Select every bvar except the last one (position() only counts bvars, not the other siblings)-->
			<xsl:for-each select="mml:bvar[position()!=last()]">  
            		<xsl:apply-templates select="."/><xsl:text>,</xsl:text>
			</xsl:for-each>
			<xsl:apply-templates select="mml:bvar[position()=last()]"/>
			<xsl:text>}</xsl:text>
		</xsl:when>
		<xsl:otherwise> <!-- No bvars, no underscore... -->
			<xsl:text>\max</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>\left\{</xsl:text>
     	<xsl:for-each select="child::*[name()!='mml:condition' and 	name()!='mml:bvar' and 	name()!='mml:max' and 	position()!=last()]">
     		<xsl:apply-templates select="."/><xsl:text>,</xsl:text>
		<xsl:message>RCAS: MathML mml:apply[mml:min] Applying templates to <xsl:copy-of select="name(.)"/></xsl:message>
	</xsl:for-each>
     	<xsl:apply-templates select="child::*[name()!='mml:condition' and	name()!='mml:bvar' and 	name()!='mml:max' and 	position()=last()]"/>
	<!-- If there is a condition, do not close... -->	
	<xsl:if test="mml:condition">
     		<xsl:text>|</xsl:text><xsl:apply-templates select="mml:condition"/>
	</xsl:if>
     	<xsl:text>\right\}</xsl:text>
	<xsl:text>}</xsl:text>
</xsl:template>


<!-- APPLY CONTAINING MIN	-->
<xsl:template match="mml:apply/mml:min"/>
<xsl:template match="mml:apply[mml:min]">
	<xsl:text>{</xsl:text>
	<xsl:choose>
		<xsl:when test="mml:bvar"> <!-- if there are bvars-->
			<xsl:text>\min_{</xsl:text>
			<!-- Select every bvar except the last one (position() only counts bvars, not the other siblings)-->
			<xsl:for-each select="mml:bvar[position()!=last()]">  
            		<xsl:apply-templates select="."/><xsl:text>,</xsl:text>
			</xsl:for-each>
			<xsl:apply-templates select="mml:bvar[position()=last()]"/>
			<xsl:text>}</xsl:text>
		</xsl:when>
		<xsl:otherwise> <!-- No bvars, no underscore... -->
			<xsl:text>\min</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>\left\{</xsl:text>
     	<xsl:for-each select="child::*[name()!='mml:condition' and 	name()!='mml:bvar' and 	name()!='mml:min' and 	position()!=last()]">
		<xsl:message>RCAS: MathML mml:apply[mml:min] Applying templates to <xsl:copy-of select="name(.)"/></xsl:message>
     		<xsl:apply-templates select="."/>
		<xsl:text>,</xsl:text>
	</xsl:for-each>
     	<xsl:apply-templates select="child::*[name()!='mml:condition' and	name()!='mml:bvar' and 	name()!='mml:min' and 	position()=last()]"/>
	<!-- If there is a condition, do not close... -->	
	<xsl:if test="mml:condition">
     		<xsl:text>|</xsl:text><xsl:apply-templates select="mml:condition"/>
	</xsl:if>
     	<xsl:text>\right\}</xsl:text>
	<xsl:text>}</xsl:text>
</xsl:template>








<!-- APPLY CONTAINING substraction(minus)	-->
<!-- unary or binary operator			-->
<xsl:template match="mml:apply[mml:minus]">
<xsl:text>{</xsl:text>
<xsl:choose> <!-- binary -->
	<xsl:when test="count(child::*)=3">
		<xsl:apply-templates select="child::*[position()=2]"/>
		<xsl:text>-</xsl:text>
		<xsl:choose>
	      	<xsl:when test="((name(*[position()=3])='mml:ci' or name(*[position()=3])='mml:cn') and contains(*[position()=3]/text(),'-')) or (name(*[position()=3])='mml:apply')">
			<xsl:text>\left(</xsl:text> <xsl:apply-templates select="*[position()=3]"/><xsl:text>\right)</xsl:text>
			<!-- surround negative or complex things with brackets -->
			</xsl:when>
	      	<xsl:otherwise>
      	  		<xsl:apply-templates select="*[position()=3]"/>
		      </xsl:otherwise>
		</xsl:choose>
	</xsl:when>
	<xsl:otherwise> <!-- unary -->
		<xsl:text>-</xsl:text>
		<xsl:choose>
    		<xsl:when test=
			"((name(*[position()=2])='mml:ci' or name(*[position()=2])='mml:cn') and contains(*[position()=2]/text(),'-')) or (name(*[position()=2])='mml:apply')">
			<xsl:text>\left(</xsl:text>
				<xsl:apply-templates select="child::*[position()=last()]"/>
			<xsl:text>\right)</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="child::*[position()=last()]"/>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:otherwise>
</xsl:choose>
<xsl:text>}</xsl:text>
</xsl:template>


<!-- addition -->
<xsl:template match="mml:apply[mml:plus]">
  <xsl:choose>
  <xsl:when test="count(child::*)>=3">
    <mrow>
      <xsl:choose>
        <xsl:when test="((name(*[position()=2])='mml:ci' or name(*[position()=2])='mml:cn') and contains(*[position()=2]/text(),'-')) or (*[position()=2 and self::mml:apply and child::mml:minus])">
          <mfenced separators=""><xsl:apply-templates select="*[position()=2]"/></mfenced> <!-- surround negative things with brackets -->
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="*[position()=2]"/>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:for-each select="child::*[position()!=1 and position()!=2]">
        <xsl:choose>
        <xsl:when test="((name(.)='mml:ci' or name(.)='mml:cn') and contains(./text(),'-')) or (self::mml:apply and child::mml:minus)"> <!-- surround negative things with brackets -->
          <mo>+</mo><mfenced separators=""><xsl:apply-templates select="."/></mfenced>
        </xsl:when>
        <xsl:otherwise>
          <mo>+</mo><xsl:apply-templates select="."/>
        </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </mrow>
  </xsl:when>
  <xsl:when test="count(child::*)=2">
    <mrow>
      <mo>+</mo><xsl:apply-templates select="child::*[position()=2]"/>
    </mrow>
  </xsl:when>
  <xsl:otherwise>
    <mo>+</mo>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- power -->
<xsl:template match="mml:apply[mml:power]">
	<xsl:text> {</xsl:text>
	<xsl:choose>
		<xsl:when test="name(*[position()=2])='mml:apply'">
			<xsl:text>\left(</xsl:text>
			<xsl:apply-templates select="child::*[position()=2]"/>
			<xsl:text>\rigth)</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="child::*[position()=2]"/>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>}^{</xsl:text>
		<xsl:apply-templates select="child::*[position()=3]"/>
	<xsl:text>} </xsl:text>
</xsl:template>

<!-- remainder -->
<xsl:template match="mml:apply[mml:rem]">
  <mrow>
    <xsl:choose> <!-- surround with brackets if operands are composed-->
    <xsl:when test="name(*[position()=2])='mml:apply'">
      <mfenced separators=""><xsl:apply-templates select="*[position()=2]"/></mfenced>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="*[position()=2]"/>
    </xsl:otherwise>
    </xsl:choose>
    <mo>mod</mo>
    <xsl:choose>
    <xsl:when test="name(*[position()=3])='mml:apply'">
      <mfenced separators=""><xsl:apply-templates select="*[position()=3]"/></mfenced>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="*[position()=3]"/>
    </xsl:otherwise>
    </xsl:choose>
  </mrow>
</xsl:template>

<!-- multiplication -->
<xsl:template match="mml:apply[mml:times]">
<xsl:choose>
<xsl:when test="count(child::*)>=3">
  <mrow>
    <xsl:for-each select="child::*[position()!=last() and  position()!=1]">
      <xsl:choose>
      <xsl:when test="mml:plus"> <!--add brackets around + children for priority purpose-->
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced><mo><mchar name="InvisibleTimes"/></mo>
      </xsl:when>
      <xsl:when test="mml:minus"> <!--add brackets around - children for priority purpose-->
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced><mo><mchar name="InvisibleTimes"/></mo>
      </xsl:when>
      <xsl:when test="(name(.)='mml:ci' or name(.)='mml:cn') and contains(text(),'-')"> <!-- have to do it using contains because starts-with doesn't seem to work well in XT-->
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced><mo><mchar name="InvisibleTimes"/></mo>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="."/><mo><mchar name="InvisibleTimes"/></mo>
      </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <xsl:for-each select="child::*[position()=last()]">
      <xsl:choose>
      <xsl:when test="mml:plus">
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced>
      </xsl:when>
      <xsl:when test="mml:minus">
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced>
      </xsl:when>
      <xsl:when test="(name(.)='mml:ci' or name(.)='mml:cn') and contains(text(),'-')"> <!-- have to do it using contains because starts-with doesn't seem to work well in  XT-->
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="."/>
      </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </mrow>
</xsl:when>
<xsl:when test="count(child::*)=2">  <!-- unary -->
  <mrow>
    <mo><mchar name="InvisibleTimes"/></mo>
    <xsl:choose>
      <xsl:when test="mml:plus">
        <mfenced separators=""><xsl:apply-templates select="*[position()=2]"/></mfenced>
      </xsl:when>
      <xsl:when test="mml:minus">
        <mfenced separators=""><xsl:apply-templates select="*[position()=2]"/></mfenced>
      </xsl:when>
      <xsl:when test="(*[position()=2 and self::mml:ci] or *[position()=2 and self::mml:cn]) and contains(*[position()=2]/text(),'-')">
        <mfenced separators=""><xsl:apply-templates select="*[position()=2]"/></mfenced>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*[position()=2]"/>
      </xsl:otherwise>
    </xsl:choose>
  </mrow>
</xsl:when>
<xsl:otherwise>  <!-- no operand -->
  <mo><mchar name="InvisibleTimes"/></mo>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- root -->
<xsl:template match="mml:apply[mml:root]">
  <xsl:choose>
  <xsl:when test="mml:degree">
    <xsl:choose>
    <xsl:when test="mml:degree/mml:cn/text()='2'"> <!--if degree=2 display a standard square root-->
      <msqrt>
        <xsl:apply-templates select="child::*[position()=3]"/>
      </msqrt>
    </xsl:when>
    <xsl:otherwise>
      <mroot>
        <xsl:apply-templates select="child::*[position()=3]"/>
        <mrow><xsl:apply-templates select="mml:degree/*"/></mrow>
      </mroot>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:when>
  <xsl:otherwise> <!-- no degree specified-->
    <msqrt>
      <xsl:apply-templates select="child::*[position()=2]"/>
    </msqrt>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- greatest common divisor -->
<xsl:template match="mml:apply[mml:gcd]">
  <mrow>
    <mi>gcd</mi><mo><mchar name="ApplyFunction"/></mo>
    <mfenced>
      <xsl:apply-templates select="child::*[position()!=1]"/>
    </mfenced>
  </mrow>
</xsl:template>

<!-- AND -->
<xsl:template match="mml:apply[mml:and]">
<mrow>
  <xsl:choose>
  <xsl:when test="count(*)>=3"> <!-- at least two operands (common case)-->
    <xsl:for-each select="child::*[position()!=last() and  position()!=1]">
      <xsl:choose>
      <xsl:when test="mml:or"> <!--add brackets around OR children for priority purpose-->
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced><mo><mchar name="And"/></mo>
      </xsl:when>
      <xsl:when test="mml:xor"> <!--add brackets around XOR children for priority purpose-->
       <mfenced separators=""><xsl:apply-templates select="."/></mfenced><mo><mchar name="And"/></mo> 
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="."/><mo><mchar name="And"/></mo>
      </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <xsl:for-each select="child::*[position()=last()]">
      <xsl:choose>
      <xsl:when test="mml:or">
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced>
      </xsl:when>
      <xsl:when test="mml:xor">
        <mfenced separators=""><xsl:apply-templates select="."/></mfenced>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="."/>
      </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:when>
  <xsl:when test="count(*)=2">
    <mo><mchar name="And"/></mo><xsl:apply-templates select="*[position()=last()]"/>
  </xsl:when>
  <xsl:otherwise>
    <mo><mchar name="And"/></mo>
  </xsl:otherwise>
  </xsl:choose>
</mrow>
</xsl:template>

<!-- OR -->
<xsl:template match="mml:apply[mml:or]">
<mrow>
  <xsl:choose>
  <xsl:when test="count(*)>=3">
    <xsl:for-each select="child::*[position()!=last() and  position()!=1]">
      <xsl:apply-templates select="."/><mo><mchar name="Or"/></mo>
    </xsl:for-each>
    <xsl:apply-templates select="child::*[position()=last()]"/>
    </xsl:when>
    <xsl:when test="count(*)=2">
      <mo><mchar name="Or"/></mo><xsl:apply-templates select="*[position()=last()]"/>
  </xsl:when>
  <xsl:otherwise>
    <mo><mchar name="Or"/></mo>
  </xsl:otherwise>
  </xsl:choose>
</mrow>
</xsl:template>

<!-- XOR -->
<xsl:template match="mml:apply[mml:xor]">
<mrow>
  <xsl:choose>
  <xsl:when test="count(*)>=3">
    <xsl:for-each select="child::*[position()!=last() and  position()!=1]">
      <xsl:apply-templates select="."/><mo>xor</mo>
    </xsl:for-each>
    <xsl:apply-templates select="child::*[position()=last()]"/>
    </xsl:when>
    <xsl:when test="count(*)=2">
      <mo>xor</mo><xsl:apply-templates select="*[position()=last()]"/>
  </xsl:when>
  <xsl:otherwise>
    <mo>xor</mo>
  </xsl:otherwise>
  </xsl:choose>
</mrow>
</xsl:template>

<!-- NOT -->
<xsl:template match="mml:apply[mml:not]">
  <mrow>
    <mo><mchar name="Not"/></mo>
    <xsl:choose>
    <xsl:when test="child::mml:apply"><!--add brackets around OR,AND,XOR children for priority purpose-->
      <mfenced separators="">
        <xsl:apply-templates select="child::*[position()=2]"/>
      </mfenced>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="child::*[position()=2]"/>
    </xsl:otherwise>
    </xsl:choose>
  </mrow>
</xsl:template>

<!-- implies -->
<xsl:template match="mml:apply[mml:implies]">
  <mrow>
    <xsl:apply-templates select="child::*[position()=2]"/>
    <mo><mchar name="DoubleRightArrow"/></mo>
    <xsl:apply-templates select="child::*[position()=3]"/>
  </mrow>
</xsl:template>

<xsl:template match="mml:reln[mml:implies]">
  <mrow>
    <xsl:apply-templates select="child::*[position()=2]"/>
    <mo><mchar name="DoubleRightArrow"/></mo>
    <xsl:apply-templates select="child::*[position()=3]"/>
  </mrow>
</xsl:template>

<!-- for all-->
<xsl:template match="mml:apply[mml:forall]">
  <mrow>
    <mo><mchar name="ForAll"/></mo>
    <mrow>
      <xsl:for-each select="mml:bvar[position()!=last()]">
        <xsl:apply-templates select="."/><mo>,</mo>
      </xsl:for-each>
      <xsl:apply-templates select="mml:bvar[position()=last()]"/>
    </mrow>
    <xsl:if test="mml:condition">
      <mrow><mo>,</mo><xsl:apply-templates select="mml:condition"/></mrow>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="mml:apply">
        <mo>:</mo><xsl:apply-templates select="mml:apply"/>
      </xsl:when>
      <xsl:when test="mml:reln">
        <mo>:</mo><xsl:apply-templates select="mml:reln"/>
      </xsl:when>
    </xsl:choose>
  </mrow>
</xsl:template>

<!-- in -->
<xsl:template match="mml:apply[mml:in]">
	<xsl:text>{</xsl:text>
    	<xsl:apply-templates select="child::*[position()=2]"/>
	<xsl:text>\in</xsl:text>
    	<xsl:apply-templates select="child::*[position()=3]"/>
	<xsl:text>}</xsl:text>
</xsl:template>

<!-- notin -->
<xsl:template match="mml:apply[mml:notin]">
	<xsl:text>{</xsl:text>
    	<xsl:apply-templates select="child::*[position()=2]"/>
	<xsl:text>\notin</xsl:text>
    	<xsl:apply-templates select="child::*[position()=3]"/>
	<xsl:text>}</xsl:text>
</xsl:template>

<!-- exist-->
<xsl:template match="mml:apply[mml:exists]">
  <mrow>
    <mo><mchar name="Exists"/></mo>
    <mrow>
      <xsl:for-each select="mml:bvar[position()!=last()]">
        <xsl:apply-templates select="."/><mo>,</mo>
      </xsl:for-each>
      <xsl:apply-templates select="mml:bvar[position()=last()]"/>
    </mrow>
    <xsl:if test="mml:condition">
      <mrow><mo>,</mo><xsl:apply-templates select="mml:condition"/></mrow>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="mml:apply">
        <mo>:</mo><xsl:apply-templates select="mml:apply"/>
      </xsl:when>
      <xsl:when test="mml:reln">
        <mo>:</mo><xsl:apply-templates select="mml:reln"/>
      </xsl:when>
    </xsl:choose>
  </mrow>
</xsl:template>

<!-- absolute value -->
<xsl:template match="mml:apply[mml:abs]">
  <mrow><mo>|</mo><xsl:apply-templates select="child::*[position()=last()]"/><mo>|</mo></mrow>
</xsl:template>

<!-- conjugate -->
<xsl:template match="mml:apply[mml:conjugate]">
  <mover>
    <xsl:apply-templates select="child::*[position()=2]"/>
    <mo><mchar name="ovbar"/></mo>  <!-- does not work, UNICODE x0233D  or perhaps OverBar-->
  </mover>
</xsl:template>

<!-- argument of complex number -->
<xsl:template match="mml:apply[mml:arg]">
  <mrow>
    <mi>arg</mi><mo><mchar name="ApplyFunction"/></mo><mfenced separators=""><xsl:apply-templates select="child::*[position()=2]"/></mfenced>
  </mrow>
</xsl:template>

<!-- real part of complex number -->
<xsl:template match="mml:apply[mml:real]">
  <mrow>
    <mi><xsl:text disable-output-escaping='yes'>&amp;#x0211C;</xsl:text><!-- mchar Re or realpart should work--></mi>
    <mo><mchar name="ApplyFunction"/></mo>
    <mfenced separators=""><xsl:apply-templates select="child::*[position()=2]"/></mfenced>
  </mrow>
</xsl:template>

<!-- imaginary part of complex number -->
<xsl:template match="mml:apply[mml:imaginary]">
  <mrow>
    <mi><xsl:text disable-output-escaping='yes'>&amp;#x02111;</xsl:text><!-- mchar Im or impart should work--></mi>
    <mo><mchar name="ApplyFunction"/></mo>
    <mfenced separators=""><xsl:apply-templates select="child::*[position()=2]"/></mfenced>
  </mrow>
</xsl:template>

<!-- lowest common multiple -->
<xsl:template match="mml:apply[mml:lcm]">
  <mrow>
    <mi>lcm</mi><mo><mchar name="ApplyFunction"/></mo>
    <mfenced>
      <xsl:apply-templates select="child::*[position()!=1]"/>
    </mfenced>
  </mrow>
</xsl:template>


</xsl:stylesheet>