This repository has been archived by the owner on Jun 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathxmlmerge.php
90 lines (81 loc) · 2.08 KB
/
xmlmerge.php
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
<?php
/* MERGE ELEMENTS */
$xml=simplexml_load_file("segments.xml");
$data_elm=simplexml_load_file("data_elements.xml");
$compdata_elm=simplexml_load_file("composite_data_elements.xml");
foreach ($xml->segment as $seg)
{
foreach($seg->children() as $child)
{
if($child->getName()=="composite_data_element")
{
$result = $compdata_elm->xpath('*[@id="'.$child["id"].'"]');
//var_dump($result[0]);
foreach ($result[0]->attributes() as $k => $v)
{
if($k=="id")continue;
$child->addAttribute($k,$v);
}
foreach ($result[0]->children() as $orphan)
{
xml_adopt($child,$orphan);
}
}
}
}
foreach ($xml->segment as $seg)
{
foreach($seg->children() as $child)
{
if($child->getName()=="data_element")
{
$result = $data_elm->xpath('*[@id="'.$child["id"].'"]');
//var_dump($result[0]);
foreach ($result[0]->attributes() as $k => $v)
{
if($k=="id")continue;
$child->addAttribute($k,$v);
}
foreach ($result[0]->children() as $orphan)
{
xml_adopt($child,$orphan);
}
}
if($child->getName()=="composite_data_element")
{
foreach($child->children() as $child2)
{
if($child2->getName()=="data_element")
{
$result = $data_elm->xpath('*[@id="'.$child2["id"].'"]');
//var_dump($result[0]);
foreach ($result[0]->attributes() as $k => $v)
{
if($k=="id")continue;
$child2->addAttribute($k,$v);
}
}
}
}
}
}
//var_dump($child);
$xml->asXml("_segments.xml");
//var_dump($data_elm->data_elements); data_elements/data_element[@id=9019]
function xml_adopt($root, $new, $namespace = null) {
// first add the new node
$node = $root->addChild($new->getName(), (string) $new, $namespace);
// add any attributes for the new node
foreach($new->attributes() as $attr => $value) {
$node->addAttribute($attr, $value);
}
// get all namespaces, include a blank one
$namespaces = array_merge(array(null), $new->getNameSpaces(true));
// add any child nodes, including optional namespace
foreach($namespaces as $space) {
foreach ($new->children($space) as $child) {
xml_adopt($node, $child, $space);
}
}
}
?>