/
xml2_node.cpp
786 lines (640 loc) · 18.5 KB
/
xml2_node.cpp
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
#define R_NO_REMAP
#include <Rinternals.h>
#undef R_NO_REMAP
#include <libxml/tree.h>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include "xml2_types.h"
#include "xml2_utils.h"
template<typename T> // for xmlAttr and xmlNode
std::string nodeName(T* node, SEXP nsMap) {
std::string name = Xml2String(node->name).asStdString();
if (Rf_xlength(nsMap) == 0) {
return name;
}
xmlNs* ns = node->ns;
if (ns == NULL)
return name;
std::string prefix = NsMap(nsMap).findPrefix(Xml2String(ns->href).asStdString());
return prefix + ":" + name;
}
// [[export]]
extern "C" SEXP node_name(SEXP node_sxp, SEXP nsMap) {
BEGIN_CPP
XPtrNode node(node_sxp);
std::string name = nodeName(node.checked_get(), nsMap);
return Rf_ScalarString(Rf_mkCharLenCE(name.c_str(), name.size(), CE_UTF8));
END_CPP
}
// [[export]]
extern "C" SEXP node_set_name(SEXP node_sxp, SEXP value) {
BEGIN_CPP
XPtrNode node(node_sxp);
xmlNodeSetName(node, asXmlChar(value));
return R_NilValue;
END_CPP
}
// [[export]]
extern "C" SEXP node_text(SEXP node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
return Rf_ScalarString(Xml2String(xmlNodeGetContent(node.checked_get())).asRString());
END_CPP
}
bool hasPrefix(std::string lhs, std::string rhs) {
if (lhs.length() > rhs.length()) {
return false;
}
return std::equal(
lhs.begin(),
lhs.end(),
rhs.begin());
}
const xmlChar* xmlNsDefinition(xmlNodePtr node, const xmlChar* lookup) {
xmlNsPtr next = node->nsDef;
while(next != NULL) {
// default namespace
if (xmlStrEqual(next->prefix, lookup)) {
return next->href;
}
next = next->next;
}
return NULL;
}
// [[export]]
extern "C" SEXP node_attr(
SEXP node_sxp,
SEXP name_sxp,
SEXP missing_sxp,
SEXP nsMap_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
std::string name(CHAR(STRING_ELT(name_sxp, 0)));
if (Rf_xlength(missing_sxp) != 1) {
Rf_error("`missing` should be length 1");
}
SEXP missingVal = STRING_ELT(missing_sxp, 0);
if (name == "xmlns") {
return Rf_ScalarString(Xml2String(xmlNsDefinition(node, NULL)).asRString(missingVal));
}
if (hasPrefix("xmlns:", name)) {
std::string prefix = name.substr(6);
return Rf_ScalarString(Xml2String(xmlNsDefinition(node, asXmlChar(prefix))).asRString(missingVal));
}
xmlChar* string;
if (Rf_xlength(nsMap_sxp) == 0) {
string = xmlGetProp(node.checked_get(), asXmlChar(name));
} else {
size_t colon = name.find(':');
if (colon == std::string::npos) {
// Has namespace spec, but attribute not qualified, so look for attribute
// without namespace
string = xmlGetNoNsProp(node.checked_get(), asXmlChar(name));
} else {
// Split name into prefix & attr, then look up full url
std::string
prefix = name.substr(0, colon),
attr = name.substr(colon + 1, name.size() - 1);
std::string url = NsMap(nsMap_sxp).findUrl(prefix);
string = xmlGetNsProp(node.checked_get(), asXmlChar(attr), asXmlChar(url));
}
}
return Rf_ScalarString(Xml2String(string).asRString(missingVal));
END_CPP
}
// [[export]]
extern "C" SEXP node_attrs(SEXP node_sxp, SEXP nsMap_sxp) {
BEGIN_CPP
XPtrNode node_(node_sxp);
int n = 0;
xmlNodePtr node = node_.checked_get();
if (node->type == XML_ELEMENT_NODE) {
// attributes
for(xmlAttr* cur = node->properties; cur != NULL; cur = cur->next)
n++;
// namespace definitions
for(xmlNsPtr cur = node->nsDef; cur != NULL; cur = cur->next)
n++;
SEXP names = PROTECT(Rf_allocVector(STRSXP, n));
SEXP values = PROTECT(Rf_allocVector(STRSXP, n));
int i = 0;
for(xmlAttr* cur = node->properties; cur != NULL; cur = cur->next, ++i) {
std::string name = nodeName(cur, nsMap_sxp);
SET_STRING_ELT(names, i, Rf_mkCharLenCE(name.c_str(), name.size(), CE_UTF8));
xmlNs* ns = cur->ns;
if (ns == NULL) {
if (Rf_xlength(nsMap_sxp) > 0) {
SET_STRING_ELT(values, i, Xml2String(xmlGetNoNsProp(node, cur->name)).asRString());
} else {
SET_STRING_ELT(values, i, Xml2String(xmlGetProp(node, cur->name)).asRString());
}
} else {
SET_STRING_ELT(values, i, Xml2String(xmlGetNsProp(node, cur->name, ns->href)).asRString());
}
}
for(xmlNsPtr cur = node->nsDef; cur != NULL; cur = cur->next, ++i) {
if (cur->prefix == NULL) {
SET_STRING_ELT(names, i, Rf_mkChar("xmlns"));
} else {
std::string name = std::string("xmlns:") + Xml2String(cur->prefix).asStdString();
SET_STRING_ELT(names,i, Rf_mkCharLenCE(name.c_str(), name.size(), CE_UTF8));
}
SET_STRING_ELT(values, i, Xml2String(cur->href).asRString());
}
Rf_setAttrib(values, R_NamesSymbol, names);
UNPROTECT(2);
return values;
}
return Rf_allocVector(STRSXP, 0);
END_CPP
}
// Fix the tree by removing the namespace pointers to the given tree
void xmlRemoveNamespace(xmlNodePtr tree, xmlNsPtr ns) {
// From https://github.com/GNOME/libxml2/blob/v2.9.2/tree.c#L6440
//
xmlNodePtr node = tree;
/*
* Browse the full subtree, deep first
*/
while(node != NULL) {
if (node->ns != NULL && node->ns == ns) {
node->ns = NULL;
}
// Check for namespaces on the attributes
if (ns->prefix != NULL && // default namespaces will not exist on attributes
node->type == XML_ELEMENT_NODE) {
xmlAttrPtr attr = node->properties;
while (attr != NULL) {
if (attr->ns != NULL && attr->ns == ns) {
attr->ns = NULL;
}
attr = attr->next;
}
}
if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
/* deep first */
node = node->children;
} else if ((node != tree) && (node->next != NULL)) {
/* then siblings */
node = node->next;
} else if (node != tree) {
/* go up to parents->next if needed */
while (node != tree) {
if (node->parent != NULL)
node = node->parent;
if ((node != tree) && (node->next != NULL)) {
node = node->next;
break;
}
if (node->parent == NULL) {
node = NULL;
break;
}
}
/* exit condition */
if (node == tree)
node = NULL;
} else
break;
}
return;
}
// Fix the tree by adding the namespace pointers to the given tree
void xmlAddNamespace(xmlNodePtr tree, xmlNsPtr ns) {
// Only needed for default namespaces
if (ns->prefix != NULL) {
return;
}
// From https://github.com/GNOME/libxml2/blob/v2.9.2/tree.c#L6440
//
xmlNodePtr node = tree;
/*
* Browse the full subtree, deep first
*/
while(node != NULL) {
if (node->ns == NULL) {
node->ns = ns;
}
if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
/* deep first */
node = node->children;
} else if ((node != tree) && (node->next != NULL)) {
/* then siblings */
node = node->next;
} else if (node != tree) {
/* go up to parents->next if needed */
while (node != tree) {
if (node->parent != NULL)
node = node->parent;
if ((node != tree) && (node->next != NULL)) {
node = node->next;
break;
}
if (node->parent == NULL) {
node = NULL;
break;
}
}
/* exit condition */
if (node == tree)
node = NULL;
} else
break;
}
return;
}
void removeNs(xmlNodePtr node, const xmlChar* prefix) {
if (node == NULL) {
return;
}
if (node->nsDef == NULL) {
return;
}
xmlNsPtr prev = node->nsDef;
if (xmlStrEqual(prev->prefix, prefix)) {
node->nsDef = prev->next;
xmlRemoveNamespace(node, prev);
xmlFreeNs(prev);
return;
}
while(prev->next != NULL) {
xmlNsPtr cur = prev->next;
if (xmlStrEqual(cur->prefix, prefix)) {
prev->next = cur->next;
xmlRemoveNamespace(node, cur);
xmlFreeNs(cur);
return;
}
prev = prev->next;
}
return;
}
// [[export]]
extern "C" SEXP node_set_attr(SEXP node_sxp, SEXP name_sxp, SEXP value, SEXP nsMap) {
BEGIN_CPP
XPtrNode node_(node_sxp);
std::string name(CHAR(STRING_ELT(name_sxp, 0)));
const xmlNodePtr node = node_.checked_get();
if (name == "xmlns") {
xmlAddNamespace(node, xmlNewNs(node, asXmlChar(value), NULL));
return R_NilValue;
}
if (hasPrefix("xmlns:", name)) {
std::string prefix = name.substr(6);
removeNs(node, asXmlChar(prefix));
xmlAddNamespace(node, xmlNewNs(node, asXmlChar(value), asXmlChar(prefix)));
return R_NilValue;
}
if (Rf_xlength(nsMap) == 0) {
xmlSetProp(node, asXmlChar(name), asXmlChar(value));
} else {
size_t colon = name.find(':');
if (colon == std::string::npos) {
// Has namespace spec, but attribute not qualified, so just use that name
xmlSetProp(node, asXmlChar(name), asXmlChar(value));
} else {
// Split name into prefix & attr, then look up full url
std::string
prefix = name.substr(0, colon),
attr = name.substr(colon + 1, name.size() - 1);
std::string url = NsMap(nsMap).findUrl(prefix);
xmlNsPtr ns = xmlSearchNsByHref(node_->doc, node, asXmlChar(url));
xmlSetNsProp(node, ns, asXmlChar(attr), asXmlChar(value));
}
}
return R_NilValue;
END_CPP
}
// [[export]]
extern "C" SEXP node_remove_attr(SEXP node_sxp, SEXP name_sxp, SEXP nsMap) {
BEGIN_CPP
XPtrNode node_(node_sxp);
std::string name(CHAR(STRING_ELT(name_sxp, 0)));
const xmlNodePtr node = node_.checked_get();
if (name == "xmlns") {
removeNs(node, NULL);
return R_NilValue;
}
if (hasPrefix("xmlns:", name)) {
std::string prefix = name.substr(6);
removeNs(node, asXmlChar(prefix));
return R_NilValue;
}
if (Rf_xlength(nsMap) == 0) {
xmlUnsetProp(node, asXmlChar(name));
} else {
size_t colon = name.find(':');
if (colon == std::string::npos) {
// Has namespace spec, but attribute not qualified, so just use that name
xmlUnsetNsProp(node, NULL, asXmlChar(name));
} else {
// Split name into prefix & attr, then look up full url
std::string
prefix = name.substr(0, colon),
attr = name.substr(colon + 1, name.size() - 1);
std::string url = NsMap(nsMap).findUrl(prefix);
xmlNsPtr ns = xmlSearchNsByHref(node_->doc, node, asXmlChar(url));
xmlUnsetNsProp(node, ns, asXmlChar(attr));
}
}
return R_NilValue;
END_CPP
}
SEXP asList(std::vector<xmlNode*> nodes) {
SEXP out = PROTECT(Rf_allocVector(VECSXP, nodes.size()));
for (size_t i = 0; i < nodes.size(); ++i) {
XPtrNode node(nodes[i]);
SET_VECTOR_ELT(out, i, SEXP(node));
}
UNPROTECT(1);
return out;
}
// [[export]]
extern "C" SEXP node_children(SEXP node_sxp, SEXP only_node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
bool only_node = LOGICAL(only_node_sxp)[0];
std::vector<xmlNode*> out;
for(xmlNode* cur = node->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (only_node && cur->type != XML_ELEMENT_NODE) {
continue;
}
out.push_back(cur);
}
return asList(out);
END_CPP
}
// [[export]]
extern "C" SEXP node_length(SEXP node_sxp, SEXP only_node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
bool only_node = LOGICAL(only_node_sxp)[0];
int i = 0;
for(xmlNode* cur = node->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (only_node && cur->type != XML_ELEMENT_NODE) {
continue;
}
++i;
}
return Rf_ScalarInteger(i);
END_CPP
}
// [[export]]
extern "C" SEXP node_has_children(SEXP node_sxp, SEXP only_node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
bool only_node = LOGICAL(only_node_sxp)[0];
for(xmlNode* cur = node->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (only_node && cur->type != XML_ELEMENT_NODE) {
continue;
}
return Rf_ScalarLogical(true);
}
return Rf_ScalarLogical(false);
END_CPP
}
// [[export]]
extern "C" SEXP node_parents(SEXP node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
std::vector<xmlNode*> out;
for(xmlNode* cur = node->parent; cur != NULL; cur = cur->parent) {
if (cur->type != XML_ELEMENT_NODE)
continue;
out.push_back(cur);
}
return asList(out);
END_CPP
}
// [[export]]
extern "C" SEXP node_siblings(SEXP node_sxp, SEXP only_node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
bool only_node = LOGICAL(only_node_sxp)[0];
std::vector<xmlNode*> out;
xmlNode* parent = node->parent;
if (parent == NULL)
return Rf_allocVector(VECSXP, 0);
for(xmlNode* cur = parent->xmlChildrenNode; cur != NULL; cur = cur->next) {
if (cur == node) {
continue;
}
if (only_node && cur->type != XML_ELEMENT_NODE) {
continue;
}
out.push_back(cur);
}
return asList(out);
END_CPP
}
// [[export]]
extern "C" SEXP node_parent(SEXP node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
if (node->parent == NULL) {
Rf_error("Parent does not exist");
}
XPtrNode out(node->parent);
return SEXP(out);
END_CPP
}
// [[export]]
extern "C" SEXP node_path(SEXP node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
return Rf_ScalarString(Xml2String(xmlGetNodePath(node.checked_get())).asRString());
END_CPP
}
// [[export]]
extern "C" SEXP nodes_duplicated(SEXP nodes) {
BEGIN_CPP
std::set<xmlNode*> seen;
int n = Rf_xlength(nodes);
SEXP out = PROTECT(Rf_allocVector(LGLSXP, n));
for (int i = 0; i < n; ++i) {
bool result;
SEXP cur = VECTOR_ELT(nodes, i);
if (Rf_inherits(cur, "xml_node")) {
XPtrNode node(VECTOR_ELT(cur, 0));
result = !seen.insert(node.checked_get()).second;
} else if (Rf_inherits(cur, "xml_missing")) {
result = false;
} else {
XPtrNode node(cur);
result = !seen.insert(node.checked_get()).second;
}
LOGICAL(out)[i] = result;
}
UNPROTECT(1);
return out;
END_CPP
}
// [[export]]
extern "C" SEXP node_type(SEXP node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
return Rf_ScalarInteger(node->type);
END_CPP
}
// [[export]]
extern "C" SEXP node_copy(SEXP node_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
XPtrNode copy(xmlCopyNode(node.checked_get(), 1));
return SEXP(copy);
END_CPP
}
// [[export]]
extern "C" SEXP node_set_content(SEXP node_sxp, SEXP content) {
BEGIN_CPP
XPtrNode node(node_sxp);
xmlNodeSetContentLen(node.checked_get(), asXmlChar(content), Rf_xlength(STRING_ELT(content, 0)));
return R_NilValue;
END_CPP
}
// [[export]]
extern "C" SEXP node_append_content(SEXP node_sxp, SEXP content) {
BEGIN_CPP
XPtrNode node(node_sxp);
xmlNodeAddContentLen(node.checked_get(), asXmlChar(content), Rf_xlength(STRING_ELT(content, 0)));
return R_NilValue;
END_CPP
}
// [[export]]
extern "C" SEXP node_new_text(SEXP node_sxp, SEXP content) {
BEGIN_CPP
XPtrNode node(node_sxp);
xmlAddChild(node.checked_get(), xmlNewTextLen(asXmlChar(content), Rf_xlength(STRING_ELT(content, 0))));
return R_NilValue;
END_CPP
}
// [[export]]
extern "C" SEXP node_append_child(SEXP parent_sxp, SEXP cur_sxp) {
BEGIN_CPP
XPtrNode parent(parent_sxp);
XPtrNode cur(cur_sxp);
XPtrNode out(xmlAddChild(parent.checked_get(), cur.checked_get()));
return SEXP(out);
END_CPP
}
// [[export]]
extern "C" SEXP node_prepend_child(SEXP parent_sxp, SEXP cur_sxp) {
BEGIN_CPP
XPtrNode parent(parent_sxp);
XPtrNode cur(cur_sxp);
XPtrNode out(xmlAddPrevSibling(parent.checked_get()->children, cur.checked_get()));
return SEXP(out);
END_CPP
}
// Previous sibling
// [[export]]
extern "C" SEXP node_prepend_sibling(SEXP cur_sxp, SEXP elem_sxp) {
BEGIN_CPP
XPtrNode cur(cur_sxp);
XPtrNode elem(elem_sxp);
XPtrNode out(xmlAddPrevSibling(cur.checked_get(), elem.checked_get()));
return SEXP(out);
END_CPP
}
// Append sibling
// [[export]]
extern "C" SEXP node_append_sibling(SEXP cur_sxp, SEXP elem_sxp) {
BEGIN_CPP
XPtrNode cur(cur_sxp);
XPtrNode elem(elem_sxp);
XPtrNode out(xmlAddNextSibling(cur.checked_get(), elem.checked_get()));
return SEXP(out);
END_CPP
}
// Replace node
// [[export]]
extern "C" SEXP node_replace(SEXP old_sxp, SEXP cur_sxp) {
BEGIN_CPP
XPtrNode old(old_sxp);
XPtrNode cur(cur_sxp);
XPtrNode out(xmlReplaceNode(old.checked_get(), cur.checked_get()));
return SEXP(out);
END_CPP
}
// [[export]]
extern "C" SEXP node_remove(SEXP node_sxp, SEXP free_sxp) {
BEGIN_CPP
XPtrNode node(node_sxp);
bool free = LOGICAL(free_sxp)[0];
xmlUnlinkNode(node.checked_get());
if (free) {
xmlFreeNode(node.checked_get());
}
return R_NilValue;
END_CPP
}
// [[export]]
extern "C" SEXP node_new(SEXP name) {
BEGIN_CPP
XPtrNode node(xmlNewNode(NULL, asXmlChar(name)));
return SEXP(node);
END_CPP
}
// [[export]]
extern "C" SEXP node_cdata_new(SEXP doc_sxp, SEXP content_sxp) {
BEGIN_CPP
XPtrDoc doc(doc_sxp);
XPtrNode node(xmlNewCDataBlock(doc.checked_get(), asXmlChar(content_sxp), Rf_xlength(STRING_ELT(content_sxp, 0))));
return SEXP(node);
END_CPP
}
// [[export]]
extern "C" SEXP node_comment_new(SEXP content) {
BEGIN_CPP
XPtrNode node(xmlNewComment(asXmlChar(content)));
return SEXP(node);
END_CPP
}
// [[export]]
extern "C" SEXP node_new_ns(SEXP name, SEXP ns_sxp) {
BEGIN_CPP
XPtrNs ns(ns_sxp);
XPtrNode node(xmlNewNode(ns.checked_get(), asXmlChar(name)));
return SEXP(node);
END_CPP
}
// [[export]]
extern "C" SEXP node_set_namespace_uri(SEXP doc_sxp, SEXP node_sxp, SEXP uri) {
BEGIN_CPP
XPtrDoc doc(doc_sxp);
XPtrNode node(node_sxp);
xmlNsPtr ns = xmlSearchNsByHref(doc.checked_get(), node.checked_get(), asXmlChar(uri));
xmlSetNs(node.checked_get(), ns);
return R_NilValue;
END_CPP
}
// [[export]]
extern "C" SEXP node_set_namespace_prefix(SEXP doc_sxp, SEXP node_sxp, SEXP prefix_sxp) {
BEGIN_CPP
XPtrDoc doc(doc_sxp);
XPtrNode node(node_sxp);
xmlNsPtr ns = NULL;
if (Rf_xlength(STRING_ELT(prefix_sxp, 0)) == 0) {
ns = xmlSearchNs(doc.checked_get(), node.checked_get(), NULL);
} else {
ns = xmlSearchNs(doc.checked_get(), node.checked_get(), asXmlChar(prefix_sxp));
}
xmlSetNs(node.checked_get(), ns);
return R_NilValue;
END_CPP
}
// [[export]]
extern "C" SEXP node_new_dtd(SEXP doc_sxp, SEXP name_sxp, SEXP eid_sxp, SEXP sid_sxp) {
BEGIN_CPP
XPtrDoc doc(doc_sxp);
std::string name(CHAR(STRING_ELT(name_sxp, 0)));
std::string eid(CHAR(STRING_ELT(eid_sxp, 0)));
std::string sid(CHAR(STRING_ELT(sid_sxp, 0)));
xmlDtdPtr dtd = xmlNewDtd(doc, name == "" ? NULL : asXmlChar(name), eid == "" ? NULL : asXmlChar(eid), sid == "" ? NULL : asXmlChar(sid));
xmlAddChild(reinterpret_cast<xmlNodePtr>(doc.checked_get()), reinterpret_cast<xmlNodePtr>(dtd));
return R_NilValue;
END_CPP
}