This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
openrosa2html5form.xsl
1706 lines (1643 loc) · 85.4 KB
/
openrosa2html5form.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!--
*****************************************************************************************************
XSLT Stylesheet that transforms OpenRosa style (X)Forms into valid HTMl5 forms
(exception: when non-IANA lang attributes are used the form will not validate (but that's not serious))
*****************************************************************************************************
-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:orx="http://openrosa.org/xforms"
xmlns:enk="http://enketo.org/xforms"
xmlns:odk="http://www.opendatakit.org/xforms"
xmlns:kb="http://kobotoolbox.org/xforms"
xmlns:esri="http://esri.com/xforms"
xmlns:oc="http://openclinica.org/xforms"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jr="http://openrosa.org/javarosa"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
xmlns:dyn="http://exslt.org/dynamic"
extension-element-prefixes="exsl str dyn"
version="1.0"
>
<xsl:param name="include-relevant-msg"/>
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/><!-- for xml: version="1.0" -->
<xsl:variable name="upper-case" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name="lower-case" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="undefined">undefined</xsl:variable>
<xsl:variable name="warning">warning</xsl:variable>
<xsl:variable name="error">error</xsl:variable>
<xsl:variable name="translated"><!-- assumes that either a whole form is translated or nothing (= real life) -->
<xsl:if test="count(/h:html/h:head/xf:model/xf:itext/xf:translation) > 1" >
<xsl:value-of select="string('true')" /><!-- no time to figure out how to use real boolean values -->
</xsl:if>
</xsl:variable>
<xsl:variable name="default-lang">
<xsl:choose>
<xsl:when test="h:html/h:head/xf:model/xf:itext/xf:translation[@default]/@lang">
<xsl:value-of select="h:html/h:head/xf:model/xf:itext/xf:translation[@default]/@lang" />
</xsl:when>
<xsl:otherwise>
<!-- first language or empty if itext was not used -->
<xsl:value-of select="h:html/h:head/xf:model/xf:itext/xf:translation[1]/@lang" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:template match="/">
<xsl:if test="not(function-available('exsl:node-set'))">
<xsl:message terminate="yes">FATAL ERROR: exsl:node-set function is not available in this XSLT processor</xsl:message>
</xsl:if>
<xsl:if test="not(function-available('str:replace'))">
<xsl:message terminate="yes">FATAL ERROR: str:replace function is not available in this XSLT processor</xsl:message>
</xsl:if>
<xsl:if test="not(function-available('dyn:evaluate'))">
<xsl:message terminate="yes">FATAL ERROR: dyn:evaluate function is not available in this XSLT processor</xsl:message>
</xsl:if>
<xsl:if test="not(function-available('str:tokenize'))">
<xsl:message terminate="yes">FATAL ERROR: str:tokenize function is not available in this XSLT processor</xsl:message>
</xsl:if>
<xsl:for-each select="/h:html/h:head/xf:model/xf:bind">
<xsl:if test="not(substring(./@nodeset, 1, 1) = '/')">
<xsl:message terminate="no">WARNING: Found binding(s) with relative nodeset attribute <!--on element: <xsl:value-of select="./@nodeset" />--> (form may work correctly if relative nodesets were used consistently throughout xml form in bindings as well as body, otherwise it will certainly be messed up). </xsl:message>
</xsl:if>
</xsl:for-each>
<!--> <xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
<html>
<head>
<title>
<xsl:text>Transformation of JR (X)Form to HTML5</xsl:text>
</title>
<script src="jquery.min.js" type="text/javascript" ><xsl:text> </xsl:text></script>
<script type="text/javascript">
<xsl:text disable-output-escaping='yes'>
$(function() {
$('#form-languages a').click(function(){
$('form [lang]').show().not('[lang="'+$(this).attr('lang')+'"], [lang=""], #form-languages a').hide();
});
});</xsl:text>
</script>
</head>-->
<root>
<form autocomplete="off" novalidate="novalidate" class="clearfix" dir="ltr">
<xsl:attribute name="class">
<xsl:value-of select="'or clearfix'" />
<xsl:if test="/h:html/h:body/@class">
<xsl:value-of select="concat(' ', /h:html/h:body/@class)" />
</xsl:if>
</xsl:attribute>
<xsl:attribute name="id">
<xsl:choose>
<xsl:when test="/h:html/h:head/xf:model/xf:instance[1]/child::node()/@id">
<xsl:value-of select="translate(/h:html/h:head/xf:model/xf:instance/child::node()/@id, ' ', '_' )" /><!-- not smart! -->
</xsl:when>
<xsl:when test="/h:html/h:head/xf:model/xf:instance/child::node()/@xmlns">
<xsl:value-of select="translate(/h:html/h:head/xf:model/xf:instance/child::node()/@xmlns, ' ', '_')" />
</xsl:when>
<xsl:otherwise>
<xsl:text>_</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="/h:html/h:head/xf:model/xf:submission/@action">
<xsl:attribute name="action">
<xsl:value-of select="/h:html/h:head/xf:model/xf:submission/@action"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="/h:html/h:head/xf:model/xf:submission/@method">
<xsl:attribute name="method">
<xsl:choose>
<xsl:when test="/h:html/h:head/xf:model/xf:submission/@method = 'form-data-post'">
<xsl:value-of select="'post'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="/h:html/h:head/xf:model/xf:submission/@method"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:if>
<xsl:if test="/h:html/h:head/xf:model/xf:submission/@base64RsaPublicKey">
<xsl:attribute name="data-base64RsaPublicKey">
<xsl:value-of select="/h:html/h:head/xf:model/xf:submission/@base64RsaPublicKey"/>
</xsl:attribute>
</xsl:if>
<xsl:text> </xsl:text>
<xsl:comment>This form was created by transforming a OpenRosa-flavored (X)Form using an XSL stylesheet created by Enketo LLC.</xsl:comment>
<section class="form-logo">
<xsl:text> </xsl:text>
</section>
<h3 dir="auto" id="form-title">
<xsl:choose>
<xsl:when test="/h:html/h:head/h:title">
<xsl:value-of select="/h:html/h:head/h:title"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>No Title</xsl:text>
</xsl:otherwise>
</xsl:choose>
</h3>
<!--
<div id="stats" style="display: none;">
<span id="jrSelect"><xsl:value-of select="count(/h:html/h:body//xf:select)"/></span>
<span id="jrSelect1"><xsl:value-of select="count(/h:html/h:body//xf:select1)"/></span>
<span id="jrItemset"><xsl:value-of select="count(/h:html/h:body//xf:itemset)"/></span>
<span id="jrItem"><xsl:value-of select="count(/h:html/h:body//xf:item)"/></span>
<span id="jrInput"><xsl:value-of select="count(/h:html/h:body//xf:input)"/></span>
<span id="jrUpload"><xsl:value-of select="count(/h:html/h:body//xf:upload)"/></span>
<span id="jrTrigger"><xsl:value-of select="count(/h:html/h:body//xf:trigger)"/></span>
<span id="jrRepeat"><xsl:value-of select="count(/h:html/h:body//xf:repeat)"/></span>
<span id="jrRelevant"><xsl:value-of select="count(/h:html/h:head/xf:model/xf:bind[@relevant])"/></span>
<span id="jrConstraint"><xsl:value-of select="count(/h:html/h:head/xf:model/xf:bind[@constraint])"/></span>
<span id="jrCalculate"><xsl:value-of select="count(/h:html/h:head/xf:model/xf:bind[@calculate])"/></span>
<span id="jrPreload"><xsl:value-of select="count(/h:html/h:head/xf:model/xf:bind[@jr:preload])"/></span>
</div>
-->
<xsl:if test="//*/@lang" >
<select id="form-languages">
<xsl:if test="$translated != 'true'">
<xsl:attribute name="style">display:none;</xsl:attribute>
</xsl:if>
<xsl:attribute name="data-default-lang">
<xsl:value-of select="$default-lang" />
</xsl:attribute>
<xsl:call-template name="languages" />
</select>
</xsl:if>
<xsl:apply-templates />
<!-- Create hidden input fields for preload items that do not have a form control. -->
<xsl:if test="/h:html/h:head/xf:model/xf:bind[@jr:preload]" >
<fieldset id="or-preload-items" style="display:none;">
<xsl:apply-templates select="/h:html/h:head/xf:model/xf:bind[@jr:preload]"/>
</fieldset>
</xsl:if>
<!-- Create hidden input fields for calculated items that do not have a form control. -->
<!-- the template will exclude those that have an input field -->
<xsl:if test="/h:html/h:head/xf:model/xf:bind[@calculate]">
<fieldset id="or-calculated-items" style="display:none;">
<xsl:apply-templates select="/h:html/h:head/xf:model/xf:bind[@calculate]" />
</fieldset>
</xsl:if>
<!--
<xsl:if test="/h:html/h:body//xf:output">
<xsl:message>WARNING: Output element(s) added but note that only /absolute/path/to/node is properly supported as "value" attribute of outputs. Please test to make sure they do what you want.</xsl:message>
</xsl:if>
<xsl:if test="/h:html/h:body//xf:itemset">
<xsl:message>WARNING: Itemset support is experimental. Make sure to test whether they do what you want.</xsl:message>
</xsl:if>
-->
<xsl:if test="//xf:submission">
<xsl:message>ERROR: Submissions element(s) not supported.</xsl:message>
</xsl:if>
</form>
</root>
</xsl:template>
<xsl:template match="h:head"/> <!--[not(self::xf:model/xf:bind[@jr:preload])]" />-->
<xsl:template match="xf:group">
<!-- NOTE: TO IMPROVE PERFORMANCE, SUPPORT FOR RELATIVE NODESET BINDINGS HAS BEEN SWITCHED OFF
To turn this back on:
- uncomment the variable nodeset_used
- revert back to commented-out code for variable nodeset
- revert back to commented-out code for variable binding
- all this takes place in the next 10 lines
<xsl:variable name="nodeset_used">
<xsl:call-template name="nodeset_used" />
</xsl:variable>
-->
<xsl:variable name="nodeset">
<!--<xsl:call-template name="nodeset_absolute">
<xsl:with-param name="nodeset_u" select="$nodeset_used"/>
</xsl:call-template>-->
<xsl:call-template name="nodeset_used" />
</xsl:variable>
<!-- note that bindings are not required -->
<!--<xsl:variable name="binding" select="/h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset_used] | /h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset]"/>-->
<xsl:variable name="binding" select="/h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset]"/>
<section>
<xsl:attribute name="class">
<!-- only add or-group if label is present or if it has a repeat as child-->
<xsl:choose>
<xsl:when test="string(./xf:label/@ref) or string(./xf:label)">
<xsl:value-of select="'or-group '" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'or-group-data '" />
</xsl:otherwise>
</xsl:choose>
<xsl:if test="$binding/@relevant">
<xsl:value-of select="'or-branch pre-init '"/>
</xsl:if>
<xsl:call-template name="appearance" />
<!-- Workaround for XLSForm limitation: add "compact" to group if the immediate repeat child has this appearance -->
<!-- This should actually be fixed in pyxform instead -->
<xsl:if test="contains(./xf:repeat/@appearance, 'compact')">
<xsl:value-of select="'or-appearance-compact '"/>
</xsl:if>
<!-- same workaround for "no-collapse" -->
<xsl:if test="contains(./xf:repeat/@appearance, 'no-collapse')">
<xsl:value-of select="'or-appearance-no-collapse '"/>
</xsl:if>
</xsl:attribute>
<xsl:if test="string($nodeset)">
<!--<xsl:variable name="nodeset" select="@ref" />-->
<xsl:attribute name="name">
<xsl:value-of select="$nodeset"/>
</xsl:attribute>
<xsl:if test="$binding/@relevant">
<xsl:attribute name="data-relevant">
<!--<xsl:value-of select="/h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset]/@relevant" />-->
<xsl:value-of select="$binding/@relevant"/>
</xsl:attribute>
</xsl:if>
</xsl:if>
<xsl:if test="string(./xf:label/@ref) or string (./xf:label)">
<h4>
<xsl:apply-templates select="xf:label" />
</h4>
</xsl:if>
<xsl:apply-templates select="*[not(self::xf:label or self::xf:hint)]"/>
<xsl:call-template name="constraint-and-required-msg" >
<xsl:with-param name="binding" select="$binding"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</section><xsl:comment>end of group <xsl:value-of select="@nodeset" /> </xsl:comment>
</xsl:template>
<xsl:template match="xf:repeat">
<!-- NOTE: TO IMPROVE PERFORMANCE, SUPPORT FOR RELATIVE NODESET BINDINGS HAS BEEN SWITCHED OFF
To turn this back on:
- uncomment the variable nodeset_used
- revert back to commented-out code for variable nodeset
- revert back to commented-out code for variable binding
- all this takes place in the next 10 lines
<xsl:variable name="nodeset_used">
<xsl:call-template name="nodeset_used" />
</xsl:variable>
-->
<!-- the correct absolute nodeset as used in HTML -->
<xsl:variable name="nodeset">
<!--<xsl:call-template name="nodeset_absolute">
<xsl:with-param name="nodeset_u" select="$nodeset_used"/>
</xsl:call-template>-->
<xsl:call-template name="nodeset_used" />
</xsl:variable>
<!-- note that bindings are not required -->
<!--<xsl:variable name="binding" select="/h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset_used] | /h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset]" />-->
<xsl:variable name="binding" select="/h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset]" />
<section>
<xsl:attribute name="class">
<xsl:value-of select="'or-repeat '" />
<!-- watch out or-branch pre-init added to or-group parent! -->
<!--<xsl:if test="$binding/@relevant">
<xsl:value-of select="'or-branch pre-init '"/>
</xsl:if>-->
<xsl:call-template name="appearance" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="$nodeset"/>
</xsl:attribute>
<xsl:if test="string(./xf:label/@ref) or string (./xf:label)">
<h4>
<xsl:apply-templates select="xf:label" />
</h4>
</xsl:if>
<xsl:apply-templates select="*[not(self::xf:label or self::xf:hint)]"/>
<xsl:text>
</xsl:text>
</section>
<!-- Add a repeat-info node -->
<div>
<xsl:attribute name="class">
<xsl:value-of select="'or-repeat-info'" />
</xsl:attribute>
<xsl:attribute name="data-name">
<xsl:value-of select="$nodeset"/>
</xsl:attribute>
<xsl:if test="@jr:count">
<xsl:attribute name="data-repeat-count">
<xsl:value-of select="@jr:count" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@jr:noAddRemove">
<xsl:attribute name="data-repeat-fixed">
<xsl:value-of select="string('fixed')"/>
</xsl:attribute>
</xsl:if>
</div>
</xsl:template>
<xsl:template name="appearance">
<xsl:variable name="select-type">
<xsl:if test="local-name() = 'select' or local-name() = 'select1'">
<xsl:value-of select="'true'"/>
</xsl:if>
</xsl:variable>
<xsl:if test="@appearance">
<xsl:variable name="appearances" select="str:tokenize(@appearance)" />
<xsl:for-each select="exsl:node-set($appearances)">
<xsl:variable name="appearance">
<xsl:value-of select="normalize-space(translate(., $upper-case, $lower-case))"/>
</xsl:variable>
<xsl:value-of select="concat('or-appearance-', $appearance, ' ')"/>
<!-- convert deprecated appearances, but leave the deprecated ones -->
<xsl:if test="$select-type = 'true'">
<xsl:if test="$appearance = 'horizontal'">
<xsl:value-of select="'or-appearance-columns '" />
</xsl:if>
<xsl:if test="$appearance = 'horizontal-compact'">
<xsl:value-of select="'or-appearance-columns-pack '" />
</xsl:if>
<xsl:if test="$appearance = 'compact'">
<xsl:value-of select="'or-appearance-columns-pack or-appearance-no-buttons '" />
</xsl:if>
<xsl:if test="starts-with($appearance, 'compact-')">
<xsl:value-of select="concat('or-appearance-columns-', substring-after($appearance, '-'), ' or-appearance-no-buttons ')" />
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:if>
<!-- turn rows attribute into an appearance (which is what it should have been in the first place imho)-->
<xsl:if test="./@rows">
<xsl:value-of select="concat('or-appearance-rows-', ./@rows, ' ')" />
</xsl:if>
</xsl:template>
<xsl:template match="xf:input | xf:upload | xf:range | xf:item | xf:bind[@jr:preload] | xf:bind[@calculate]">
<!-- NOTE: TO IMPROVE PERFORMANCE, SUPPORT FOR RELATIVE NODESET BINDINGS HAS BEEN SWITCHED OFF
To turn this back on:
- uncomment the variable nodeset_used
- revert back to commented-out code for variable nodeset
- revert back to commented-out code for variable binding
- all this takes place in the next 10 lines
<xsl:variable name="nodeset_used">
<xsl:call-template name="nodeset_used" />
</xsl:variable>
-->
<!-- the correct absolute nodeset as used in HTML -->
<xsl:variable name="nodeset">
<!--<xsl:call-template name="nodeset_absolute">
<xsl:with-param name="nodeset_u" select="$nodeset_used"/>
</xsl:call-template>-->
<xsl:call-template name="nodeset_used" />
</xsl:variable>
<!-- note that bindings are not required -->
<!--<xsl:variable name="binding" select="/h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset_used] | /h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset]" />-->
<xsl:variable name="binding" select="/h:html/h:head/xf:model/xf:bind[@nodeset=$nodeset]" />
<!-- If this is a bind element that also has an input, do nothing as it will be dealt with by the corresponding xf:input -->
<!-- Note that this test is not fully spec-compliant. It will work with XLS-form produced forms that have no relative nodes
and use the ref atribute only -->
<xsl:if test="not( local-name() = 'bind' and (
/h:html/h:body//xf:input[@ref=$nodeset] or
/h:html/h:body//xf:upload[@ref=$nodeset] or
/h:html/h:body//xf:select[@ref=$nodeset] or
/h:html/h:body//xf:select1[@ref=$nodeset] ) )">
<xsl:choose>
<xsl:when test="(local-name() = 'range' and contains(@appearance, 'picker'))">
<xsl:call-template name="select-select">
<xsl:with-param name="nodeset" select="$nodeset" />
<xsl:with-param name="binding" select="$binding" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<label>
<xsl:attribute name="class">
<xsl:if test="local-name() = 'input' or local-name() = 'upload' or local-name() = 'range'">
<xsl:value-of select="'question '"/>
</xsl:if>
<xsl:if test="(local-name() = 'input' or local-name() = 'upload' or local-name() = 'range' or local-name() = 'bind') and $binding/@relevant">
<xsl:value-of select="'or-branch pre-init '"/>
</xsl:if>
<xsl:if test="local-name() = 'bind'">
<xsl:value-of select="'calculation '"/>
</xsl:if>
<!--<xsl:if test="local-name() = 'item'">
<xsl:value-of select="'clearfix '"/>
</xsl:if>-->
<xsl:if test="local-name() != 'item'">
<xsl:value-of select="'non-select '"/>
</xsl:if>
<xsl:call-template name="appearance" />
</xsl:attribute>
<xsl:apply-templates select="./@kb:image-customization"/>
<xsl:if test="not(local-name() = 'item' or local-name() = 'bind')">
<xsl:apply-templates select="xf:label" />
<xsl:if test="not($binding/@readonly = 'true()')">
<xsl:apply-templates select="$binding/@required"/>
</xsl:if>
</xsl:if>
<!--
note: Hints should actually be placed in title attribute (of input) as it is semantically nicer.
However, to support multiple languages and parse all of them (to be available offline)
they are placed in the label instead.
-->
<xsl:apply-templates select="xf:hint" />
<xsl:variable name="appearance">
<xsl:value-of select="translate(@appearance, $upper-case, $lower-case)"/>
</xsl:variable>
<xsl:variable name="element">
<xsl:choose>
<xsl:when test="$binding/@type = 'string' and contains($appearance, 'multi-line') or contains($appearance, 'multiline') or contains($appearance, 'text-area') or contains($appearance, 'textarea') or ./@rows">
<xsl:value-of select="string('textarea')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="string('input')" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="type">
<xsl:if test="$element = 'textarea'">
<xsl:value-of select="$element"/>
</xsl:if>
</xsl:variable>
<xsl:element name="{$element}">
<xsl:choose>
<xsl:when test="ancestor::odk:rank">
<xsl:call-template name="rank-item-attributes"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="binding-attributes">
<xsl:with-param name="binding" select="$binding"/>
<xsl:with-param name="nodeset" select="$nodeset"/>
<xsl:with-param name="type" select="$type"/>
</xsl:call-template>
<xsl:if test="$element='textarea'">
<xsl:text><!-- avoid self-closing textarea --> </xsl:text>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:if test="local-name() = 'item'">
<xsl:apply-templates select="xf:label" />
</xsl:if>
<xsl:if test="not(local-name() = 'item' or local-name() = 'bind')">
<xsl:call-template name="constraint-and-required-msg" >
<xsl:with-param name="binding" select="$binding"/>
</xsl:call-template>
</xsl:if>
</label>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="range-option-picker">
<xsl:param name="start" />
<xsl:param name="end" />
<xsl:param name="step" />
<xsl:param name="readonly" />
<xsl:param name="limit" />
<xsl:if test="not($start > $end)">
<option>
<xsl:if test="$readonly">
<xsl:attribute name="disabled">
<xsl:value-of select="'disabled'"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="string($start)">
<xsl:value-of select="$start" />
</xsl:when>
<xsl:otherwise>
<xsl:message>ERROR: Could not determine value of select option.</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="$start" />
</option>
<span>
<xsl:attribute name="data-option-value">
<xsl:value-of select="$start" />
</xsl:attribute>
<xsl:value-of select="."/>
</span>
<xsl:variable name="next" select="$start + $step"/>
<xsl:choose>
<xsl:when test="$limit > 1">
<xsl:apply-templates select="current()" mode="range-option-picker">
<xsl:with-param name="start" select="$next"/>
<xsl:with-param name="end" select="$end"/>
<xsl:with-param name="step" select="$step"/>
<xsl:with-param name="readonly" select="$readonly"/>
<xsl:with-param name="limit" select="$limit - 1"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:message>ERROR: Exceed maximum iterations allowed.</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template match="xf:item" mode="select-option">
<xsl:param name="tolerate-spaces" />
<xsl:param name="readonly" />
<xsl:variable name="label_translations">
<xsl:apply-templates select="xf:label" />
</xsl:variable>
<xsl:variable name="value">
<xsl:value-of select="xf:value" />
<xsl:if test="not($tolerate-spaces) and contains(xf:value, ' ')">
<xsl:message terminate="yes">ERROR: (Multi-)select item found with a value that contains spaces!</xsl:message>
</xsl:if>
<xsl:if test="not(string(xf:value))">
<xsl:message terminate="no">WARNING: Select item found without a value!</xsl:message>
</xsl:if>
</xsl:variable>
<option>
<xsl:if test="$readonly">
<xsl:attribute name="disabled">
<xsl:value-of select="'disabled'"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="string($value)">
<xsl:value-of select="$value" />
</xsl:when>
<xsl:otherwise>
<xsl:message>ERROR: Could not determine value of select option.</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<!-- better to use default language if defined and otherwise span[1] -->
<xsl:choose>
<!-- TODO: IT WOULD BE MORE EFFICIENT TO EXTRACT THIS FROM exsl:node-set($label_translations) -->
<xsl:when test="exsl:node-set($label_translations)/span[@lang=$default-lang]">
<xsl:value-of select="exsl:node-set($label_translations)/span[@lang=$default-lang] " />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="exsl:node-set($label_translations)/span[1] " />
</xsl:otherwise>
</xsl:choose>
</option>
<xsl:for-each select="exsl:node-set($label_translations)/span" >
<span>
<xsl:attribute name="data-option-value">
<xsl:value-of select="$value" />
</xsl:attribute>
<xsl:attribute name="lang">
<xsl:value-of select="@lang" />
</xsl:attribute>
<xsl:value-of select="."/>
</span>
</xsl:for-each>
</xsl:template>
<xsl:template match="xf:itemset" mode="templates">
<xsl:param name="nodeset" />
<xsl:param name="binding"/>
<xsl:choose>
<xsl:when test="$binding">
<label class="itemset-template">
<xsl:attribute name="data-items-path">
<xsl:value-of select="@nodeset"/>
</xsl:attribute>
<!--<xsl:value-of select="'__LABEL__'" />-->
<input>
<xsl:choose>
<xsl:when test="ancestor::odk:rank">
<xsl:call-template name="rank-item-attributes"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="binding-attributes">
<xsl:with-param name="binding" select="$binding"/>
<xsl:with-param name="nodeset" select="$nodeset"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:attribute name="value"></xsl:attribute>
</input>
</label>
</xsl:when>
<xsl:otherwise>
<option class="itemset-template" value="">
<xsl:attribute name="data-items-path">
<xsl:value-of select="@nodeset"/>
</xsl:attribute>
<xsl:value-of select="'...'"/>
</option>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="xf:itemset" mode="labels">
<xsl:variable name="value-ref" select="./xf:value/@ref" />
<xsl:variable name="label-ref" select="./xf:label/@ref" />
<xsl:variable name="iwq" select="substring-before(substring-after(@nodeset, 'instance('),')/')" />
<!-- Needs to also deal with randomize(instance("id")/path/to/node), randomize(instance("id")/path/to/node, 3) -->
<!-- Super inelegant and not robust without regexp:match -->
<xsl:variable name="instance-path-temp">
<xsl:choose>
<xsl:when test="contains(@nodeset, 'randomize(') and contains(@nodeset, ',')">
<xsl:value-of select="substring-before(substring-after(@nodeset, ')'), ',')"/>
</xsl:when>
<xsl:when test="contains(@nodeset, 'randomize(')">
<xsl:value-of select="substring-before(substring-after(@nodeset, ')'), ')')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after(@nodeset, ')')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="instance-path" select="str:replace($instance-path-temp, '/', '/xf:')" />
<xsl:variable name="instance-path-nofilter">
<xsl:call-template name="strip-filter">
<xsl:with-param name="string" select="$instance-path"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="instance-id" select="substring($iwq, 2, string-length($iwq)-2)" />
<span class="itemset-labels">
<xsl:attribute name="data-value-ref">
<xsl:value-of select="$value-ref"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="contains($label-ref, 'jr:itext(')">
<xsl:attribute name="data-label-type">
<xsl:value-of select="'itext'"/>
</xsl:attribute>
<xsl:variable name="label-node-name"
select="substring(substring-after($label-ref, 'itext('),1,string-length(substring-after($label-ref, 'itext('))-1)"/>
<xsl:attribute name="data-label-ref">
<xsl:value-of select="$label-node-name"/>
</xsl:attribute>
<xsl:for-each select="dyn:evaluate(concat('/h:html/h:head/xf:model/xf:instance[@id="', $instance-id, '"]', $instance-path-nofilter))">
<!-- so this is support for itext(node) (not itext(path/to/node)), but only 'ad-hoc' for itemset labels for now -->
<xsl:variable name="id" select="./*[name()=$label-node-name]" />
<xsl:call-template name="translations">
<xsl:with-param name="id" select="$id"/>
<xsl:with-param name="class" select="'option-label'"/>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="data-label-ref">
<xsl:value-of select="$label-ref"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
</span>
</xsl:template>
<!--
turns: /path/to/node[value=/some/other/node] into: /path/to/node
this function is probably way too aggressive but will work for xls-form generated forms
to do this properly a regexp:replace is required, but not supported in libXML
kept the recursion in, even though it is not being used right now
-->
<xsl:template name="strip-filter">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string, '[') and contains($string, ']')">
<xsl:value-of select="substring-before($string, '[')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="string-length(substring-after($string, ']')) > 0">
<xsl:call-template name="strip-filter">
<xsl:with-param name="string" select="substring-after($string,']')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<!--
turns STRING: '/path/to/node' into: /*[name()='path'/*[name()='to']/*[name()='node'
-->
<!--
<xsl:template name="string-to-path">
<xsl:param name="path-string"/>
<xsl:if test="starts-with($path-string, '/')">
<xsl:value-of select="'/'"/>
</xsl:if>
<xsl:choose>
<xsl:when test="contains($path-string, '/')">
<xsl:value-of select="substring-before($path-string, '/')"/>
<xsl:call-template name="string-to-path">
<xsl:with-param name="path-string" select="substring-after($path-string, '/')"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
-->
<xsl:template name="select-select">
<xsl:param name="nodeset"/>
<xsl:param name="binding"/>
<xsl:variable name="appearance" select="./@appearance" />
<xsl:variable name="datalist-id" select="translate($nodeset, ' _-.\/', '')"/>
<xsl:variable name="type">
<xsl:choose>
<xsl:when test="local-name() = 'select'">select_multiple</xsl:when>
<xsl:otherwise>select_one</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="element">
<xsl:choose>
<xsl:when test="local-name() = 'select1' and (contains(@appearance, 'autocomplete') or contains(@appearance, 'search'))">datalist</xsl:when>
<xsl:otherwise>select</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="options">
<xsl:choose>
<xsl:when test="contains(@appearance, 'picker')">
<xsl:apply-templates select="current()" mode="range-option-picker">
<xsl:with-param name="start" select="@start" />
<xsl:with-param name="end" select="@end" />
<xsl:with-param name="step" select="@step" />
<xsl:with-param name="readonly" select="$binding/@readonly = 'true()'" />
<xsl:with-param name="limit" select="500" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="xf:item" mode="select-option">
<xsl:with-param name="tolerate-spaces" select="$type = 'select_one'" />
<xsl:with-param name="readonly" select="$binding/@readonly = 'true()'" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<label>
<xsl:attribute name="class">
<xsl:value-of select="'question '"/>
<xsl:if test="./@appearance">
<xsl:call-template name="appearance" />
</xsl:if>
<xsl:if test="$binding/@relevant">
<xsl:value-of select="' or-branch pre-init '"/>
</xsl:if>
</xsl:attribute>
<xsl:apply-templates select="./@kb:image-customization"/>
<xsl:apply-templates select="xf:label" />
<xsl:apply-templates select="$binding/@required"/>
<xsl:apply-templates select="xf:hint" />
<xsl:if test="$element = 'datalist'">
<input>
<xsl:call-template name="binding-attributes">
<xsl:with-param name="nodeset" select="$nodeset" />
<xsl:with-param name="binding" select="$binding" />
<xsl:with-param name="type" select="$type" />
</xsl:call-template>
<!-- override type attribute -->
<xsl:attribute name="type">
<xsl:value-of select="'text'"/>
</xsl:attribute>
<xsl:attribute name="list">
<xsl:value-of select="$datalist-id"/>
</xsl:attribute>
</input>
</xsl:if>
<xsl:element name="{$element}">
<xsl:choose>
<xsl:when test="$element != 'datalist'">
<xsl:call-template name="binding-attributes">
<xsl:with-param name="nodeset" select="$nodeset" />
<xsl:with-param name="binding" select="$binding" />
<xsl:with-param name="type" select="$type" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="id">
<xsl:value-of select="$datalist-id"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="not(./xf:itemset)">
<option value="">...</option>
<xsl:for-each select="exsl:node-set($options)/option">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<!--<xsl:attribute name="data-itemset"/>-->
<xsl:apply-templates select="xf:itemset" mode="templates"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<span class="or-option-translations" style="display:none;">
<xsl:if test="not(./xf:itemset) and $translated = 'true'">
<xsl:for-each select="exsl:node-set($options)/span">
<xsl:copy-of select="." />
</xsl:for-each>
</xsl:if>
<xsl:text>
</xsl:text>
</span>
<xsl:if test="./xf:itemset">
<xsl:apply-templates select="xf:itemset" mode="labels"/>
</xsl:if>
<xsl:call-template name="constraint-and-required-msg" >
<xsl:with-param name="binding" select="$binding"/>
</xsl:call-template>
</label>
</xsl:template>
<xsl:template name="select-input">
<xsl:param name="nodeset"/>
<xsl:param name="binding"/>
<!--
legends are a royal pain-in-the-ass, but semantically correct to use. To restore sanity, the least
ugly solution that works regardless of the legend text + hint length (and showing a nice error background)
is to use a double fieldset (though another outer element would be okay too). Is consequence of being stingy with
# of DOM elements used.
-->
<fieldset>
<xsl:attribute name="class">
<xsl:value-of select="'question '"/>
<xsl:if test="not(contains(@appearance, 'compact') or contains(@appearance, 'list-nolabel') or contains(@appearance, 'label') or contains(@appearance, 'likert') or contains(@appearance, 'horizontal-compact') or contains(@appearance, 'no-buttons'))" >
<xsl:value-of select="'simple-select '"/>
</xsl:if>
<xsl:if test="local-name() = 'trigger'">
<xsl:value-of select="'trigger '"/>
</xsl:if>
<xsl:if test="$binding/@relevant">
<xsl:value-of select="'or-branch pre-init '"/>
</xsl:if>
<xsl:if test="@appearance">
<xsl:call-template name="appearance" />
</xsl:if>
</xsl:attribute>
<xsl:apply-templates select="./@kb:image-customization"/>
<xsl:apply-templates select="./@kb:flash"/>
<fieldset>
<!--<xsl:if test="./xf:itemset">
<xsl:attribute name="data-itemset"/>
</xsl:if>-->
<legend>
<xsl:apply-templates select="xf:label" />
<xsl:apply-templates select="$binding/@required"/>
<xsl:apply-templates select="xf:hint" />
<xsl:text>
</xsl:text>
</legend>
<xsl:if test="local-name() = 'rank'">
<input class="rank">
<xsl:call-template name="binding-attributes">
<xsl:with-param name="binding" select="$binding"/>
<xsl:with-param name="nodeset" select="$nodeset"/>
</xsl:call-template>
</input>
</xsl:if>
<div class="option-wrapper">
<xsl:choose>
<xsl:when test="local-name() = 'trigger'">
<label>
<input value="OK">
<xsl:call-template name="binding-attributes">
<xsl:with-param name="binding" select="$binding"/>
<xsl:with-param name="nodeset" select="$nodeset"/>
</xsl:call-template>
</input>
<span class="option-label active" lang="">
<xsl:value-of select="'OK'"/>
</span>
</label>
</xsl:when>
<xsl:when test="not(./xf:itemset)">
<xsl:apply-templates select="xf:item" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="xf:itemset" mode="templates">
<xsl:with-param name="nodeset" select="$nodeset" />
<xsl:with-param name="binding" select="$binding" />
</xsl:apply-templates>
<xsl:apply-templates select="xf:itemset" mode="labels"/>
</xsl:otherwise>
</xsl:choose>
</div>
</fieldset>
<xsl:call-template name="constraint-and-required-msg" >
<xsl:with-param name="binding" select="$binding"/>
</xsl:call-template>
</fieldset>
</xsl:template>
<!--
Don't add any logic or names to odk:rank items
-->
<xsl:template name="rank-item-attributes">
<xsl:attribute name="value">
<xsl:value-of select="./xf:value"/>
</xsl:attribute>
<xsl:attribute name="class">
<xsl:value-of select="'ignore'"/>
</xsl:attribute>
<xsl:attribute name="type">
<xsl:value-of select="'text'"/>
</xsl:attribute>
</xsl:template>
<!--
adds binding attributes to the context node, meant for <input>, <select>, <textarea>
-->
<xsl:template name="binding-attributes">
<xsl:param name="binding"/>
<xsl:param name="nodeset"/>
<xsl:param name="type"/>
<xsl:variable name="xml-type">
<xsl:call-template name="xml_type">
<xsl:with-param name="nodeset" select="$nodeset"/>
<!--<xsl:with-param name="binding" select="$binding"/>-->
</xsl:call-template>
</xsl:variable>
<xsl:variable name="html-input-type">
<xsl:call-template name="html_type">
<xsl:with-param name="xml_type" select="$xml-type" />
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$type = 'select_multiple'">
<xsl:attribute name="multiple">multiple</xsl:attribute>
</xsl:when>
<xsl:when test="$type = 'select_one'"></xsl:when>
<xsl:when test="$type = 'textarea'"></xsl:when>
<xsl:when test="$type = 'rank'">
<xsl:attribute name="type">rank</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="type">
<xsl:value-of select="$html-input-type"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:attribute name="name">
<xsl:value-of select="normalize-space($nodeset)" />
</xsl:attribute>
<xsl:if test="$html-input-type = 'radio'">
<xsl:attribute name="data-name">
<xsl:value-of select="normalize-space($nodeset)" />
</xsl:attribute>
</xsl:if>