summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tagpdf/tagpdf-roles.dtx
blob: ce860d71fa0890af3b19b9d3e6d94272d5a359b6 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
% \iffalse meta-comment
%
%% File: tagpdf-roles.dtx
%
% Copyright (C) 2019-2023 Ulrike Fischer
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "tagpdf bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/u-fischer/tagpdf
%
% for those people who are interested.
%<*driver>
\DocumentMetadata{}
\documentclass{l3doc}
\usepackage{array,booktabs,caption}
\hypersetup{pdfauthor=Ulrike Fischer,
 pdftitle=tagpdf-checks module (tagpdf)}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \title{^^A
%   The \pkg{tagpdf-roles} module\\ Tags, roles and namesspace code   ^^A
%   \\ Part of the tagpdf package
% }
%
% \author{^^A
%  Ulrike Fischer\thanks
%    {^^A
%      E-mail:
%        \href{mailto:fischer@troubleshooting-tex.de}
%          {fischer@troubleshooting-tex.de}^^A
%    }^^A
% }
%
% \date{Version 0.98g, released 2023-05-16}
% \maketitle
% \begin{documentation}
% \begin{function}
%  {
%    add-new-tag (setup-key),
%    tag (rolemap-key),
%    namespace (rolemap-key),
%    role (rolemap-key),
%    role-namespace (rolemap-key),
%  }
% The \texttt{add-new-tag} key can be used in \cs{tagpdfsetup} to declare and rolemap new tags.
% It takes as value a key-value list or a simple |new-tag/old-tag|.
%
% The key-value list knows the following keys:
% \begin{description}
% \item[\texttt{tag}] This is the name of the new tag as it should 
% then be used in \cs{tagstructbegin}.
% \item[\texttt{namespace}] This is the namespace of the new tag.
%   The value should be a shorthand of a namespace.
%   The allowed values are currently |pdf|, |pdf2|, |mathml|,|latex|, |latex-book| and |user|.
%   The default value (and recommended value for a new tag) is |user|.
%   The public name of the user namespace is |tag/NS/user|. This can be used to reference
%   the namespace e.g. in attributes.
% \item[\texttt{role}] This is the tag the tag should be mapped too.
%  In a PDF 1.7 or earlier this is normally a tag from the |pdf| set,
%  in PDF 2.0 from the |pdf|, |pdf2| and |mathml| set.
%  It can also be a user tag. The tag must be declared before, as the code retrieves
%  the class of the new tag from it. 
%  The PDF format allows mapping to be done transitively.
%  But tagpdf can't/won't check such unusual role mapping.
% \item[\texttt{role-namespace}] If the role is a known tag
%  the default value is the default namespace of this tag.
%  With this key a specific namespace can be forced.  
% \end{description}
% 
% Namespaces are mostly a PDF 2.0 property, but it doesn't harm to 
% set them also in a PDF 1.7 or earlier.
% \end{function}
% 
% \begin{function}[TF]{\tag_check_child:nn}
% \begin{syntax}
% \cs{tag_check_child:nn}\Arg{tag}\Arg{namespace} \Arg{true code} \Arg{false code}
% \end{syntax}
% This checks if the tag \meta{tag} from the name space \meta{namespace}
% can be used at the current position. In tagpdf-base it is always true. 
% \end{function}
% \end{documentation}
% \begin{implementation}
%    \begin{macrocode}
%<@@=tag>
%<*header>
\ProvidesExplPackage {tagpdf-roles-code} {2023-05-16} {0.98g}
 {part of tagpdf - code related to roles and structure names}
%</header>
%    \end{macrocode}
% \section{Code related to roles and structure names}
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% 
% \subsection{Variables}
% Tags are used in structures (\cs{tagstructbegin}) and mc-chunks (\cs{tagmcbegin}). 
% 
% They have a name (a string), in lua a number (for the lua attribute), and 
% in PDF 2.0 belong to one or more name spaces, with one being the default
% name space.
% 
% Tags of structures are classified, e.g. as grouping, 
% inline or block level structure (and a few special classes like lists and tables),
% and must follow containments rules depending on their classification 
% (for example a inline structure can not contain
% a block level structure). New tags inherit their 
% classification from their rolemapping to the standard namespaces (\texttt{pdf} 
% and/or \texttt{pdf2}). 
% We store this classification as it will probably 
% be needed for tests but currently the data is not much used. 
% The classification for math (and the containment rules) 
% is unclear currently and so not set.
% 
% The attribute number is only relevant in lua and only for the MC chunks 
% (so tags with the same name from different names spaces can have the same number), 
% and so only stored if luatex is detected. 
% 
% Due to the namespaces the storing and processing of tags and there data
% are different in various places for PDF~2.0 and PDF~<2.0, which makes 
% things a bit difficult and leads to some duplications. Perhaps at some time
% there should be a clear split.
% 
% This are the main variables used by the code:
% \begin{description}
% \item[\cs{g_@@_role_tags_NS_prop}] 
% This is the core list of tag names. It uses tags as keys
% and the shorthand (e.g. pdf2, or mathml) of the default name space as value. 
% 
% In pdf 2.0 the value is needed in the structure dictionaries.
% 
% \item[\cs{g_@@_role_tags_class_prop}]
% This contains for each tag a classification type. It is used in pdf <2.0.
% 
% \item[\cs{g_@@_role_NS_prop}] This contains the names spaces. The
% values are the object references. They are used in pdf 2.0.
%
% \item[\cs{g_@@_role_rolemap_prop}]
% This contains for each tag the role to a standard tag. 
% It is used in pdf<2.0 for tag checking and to fill at the end the 
% RoleMap dictionary.
% 
% \item[\texttt{g\_@@\_role/RoleMap\_dict}] This dictionary contains 
% the standard rolemaps. It is relevant only for pdf <2.0. 
% 
% \item[\cs{g_@@_role_NS_<ns>_prop}] This prop contains the tags of
% a name space and their role. The props are also use for 
% remapping. As value they contain two brace groups: tag and namespace.
% In pdf <2.0 the namespace is empty. 
% 
% \item[\cs{g_@@_role_NS_<ns>_class_prop}]   
% This prop contains the tags of
% a name space and their type. The value is only needed for pdf 2.0.      
% 
% \item[\cs{g_@@_role_index_prop}]   
% This prop contains the standard tags (pdf in pdf<2.0,
% pdf,pdf2 + mathml in pdf 2.0) as keys, the values are a two-digit
% number. These numbers are used to get the containment rule of two tags
% from the intarray.
% 
%\item[\cs{l_@@_role_debug_prop}] This property is used to pass some info
% around for info messages or debugging.      
% \end{description} 
%
%
% \begin{variable}{\g_@@_role_tags_NS_prop}
% This is the core list of tag names. It uses tags as keys
% and the shorthand (e.g. pdf2, or mathml) of the default name space as value.
% We store the default name space also in pdf <2.0, even if not needed: 
% it doesn't harm and simplifies the code. 
% There is no need to access this from lua, so we use the standard prop commands.
%    \begin{macrocode}
\prop_new:N    \g_@@_role_tags_NS_prop 
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_role_tags_class_prop}
% With pdf 2.0 we store the class in the NS dependant props. 
% With pdf <2.0 we store for now the type(s) of a tag in a common
% prop. 
% Tags that are rolemapped should get the type from
% the target.
%    \begin{macrocode}
\prop_new:N    \g_@@_role_tags_class_prop 
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}{\g_@@_role_NS_prop}
% This holds the list of supported name spaces.
% The keys are the name tagpdf will use, the values the object reference. 
% The urls identifier are stored in related dict object.
% \begin{description}
% \item[mathml] http://www.w3.org/1998/Math/MathML
% \item[pdf2]   http://iso.org/pdf2/ssn
% \item[pdf]    http://iso.org/pdf/ssn (default)
% \item[user]   \cs{c_@@_role_userNS_id_str} (random id, for user tags)
% \item[latex]  https://www.latex-project.org/ns/dflt/2022
% \item[latex-book] https://www.latex-project.org/ns/book/2022
% \item[latex-inline]     https://www.latex-project.org/ns/inline/2022
% \end{description}
% More namespaces are possible and
% their objects references and their rolemaps must be collected
% so that an array can be written to the StructTreeRoot at the end (see tagpdf-tree).
% We use a prop to store the object reference as it will be needed rather
% often.
%    \begin{macrocode}
\prop_new:N \g_@@_role_NS_prop 
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}{\g_@@_role_index_prop}
% This prop contains the standard tags (pdf in pdf<2.0,
% pdf,pdf2 + mathml in pdf 2.0) as keys, the values are a two-digit
% number. These numbers are used to get the containment rule of two tags
% from the intarray.
%    \begin{macrocode}
\prop_new:N \g_@@_role_index_prop
%    \end{macrocode}
% \end{variable}
% \begin{variable}{\l_@@_role_debug_prop}
% This variable is used to pass more infos to debug messages. 
%    \begin{macrocode}
\prop_new:N \l_@@_role_debug_prop
%    \end{macrocode}
% \end{variable}
% We need also a bunch of temporary variables.
% \begin{variable}
%  {
%     ,\l_@@_role_tag_tmpa_tl
%     ,\l_@@_role_tag_namespace_tmpa_tl
%     ,\l_@@_role_role_tmpa_tl
%     ,\l_@@_role_role_namespace_tmpa_tl      
%     ,\l_@@_role_tmpa_seq
%  }
%    \begin{macrocode}
\tl_new:N \l_@@_role_tag_tmpa_tl
\tl_new:N \l_@@_role_tag_namespace_tmpa_tl
\tl_new:N \l_@@_role_role_tmpa_tl
\tl_new:N \l_@@_role_role_namespace_tmpa_tl  
\seq_new:N\l_@@_role_tmpa_seq
%    \end{macrocode}
% \end{variable}
% 
% \subsection{Namespaces}
% The following commands setups a name space. 
% With pdf version $<$2.0 this is only a prop with the 
% rolemap. With pdf 2.0 a dictionary must be set up. 
% Such a name space dictionaries can
% contain an optional |/Schema| and |/RoleMapNS| entry. We only reserve the
% objects but delay the writing to the finish code, where we can test if the
% keys and the name spaces are actually needed.
% This commands setups objects for the name space and its rolemap. It also
% initialize a dict to collect the rolemaps if needed, and a property
% with the tags of the name space and their rolemapping for loops.
% It is unclear if a reference to a schema file will be ever needed, 
% but it doesn't harm \ldots.
% 
% \begin{variable}{g_@@_role/RoleMap_dict,\g_@@_role_rolemap_prop}
% This is the object which contains the normal RoleMap. It is probably not
% needed in pdf 2.0 but currently kept.
%    \begin{macrocode}
\pdfdict_new:n {g_@@_role/RoleMap_dict}
\prop_new:N \g_@@_role_rolemap_prop
%    \end{macrocode}
% \end{variable}
% 
% \begin{function}{\@@_role_NS_new:nnn}
%  \begin{syntax}
%   \cs{@@_role_NS_new:nnn}\Arg{shorthand}\Arg{URI-ID}{Schema}
%  \end{syntax}
% \end{function}
% \begin{macro}{\@@_role_NS_new:nnn}
%    \begin{macrocode}
\pdf_version_compare:NnTF < {2.0}
 {
   \cs_new_protected:Npn \@@_role_NS_new:nnn #1 #2 #3
    {
      \prop_new:c { g_@@_role_NS_#1_prop }
      \prop_new:c { g_@@_role_NS_#1_class_prop }
      \prop_gput:Nnx \g_@@_role_NS_prop {#1}{}
    }
 }
 {
  \cs_new_protected:Npn \@@_role_NS_new:nnn #1 #2 #3
    {
      \prop_new:c { g_@@_role_NS_#1_prop }
      \prop_new:c { g_@@_role_NS_#1_class_prop }
      \pdf_object_new:n {tag/NS/#1}      
      \pdfdict_new:n     {g_@@_role/Namespace_#1_dict}
      \pdf_object_new:n {@@/RoleMapNS/#1}
      \pdfdict_new:n     {g_@@_role/RoleMapNS_#1_dict}
      \pdfdict_gput:nnn
        {g_@@_role/Namespace_#1_dict}
        {Type}
        {/Namespace}
      \pdf_string_from_unicode:nnN{utf8/string}{#2}\l_@@_tmpa_str
      \tl_if_empty:NF \l_@@_tmpa_str
        {
          \pdfdict_gput:nnx
            {g_@@_role/Namespace_#1_dict}
            {NS}
            {\l_@@_tmpa_str}
        }
      %RoleMapNS is added in tree
      \tl_if_empty:nF  {#3}
       {
         \pdfdict_gput:nnx{g_@@_role/Namespace_#1_dict}
          {Schema}{#3}
       }
      \prop_gput:Nnx \g_@@_role_NS_prop {#1}{\pdf_object_ref:n{tag/NS/#1}~}
    }
 }  
%    \end{macrocode}
% \end{macro}
% We need an id for the user space. For the tests it should be possible
% to set it to a fix value. So we use random numbers which can
% be fixed by setting a seed. We fake a sort of
% GUID but do not try to be really exact as it doesn't matter ...
%
% \begin{variable}{\c_@@_role_userNS_id_str}
%    \begin{macrocode}
\str_const:Nx \c_@@_role_userNS_id_str
  { data:,
    \int_to_Hex:n{\int_rand:n {65535}}
    \int_to_Hex:n{\int_rand:n {65535}}
    -
    \int_to_Hex:n{\int_rand:n {65535}}
    -
    \int_to_Hex:n{\int_rand:n {65535}}
    -
    \int_to_Hex:n{\int_rand:n {65535}}
    -
    \int_to_Hex:n{\int_rand:n {16777215}}
    \int_to_Hex:n{\int_rand:n {16777215}}
  }
%    \end{macrocode}
% \end{variable}
% Now we setup the standard names spaces. 
% The mathml space is currently only loaded for pdf 2.0.
%    \begin{macrocode}
\@@_role_NS_new:nnn {pdf}   {http://iso.org/pdf/ssn}{}
\@@_role_NS_new:nnn {pdf2}  {http://iso.org/pdf2/ssn}{}
\pdf_version_compare:NnF < {2.0}
  {    
    \@@_role_NS_new:nnn {mathml}{http://www.w3.org/1998/Math/MathML}{}
  }  
\@@_role_NS_new:nnn {latex} {https://www.latex-project.org/ns/dflt/2022}{}
\@@_role_NS_new:nnn {latex-book} {https://www.latex-project.org/ns/book/2022}{}
\@@_role_NS_new:nnn {latex-inline} {https://www.latex-project.org/ns/inline/2022}{}
\exp_args:Nnx
  \@@_role_NS_new:nnn {user}{\c_@@_role_userNS_id_str}{}
%    \end{macrocode}
%
% \subsection{Adding a new tag}
% Both when reading the files and when setting up a tag manually
% we have to store data in various places.
% 
% \begin{macro}{\@@_role_alloctag:nnn}
% This command allocates a new tag without role mapping. In the 
% lua backend it will also record the attribute value.
%    \begin{macrocode}
\pdf_version_compare:NnTF < {2.0}
  {
   \sys_if_engine_luatex:TF
    {
      \cs_new_protected:Npn \@@_role_alloctag:nnn #1 #2 #3 %#1 tagname, ns, type
       {
         \lua_now:e { ltx.@@.func.alloctag ('#1') }
         \prop_gput:Nnn \g_@@_role_tags_NS_prop   {#1}{#2}
         \prop_gput:cnn {g_@@_role_NS_#2_prop}  {#1}{{}{}}
         \prop_gput:Nnn \g_@@_role_tags_class_prop {#1}{#3}
         \prop_gput:cnn {g_@@_role_NS_#2_class_prop}  {#1}{--UNUSED--}
       }      
    }
    {   
      \cs_new_protected:Npn \@@_role_alloctag:nnn #1 #2 #3
       {
         \prop_gput:Nnn \g_@@_role_tags_NS_prop   {#1}{#2}
         \prop_gput:cnn {g_@@_role_NS_#2_prop}  {#1}{{}{}}
         \prop_gput:Nnn \g_@@_role_tags_class_prop {#1}{#3}
         \prop_gput:cnn {g_@@_role_NS_#2_class_prop}  {#1}{--UNUSED--}
       } 
    }   
  }
  {
   \sys_if_engine_luatex:TF
    {
      \cs_new_protected:Npn \@@_role_alloctag:nnn #1 #2 #3 %#1 tagname, ns, type
       {
         \lua_now:e { ltx.@@.func.alloctag ('#1') }
         \prop_gput:Nnn \g_@@_role_tags_NS_prop   {#1}{#2}
         \prop_gput:cnn {g_@@_role_NS_#2_prop}  {#1}{{}{}}
         \prop_gput:Nnn \g_@@_role_tags_class_prop {#1}{--UNUSED--}
         \prop_gput:cnn {g_@@_role_NS_#2_class_prop}  {#1}{#3}
       }      
    }
    {   
      \cs_new_protected:Npn \@@_role_alloctag:nnn #1 #2 #3
       {
         \prop_gput:Nnn \g_@@_role_tags_NS_prop   {#1}{#2}
         \prop_gput:cnn {g_@@_role_NS_#2_prop}  {#1}{{}{}}
         \prop_gput:Nnn \g_@@_role_tags_class_prop {#1}{--UNUSED--}
         \prop_gput:cnn {g_@@_role_NS_#2_class_prop}  {#1}{#3}
       } 
    }   
  }   
\cs_generate_variant:Nn  \@@_role_alloctag:nnn {nnV}   
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{pdf 1.7 and earlier}
%
% \begin{macro}{\@@_role_add_tag:nn}
% The pdf 1.7 version has only two arguments: new and rolemap name.
% The role must be an existing tag and should not be empty.
% We allow to change the role of an existing tag: as the rolemap is written
% at the end not confusion can happen.
%    \begin{macrocode}
\cs_new_protected:Nn \@@_role_add_tag:nn % (new) name, reference to old
  {
%    \end{macrocode}
% checks and messages
%    \begin{macrocode}
    \@@_check_add_tag_role:nn {#1}{#2}
    \prop_if_in:NnF \g_@@_role_tags_NS_prop {#1}
      {
        \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
          {
            \msg_info:nnn { tag }{new-tag}{#1}
          }
      }
%    \end{macrocode}
% now the addition
%    \begin{macrocode}
    \prop_get:NnN \g_@@_role_tags_class_prop {#2}\l_@@_tmpa_tl
    \quark_if_no_value:NT \l_@@_tmpa_tl 
      {
        \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--}
      }        
    \@@_role_alloctag:nnV {#1}{user}\l_@@_tmpa_tl  
%    \end{macrocode}
% We resolve rolemapping recursively so that all targets are stored as standard
% tags.
%    \begin{macrocode}
    \tl_if_empty:nF { #2 }
      {
        \prop_get:NnN \g_@@_role_rolemap_prop {#2}\l_@@_tmpa_tl
        \quark_if_no_value:NTF \l_@@_tmpa_tl
          {          
            \prop_gput:Nnx \g_@@_role_rolemap_prop {#1}{\tl_to_str:n{#2}}
          }
          {
            \prop_gput:NnV \g_@@_role_rolemap_prop {#1}\l_@@_tmpa_tl
          }          
      }
  }
\cs_generate_variant:Nn \@@_role_add_tag:nn {VV,ne}
%    \end{macrocode}
% \end{macro}
% 
% For the parent-child test we must be able to get the role.
% We use the same number of arguments as for the 2.0 command. 
% If there is no role, we assume a standard tag.
% \begin{macro}{\@@_role_get:nnNN}
%    \begin{macrocode}
\pdf_version_compare:NnT < {2.0}
 {
   \cs_new:Npn \@@_role_get:nnNN #1#2#3#4 %#1 tag, #2 NS, #3 tlvar which hold the role tag #4 empty
    {
      \prop_get:NnNF \g_@@_role_rolemap_prop {#1}#3
        {
          \tl_set:Nn #3 {#1}          
        }
      \tl_set:Nn #4 {}  
    }
   \cs_generate_variant:Nn \@@_role_get:nnNN {VVNN} 
 }   
 
%    \end{macrocode}
% \end{macro}
% \subsubsection{The pdf 2.0 version}
% \begin{macro}{\@@_role_add_tag:nnnn}
% The pdf 2.0 version takes four arguments:
% tag/namespace/role/namespace
%    \begin{macrocode}
\cs_new_protected:Nn \@@_role_add_tag:nnnn %tag/namespace/role/namespace
  {
    \@@_check_add_tag_role:nnn {#1/#2}{#3}{#4}
    \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
      {
        \msg_info:nnn { tag }{new-tag}{#1}
      }
    \prop_get:cnN { g_@@_role_NS_#4_class_prop } {#3}\l_@@_tmpa_tl
    \quark_if_no_value:NT \l_@@_tmpa_tl 
      {
        \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--}
      }         
    \@@_role_alloctag:nnV {#1}{#2}\l_@@_tmpa_tl  
%    \end{macrocode}
% Do not remap standard tags. TODO add warning?
%    \begin{macrocode}
    \tl_if_in:nnF {-pdf-pdf2-mathml-}{-#2-} 
     {
       \pdfdict_gput:nnx {g_@@_role/RoleMapNS_#2_dict}{#1}
          {
            [
              \pdf_name_from_unicode_e:n{#3}
              \c_space_tl
              \pdf_object_ref:n {tag/NS/#4}
            ]
          }
     }  
%    \end{macrocode}
% We resolve rolemapping recursively so that all targets are stored as standard
% tags for the tests.
%    \begin{macrocode}
    \tl_if_empty:nF { #2 }
      {
        \prop_get:cnN { g_@@_role_NS_#4_prop } {#3}\l_@@_tmpa_tl
        \quark_if_no_value:NTF \l_@@_tmpa_tl
          {          
            \prop_gput:cnx { g_@@_role_NS_#2_prop } {#1}
              {{\tl_to_str:n{#3}}{\tl_to_str:n{#4}}}
          }
          {
            \prop_gput:cno { g_@@_role_NS_#2_prop } {#1}{\l_@@_tmpa_tl}
          }          
      }       
   }
\cs_generate_variant:Nn \@@_role_add_tag:nnnn {VVVV}
%    \end{macrocode}
% \end{macro}
% 
% For the parent-child test we must be able to get the role.
% We use the same number of arguments as for the <2.0 command (and assume
% that we don't need a name space)% 
% \begin{macro}{\@@_role_get:nnNN}
%    \begin{macrocode}
\pdf_version_compare:NnF < {2.0}
 {
   \cs_new:Npn \@@_role_get:nnNN #1#2#3#4 
     %#1 tag, #2 NS, 
     %#3 tlvar which hold the role tag
     %#4 tlvar which hold the name of the target NS
    {
      \prop_get:cnNTF {g_@@_role_NS_#2_prop} {#1}\l_@@_tmpa_tl
        {
         \tl_set:Nx #3 {\exp_last_unbraced:NV\use_i:nn   \l_@@_tmpa_tl}         
         \tl_set:Nx #4 {\exp_last_unbraced:NV\use_ii:nn  \l_@@_tmpa_tl}
        } 
        {
         \tl_set:Nn #3 {#1}
         \tl_set:Nn #4 {#2}
        }
    }
   \cs_generate_variant:Nn \@@_role_get:nnNN {VVNN} 
 }   
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Helper command to read the data from files}
% In this section we setup the helper command to read namespace files.

% \begin{macro}{\@@_role_read_namespace_line:nw}
% This command will process a line in the name space file.
% The first argument is the name of the name space. 
% The definition differ for pdf 2.0. as we have proper name spaces there.
% With pdf<2.0 not special name spaces shouldn't update the default role or add to the rolemap
% again. We use a boolean here.
%    \begin{macrocode}
\bool_new:N\l_@@_role_update_bool
\bool_set_true:N \l_@@_role_update_bool
%    \end{macrocode}
% 
%    \begin{macrocode}
\pdf_version_compare:NnTF < {2.0}
 {
  \cs_new_protected:Npn \@@_role_read_namespace_line:nw #1#2,#3,#4,#5,#6\q_stop %
   % #1 NS, #2 tag, #3 rolemap, #4 NS rolemap #5 type
    {
      \tl_if_empty:nF { #2 }
       {
        \bool_if:NTF \l_@@_role_update_bool
         {
          \tl_if_empty:nTF {#5}
            {
              \prop_get:NnN \g_@@_role_tags_class_prop  {#3}\l_@@_tmpa_tl
              \quark_if_no_value:NT \l_@@_tmpa_tl 
                {
                  \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--}
                } 
            }
            {              
              \tl_set:Nn \l_@@_tmpa_tl {#5} 
            }             
          \@@_role_alloctag:nnV {#2}{#1}\l_@@_tmpa_tl
          \tl_if_eq:nnF {#2}{#3} 
           { 
            \@@_role_add_tag:nn {#2}{#3} 
           }  
          \prop_gput:cnn {g_@@_role_NS_#1_prop}  {#2}{{#3}{}}   
         }
         {
           \prop_gput:cnn {g_@@_role_NS_#1_prop}  {#2}{{#3}{}}
           \prop_gput:cnn {g_@@_role_NS_#1_class_prop}  {#2}{--UNUSED--}
         }       
       }     
    }
 }  
 {
   \cs_new_protected:Npn \@@_role_read_namespace_line:nw #1#2,#3,#4,#5,#6\q_stop %
    % #1 NS, #2 tag, #3 rolemap, #4 NS rolemap #5 type
    {
      \tl_if_empty:nF {#2}
       {
        \tl_if_empty:nTF {#5}
         {
           \prop_get:cnN { g_@@_role_NS_#4_class_prop } {#3}\l_@@_tmpa_tl
           \quark_if_no_value:NT \l_@@_tmpa_tl 
             {
               \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--}
             } 
         }
         {
           \tl_set:Nn \l_@@_tmpa_tl {#5}
         }     
        \@@_role_alloctag:nnV {#2}{#1}\l_@@_tmpa_tl
        \bool_lazy_and:nnT
           { ! \tl_if_empty_p:n {#3} }{! \str_if_eq_p:nn {#1}{pdf2}} 
           {
            \@@_role_add_tag:nnnn {#2}{#1}{#3}{#4}            
           }            
        \prop_gput:cnn {g_@@_role_NS_#1_prop}  {#2}{{#3}{#4}}
       }     
    }
 }  
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@@_role_read_namespace:n}
% This command reads the namespace file.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_role_read_namespace:n #1 %name of namespace
  { 
    \prop_if_exist:cF {g_@@_role_NS_#1_prop}
      { \msg_warning:nnn {tag}{namespace-unknown}{#1} }
    \file_if_exist:nTF { tagpdf-ns-#1.def}
     { 
       \ior_open:Nn \g_tmpa_ior {tagpdf-ns-#1.def}
       \msg_info:nnn {tag}{read-namespace}{#1}
       \ior_map_inline:Nn \g_tmpa_ior 
         {
           \@@_role_read_namespace_line:nw {#1} ##1,,,,\q_stop 
         }
       \ior_close:N\g_tmpa_ior    
     }
     {
      \msg_warning:nnn{tag}{namespace-missing}{#1}
     }
  }   
   
%    \end{macrocode}
% \end{macro}
%

% 
% \subsection{Reading the default data}
% The order is important as we want pdf2 and latex as default.
%    \begin{macrocode}
\@@_role_read_namespace:n {pdf}
\@@_role_read_namespace:n {pdf2}
\pdf_version_compare:NnF < {2.0}
  {\@@_role_read_namespace:n {mathml}} 
\bool_set_false:N\l_@@_role_update_bool
\@@_role_read_namespace:n {latex-inline} 
\@@_role_read_namespace:n {latex-book}  
\bool_set_true:N\l_@@_role_update_bool
\@@_role_read_namespace:n {latex}
\@@_role_read_namespace:n {pdf}
\@@_role_read_namespace:n {pdf2}
%    \end{macrocode}
%
% But is the class provides a \cs{chapter} command then we switch 
%    \begin{macrocode}
\pdf_version_compare:NnTF < {2.0}
  {
    \hook_gput_code:nnn {begindocument}{tagpdf}
      {
        \cs_if_exist:NT \chapter
           {
             \prop_map_inline:cn{g_@@_role_NS_latex-book_prop}
               {
                 \@@_role_add_tag:ne {#1}{\use_i:nn #2\c_empty_tl\c_empty_tl}
               }
           }
      }
  }
  {
    \hook_gput_code:nnn {begindocument}{tagpdf}
      {
        \cs_if_exist:NT \chapter
         {
           \prop_map_inline:cn{g_@@_role_NS_latex-book_prop}
             {
               \prop_gput:Nnn \g_@@_role_tags_NS_prop    { #1 }{ latex-book }
             }
         }
      }  
  }
%    \end{macrocode}
% \subsection{Parent-child rules}
% PDF define various rules about which tag can be a child of another tag.
% The following code implements the matrix to allow to use it in tests.
% \begin{variable}{\g_@@_role_parent_child_intarray}
% This intarray will store the rule as a number. For parent nm and child ij 
% (n,m,i,j digits) the rule is at position nmij. As we have around 56 tags,
% we need roughly a size 6000.
%    \begin{macrocode}
\intarray_new:Nn \g_@@_role_parent_child_intarray {6000}
%    \end{macrocode}
% \end{variable}
% \begin{macro}{\c_@@_role_rules_prop,\c_@@_role_rules_num_prop}
% These two properties map the rule strings to numbers and back.
% There are in tagpdf-data.dtx near the csv files for easier maintenance.
% \end{macro}
% 
% \begin{macro}{\@@_store_parent_child_rule:nnn}
% The helper command is used to store the rule. 
% It assumes that parent and child are given as 2-digit number!
%    \begin{macrocode}
\cs_new_protected:Npn \@@_store_parent_child_rule:nnn #1 #2 #3 % num parent, num child, #3 string
  {
    \intarray_gset:Nnn \g_@@_role_parent_child_intarray 
      { #1#2 }{0\prop_item:Nn\c_@@_role_rules_prop{#3}}
  }  
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{Reading in the csv-files}
% This counter will be used to identify the first (non-comment) line
%    \begin{macrocode}
\int_zero:N  \l_@@_tmpa_int
%    \end{macrocode}
% Open the file depending on the PDF version
%    \begin{macrocode}
\pdf_version_compare:NnTF < {2.0}
  {
    \ior_open:Nn \g_tmpa_ior {tagpdf-parent-child.csv}
  }
  {
    \ior_open:Nn \g_tmpa_ior {tagpdf-parent-child-2.csv}
  }  
%    \end{macrocode}

%  Now the main loop over the file
%    \begin{macrocode}
\ior_map_inline:Nn \g_tmpa_ior 
  { 
%    \end{macrocode}
% ignore lines containing only comments
%    \begin{macrocode}
    \tl_if_empty:nF{#1}
      {
%    \end{macrocode}
% count the lines ...
%    \begin{macrocode}      
        \int_incr:N\l_@@_tmpa_int 
%    \end{macrocode}
%  put the line into a seq. Attention! empty cells are dropped.
%    \begin{macrocode}
        \seq_set_from_clist:Nn\l_@@_tmpa_seq { #1 } 
        \int_compare:nNnTF {\l_@@_tmpa_int}=1
%    \end{macrocode}
% This handles the header line. It gives the tags 2-digit numbers
%    \begin{macrocode}
          {         
            \seq_map_indexed_inline:Nn \l_@@_tmpa_seq
              {
                \prop_gput:Nnx\g_@@_role_index_prop 
                  {##2}
                  {\int_compare:nNnT{##1}<{10}{0}##1}           
              }  
          }
%    \end{macrocode}
% now the data lines.
%    \begin{macrocode}
         {
           \seq_set_from_clist:Nn\l_@@_tmpa_seq { #1 }
%    \end{macrocode}
% get the name of the child tag from the first column
%    \begin{macrocode}
           \seq_pop_left:NN\l_@@_tmpa_seq\l_@@_tmpa_tl
%    \end{macrocode}
% get the number of the child, and store it in  \cs{l_@@_tmpb_tl}
%    \begin{macrocode}
           \prop_get:NVN \g_@@_role_index_prop \l_@@_tmpa_tl \l_@@_tmpb_tl
%    \end{macrocode}
% remove column 2+3
%    \begin{macrocode}
           \seq_pop_left:NN\l_@@_tmpa_seq\l_@@_tmpa_tl
           \seq_pop_left:NN\l_@@_tmpa_seq\l_@@_tmpa_tl
%    \end{macrocode}
% Now map over the rest. The index \verb+##1+ gives us the 
% number of the parent, \verb+##2+ is the data.
%    \begin{macrocode}
           \seq_map_indexed_inline:Nn \l_@@_tmpa_seq 
             {       
               \exp_args:Nnx 
               \@@_store_parent_child_rule:nnn {##1}{\l_@@_tmpb_tl}{ ##2 }       
             }        
         }
      } 
  }
%    \end{macrocode}
% close the read handle.
%    \begin{macrocode}
\ior_close:N\g_tmpa_ior 
%    \end{macrocode}
% The Root,
% Hn and mathml tags are special and need to be added explicitly
%    \begin{macrocode}
\prop_get:NnN\g_@@_role_index_prop{StructTreeRoot}\l_@@_tmpa_tl
\prop_gput:Nnx\g_@@_role_index_prop{Root}{\l_@@_tmpa_tl}
\prop_get:NnN\g_@@_role_index_prop{Hn}\l_@@_tmpa_tl
\pdf_version_compare:NnTF < {2.0}
  {
    \int_step_inline:nn{6}
      {
        \prop_gput:Nnx\g_@@_role_index_prop{H#1}{\l_@@_tmpa_tl}
      }
  }
  {
    \int_step_inline:nn{10}
      {
        \prop_gput:Nnx\g_@@_role_index_prop{H#1}{\l_@@_tmpa_tl}
      }
%    \end{macrocode}
% all mathml tags are currently handled identically
%    \begin{macrocode}
    \prop_get:NnN\g_@@_role_index_prop {mathml}\l_@@_tmpa_tl
    \prop_get:NnN\g_@@_role_index_prop {math}\l_@@_tmpb_tl
    \prop_map_inline:Nn \g_@@_role_NS_mathml_prop  
      {
        \prop_gput:NnV\g_@@_role_index_prop{#1}\l_@@_tmpa_tl
      }  
    \prop_gput:NnV\g_@@_role_index_prop{math}\l_@@_tmpb_tl    
  }    
%    \end{macrocode}
%
% \subsubsection{Retrieving the parent-child rule}
% 
% 
% \begin{macro}{\@@_role_get_parent_child_rule:nnnN}
%  This command retrieves the rule (as a number) and stores it in the tl-var.
%  It assumes that the tag in \#1 is a standard tag after role mapping 
%  for which a rule exist and is \emph{not} one of Part, Div, NonStruct
%  as the real parent has already been identified.
%  \#3 can be used to pass along data about the original tags 
%  and is only used in messages.
%  
%  TODO check temporary variables. Check if the tl-var should be fix.
%    \begin{macrocode}
\tl_new:N \l_@@_parent_child_check_tl
\cs_new_protected:Npn \@@_role_get_parent_child_rule:nnnN #1 #2 #3 #4
  % #1 parent (string) #2 child (string) #3 text for messages (eg. about Div or Rolemapping) 
  % #4 tl for state
  {     
%    \end{macrocode}
% 
%    \begin{macrocode}
     \prop_get:NnN \g_@@_role_index_prop{#1}\l_@@_tmpa_tl
     \prop_get:NnN \g_@@_role_index_prop{#2}\l_@@_tmpb_tl
     \bool_lazy_and:nnTF 
       { ! \quark_if_no_value_p:N \l_@@_tmpa_tl }
       { ! \quark_if_no_value_p:N \l_@@_tmpb_tl }
       {
%    \end{macrocode}
% Get the rule from the intarray
%    \begin{macrocode}
         \tl_set:Nx#4 
           { 
             \intarray_item:Nn 
              \g_@@_role_parent_child_intarray 
              {\l_@@_tmpa_tl\l_@@_tmpb_tl}
           }
%    \end{macrocode}
% If the state is ‡ something is wrong ...
%    \begin{macrocode}
         \int_compare:nNnT  
           {#4} = {\prop_item:Nn\c_@@_role_rules_prop{‡}}  
           {            
             %warn ? 
%    \end{macrocode}
% we must take the current child from the stack if is already there, 
% depending on location the check is called, this could also remove the 
% parent, but that is ok too.
%    \begin{macrocode}
           }
%    \end{macrocode}
%   This is the message, this can perhaps go into debug mode.
%    \begin{macrocode}
         \group_begin:
         \int_compare:nNnT {\l_@@_tmpa_int*\l_@@_loglevel_int} > { 0 }
           {      
             \prop_get:NVNF\c_@@_role_rules_num_prop #4 \l_@@_tmpa_tl
               {
                 \tl_set:Nn \l_@@_tmpa_tl {unknown}
               }
             \tl_set:Nn \l_@@_tmpb_tl {#1}
             \msg_note:nnxxx 
               { tag } 
               { role-parent-child } 
               { #1 } 
               { #2 }                                      
               { 
                 #4~(='\l_@@_tmpa_tl') 
                  \iow_newline:
                  #3
               }   
           }  
           \group_end: 
       }
       {         
         \tl_set:Nn#4 {0}
         \msg_warning:nnxxx 
           { tag } 
           {role-parent-child} 
           { #1 } 
           { #2 }
           { unknown! }  
       }
  }  
\cs_generate_variant:Nn\@@_role_get_parent_child_rule:nnnN {VVVN,VVnN}   
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{@@_check_parent_child:nnnnN}
% This commands translates rolemaps its arguments and then
% calls \cs{@@_role_get_parent_child_rule:nnnN}. 
% It does not try to resolve inheritation of \texttt{Div} etc but
% instead warns that the rule can not be detected in this case. 
% In pdf 2.0 the name spaces of the tags are relevant, so we
% have arguments for them, but in pdf <2.0 they are ignored and can
% be left empty.
%    \begin{macrocode}
\pdf_version_compare:NnTF < {2.0}
  {
   \cs_new_protected:Npn \@@_check_parent_child:nnnnN #1 #2 #3 #4 #5
    %#1 parent tag,#2 NS, #3 child tag, #4 NS, #5 tl var
     {
%    \end{macrocode}
% for debugging messages we store the arguments.
%    \begin{macrocode}
       \prop_put:Nnn \l_@@_role_debug_prop {parent} {#1} 
       \prop_put:Nnn \l_@@_role_debug_prop {child}  {#3}  
%    \end{macrocode}
% get the standard tags through rolemapping if needed
% at first the parent
%    \begin{macrocode}
       \prop_get:NnNTF \g_@@_role_index_prop {#1}\l_@@_tmpa_tl
         {
           \tl_set:Nn \l_@@_tmpa_tl {#1}
         }
         {           
           \prop_get:NnNF \g_@@_role_rolemap_prop {#1}\l_@@_tmpa_tl 
             { 
               \tl_set:Nn \l_@@_tmpa_tl {\q_no_value}
             }
         }    
%    \end{macrocode}
% now the child
%    \begin{macrocode}
       \prop_get:NnNTF \g_@@_role_index_prop {#3}\l_@@_tmpb_tl
         {
           \tl_set:Nn \l_@@_tmpb_tl {#3}
         }
         {
           \prop_get:NnNF \g_@@_role_rolemap_prop {#3}\l_@@_tmpb_tl 
             {                
               \tl_set:Nn \l_@@_tmpb_tl {\q_no_value}
             }
         } 
%    \end{macrocode}
% if we got tags for parent and child we call the checking command
%    \begin{macrocode}
       \bool_lazy_and:nnTF 
         { ! \quark_if_no_value_p:N \l_@@_tmpa_tl }
         { ! \quark_if_no_value_p:N \l_@@_tmpb_tl }     
         {  
           \@@_role_get_parent_child_rule:VVnN 
             \l_@@_tmpa_tl \l_@@_tmpb_tl 
             {Rolemapped~from:~'#1'~-->~'#3'} 
             #5
         }   
         {
           \tl_set:Nn #5 {0}
           \msg_warning:nnxxx 
            { tag } 
            {role-parent-child} 
            { #1 } 
            { #3 }
            { unknown! }  
         }
     }
   \cs_new_protected:Npn \@@_check_parent_child:nnN #1#2#3 
     {
       \@@_check_parent_child:nnnnN {#1}{}{#2}{}#3
     }  
  }
%    \end{macrocode}
% and now the pdf 2.0 version
% The version with three arguments retrieves the default 
% names space and then calls the full command. 
% Not sure if this will ever be needed but we leave it for now. 
%    \begin{macrocode}
  {
   \cs_new_protected:Npn \@@_check_parent_child:nnN #1 #2 #3
     { 
       \prop_get:NnN\g_@@_role_tags_NS_prop {#1}\l_@@_role_tag_namespace_tmpa_tl  
       \prop_get:NnN\g_@@_role_tags_NS_prop {#2}\l_@@_role_tag_namespace_tmpb_tl
       \str_if_eq:nnT{#2}{MC}{\tl_clear:N \l_@@_role_tag_namespace_tmpb_tl}
       \bool_lazy_and:nnTF 
         { ! \quark_if_no_value_p:N \l_@@_role_tag_namespace_tmpa_tl }
         { ! \quark_if_no_value_p:N \l_@@_role_tag_namespace_tmpb_tl } 
         {
           \@@_check_parent_child:nVnVN 
             {#1}\l_@@_role_tag_namespace_tmpa_tl   
             {#2}\l_@@_role_tag_namespace_tmpb_tl   
             #3
         }        
         {
           \tl_set:Nn #3 {0}
           \msg_warning:nnxxx 
            { tag } 
            {role-parent-child} 
            { #1 } 
            { #2 }
            { unknown! }  
         }
     }
%    \end{macrocode}
% and now the real command.
%    \begin{macrocode}
   \cs_new_protected:Npn \@@_check_parent_child:nnnnN #1 #2 #3 #4 #5 %tag,NS,tag,NS, tl var
      { 
        \prop_put:Nnn \l_@@_role_debug_prop {parent} {#1/#2} 
        \prop_put:Nnn \l_@@_role_debug_prop {child}  {#3/#4}       
%    \end{macrocode}
%  If the namespace is empty, we assume a standard tag,
%  otherwise we retrieve the rolemapping from the namespace
%    \begin{macrocode} 
        \tl_if_empty:nTF  {#2}
          {   
            \tl_set:Nn \l_@@_tmpa_tl {#1}
          } 
          {           
            \prop_get:cnNTF 
               { g_@@_role_NS_#2_prop }
               {#1}
               \l_@@_tmpa_tl
               {
                 \tl_set:Nx \l_@@_tmpa_tl {\tl_head:N\l_@@_tmpa_tl}
                 \tl_if_empty:NT\l_@@_tmpa_tl
                   {
                     \tl_set:Nn \l_@@_tmpa_tl {#1}
                   } 
               }
               { 
                 \tl_set:Nn \l_@@_tmpa_tl {\q_no_value}
               }  
          }            
%    \end{macrocode}
% and the same for the child
%  If the namespace is empty, we assume a standard tag,
%  otherwise we retrieve the rolemapping from the namespace
%    \begin{macrocode}
        \tl_if_empty:nTF  {#4}
          {   
            \tl_set:Nn \l_@@_tmpb_tl {#3}
          } 
          { 
            \prop_get:cnNTF 
              { g_@@_role_NS_#4_prop }
              {#3}
              \l_@@_tmpb_tl
              {
                \tl_set:Nx \l_@@_tmpb_tl { \tl_head:N\l_@@_tmpb_tl }
                \tl_if_empty:NT\l_@@_tmpb_tl
                  {
                    \tl_set:Nn \l_@@_tmpb_tl {#3}
                  }             
              }
              { 
                \tl_set:Nn \l_@@_tmpb_tl {\q_no_value}
              } 
          }         
%    \end{macrocode}
% and now get the relation
%    \begin{macrocode}
       \bool_lazy_and:nnTF 
         { ! \quark_if_no_value_p:N \l_@@_tmpa_tl }
         { ! \quark_if_no_value_p:N \l_@@_tmpb_tl }     
         {  
           \@@_role_get_parent_child_rule:VVnN 
             \l_@@_tmpa_tl \l_@@_tmpb_tl 
             {Rolemapped~from~'#1/#2'~-->~'#3\str_if_empty:nF{#4}{/#4}'} 
             #5
         }   
         {
           \tl_set:Nn #5 {0}
           \msg_warning:nnxxx 
            { tag } 
            {role-parent-child} 
            { #1 } 
            { #3 }
            { unknown! }  
         }
     }     
  }
\cs_generate_variant:Nn\@@_check_parent_child:nnN {VVN}  
\cs_generate_variant:Nn\@@_check_parent_child:nnnnN {VVVVN,nVnVN,VVnnN}  
%</package>
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}[TF]{\tag_check_child:nn}
%    \begin{macrocode}
%<base>\prg_new_protected_conditional:Npnn \tag_check_child:nn #1 #2 {T,F,TF}{\prg_return_true:}
%<*package>
\prg_set_protected_conditional:Npnn \tag_check_child:nn #1 #2 {T,F,TF}
 {
   \seq_get:NN\g_@@_struct_stack_seq\l_@@_tmpa_tl
   \@@_struct_get_parentrole:eNN
      {\l_@@_tmpa_tl}
      \l_@@_get_parent_tmpa_tl
      \l_@@_get_parent_tmpb_tl
   \@@_check_parent_child:VVnnN
     \l_@@_get_parent_tmpa_tl
     \l_@@_get_parent_tmpb_tl
      {#1}{#2}
      \l_@@_parent_child_check_tl
   \int_compare:nNnTF {  \l_@@_parent_child_check_tl } < {0}
      {\prg_return_false:} 
      {\prg_return_true:}
 } 
%    \end{macrocode}
% \end{macro}
%
% \subsection{Remapping of tags}
% In some context it can be necessary to remap or replace the tags. 
% That means instead of tag=H1 or tag=section one wants the effect of tag=Span.
% Or instead of tag=P one wants tag=Code.
% 
% The following command provide some general interface for this.
% The core idea is that before a tag is set it is fed through a function
% that can change it. We want to be able to chain such functions,
% so all of them manipulate the same variables.
% 
% \begin{variable}{\l_@@_role_remap_tag_tl,\l_@@_role_remap_NS_tl}
%    \begin{macrocode}
\tl_new:N \l_@@_role_remap_tag_tl
\tl_new:N \l_@@_role_remap_NS_tl
%    \end{macrocode}
% \end{variable}
% \begin{macro}{\@@_role_remap:}
% This function is used in the structure and the mc code before using a tag. By default it
% does nothing with the tl vars. Perhaps this should be a hook?
%    \begin{macrocode}
\cs_new_protected:Npn \@@_role_remap: {  } 
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_role_remap_id: }
% This is copy in case we have to restore the main command.
%    \begin{macrocode}
\cs_set_eq:NN \@@_role_remap_id: \@@_role_remap:
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_role_remap_inline:}
% The mapping is meant to \enquote{degrade} tags, e.g. if used
% inside some complex object. 
% The pdf<2.0 code maps the tag to the new role, the pdf 2.0 code only
% switch the NS.
%    \begin{macrocode}
\pdf_version_compare:NnTF < {2.0}
  {
    \cs_new_protected:Npn \@@_role_remap_inline:
      {
        \prop_get:cVNT { g_@@_role_NS_latex-inline_prop }\l_@@_role_remap_tag_tl\l_@@_tmpa_tl
          {
            \tl_set:Nx\l_@@_role_remap_tag_tl
              {
                \exp_last_unbraced:NV\use_i:nn \l_@@_tmpa_tl
              }
            \tl_set:Nx\l_@@_role_remap_NS_tl 
              {
                \exp_last_unbraced:NV\use_ii:nn \l_@@_tmpa_tl
              }
          }
        \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
          {
            \msg_note:nnx { tag } { role-remapping }{ \l_@@_role_remap_tag_tl }
          }  
      }
  }
  {
    \cs_new_protected:Npn \@@_role_remap_inline:
      {
        \prop_get:cVNT { g_@@_role_NS_latex-inline_prop }\l_@@_role_remap_tag_tl\l_@@_tmpa_tl
          {
            \tl_set:Nn\l_@@_role_remap_NS_tl {latex-inline}
          }
        \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
          {
            \msg_note:nnx { tag } { role-remapping }{ \l_@@_role_remap_tag_tl/latex-inline }
          }  
      }    
  }    
%    \end{macrocode}
% \end{macro}
%
% \subsection{Key-val user interface}
%  The user interface uses the key |add-new-tag|, which takes either a
%  keyval list as argument, or a tag/role.
%  \begin{macro}
%    {
%     tag (rolemap-key),
%     tag-namespace (rolemap-key),
%     role (rolemap-key),
%     role-namespace (rolemap-key),
%     add-new-tag (setup-key)}
%    \begin{macrocode}
\keys_define:nn { @@ / tag-role }
  {
    ,tag .tl_set:N = \l_@@_role_tag_tmpa_tl
    ,tag-namespace  .tl_set:N = \l_@@_role_tag_namespace_tmpa_tl
    ,role .tl_set:N = \l_@@_role_role_tmpa_tl
    ,role-namespace .tl_set:N = \l_@@_role_role_namespace_tmpa_tl
  }

\keys_define:nn { @@ / setup }
  {
    add-new-tag .code:n =
     {
       \keys_set_known:nnnN
         {@@/tag-role}
         {
           tag-namespace=user,
           role-namespace=, %so that we can test for it.
          #1
         }{@@/tag-role}\l_tmpa_tl
       \tl_if_empty:NF \l_tmpa_tl
         {
           \exp_args:NNno \seq_set_split:Nnn \l_tmpa_seq { / } {\l_tmpa_tl/}
           \tl_set:Nx \l_@@_role_tag_tmpa_tl  { \seq_item:Nn \l_tmpa_seq {1} }
           \tl_set:Nx \l_@@_role_role_tmpa_tl { \seq_item:Nn \l_tmpa_seq {2} }
         }
      \tl_if_empty:NT \l_@@_role_role_namespace_tmpa_tl
         {
           \prop_get:NVNTF
             \g_@@_role_tags_NS_prop
             \l_@@_role_role_tmpa_tl
             \l_@@_role_role_namespace_tmpa_tl
             {
                \prop_if_in:NVF\g_@@_role_NS_prop \l_@@_role_role_namespace_tmpa_tl
                 {
                   \tl_set:Nn \l_@@_role_role_namespace_tmpa_tl {user}
                 }
             }
             {
               \tl_set:Nn \l_@@_role_role_namespace_tmpa_tl {user}
             }
         }
      \pdf_version_compare:NnTF < {2.0}
       {
        %TODO add check for emptyness?
          \@@_role_add_tag:VV
              \l_@@_role_tag_tmpa_tl
              \l_@@_role_role_tmpa_tl
       }
       {
         \@@_role_add_tag:VVVV
           \l_@@_role_tag_tmpa_tl
           \l_@@_role_tag_namespace_tmpa_tl
           \l_@@_role_role_tmpa_tl
           \l_@@_role_role_namespace_tmpa_tl
       }
    }
  }
%</package>
%    \end{macrocode}
% \end{macro}
% \end{implementation}
% \PrintIndex