-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsd2form_noclass.php
211 lines (183 loc) · 7.36 KB
/
xsd2form_noclass.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
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
<?php
function rus2translit($string)
{
$converter = array(
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e',
'ё' => 'e', 'ж' => 'zh', 'з' => 'z',
'и' => 'i', 'й' => 'y', 'к' => 'k',
'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r',
'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'h', 'ц' => 'c',
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch',
'ь' => '', 'ы' => 'y', 'ъ' => '',
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E',
'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z',
'И' => 'I', 'Й' => 'Y', 'К' => 'K',
'Л' => 'L', 'М' => 'M', 'Н' => 'N',
'О' => 'O', 'П' => 'P', 'Р' => 'R',
'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch',
'Ь' => '', 'Ы' => 'Y', 'Ъ' => '',
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
);
return strtr($string, $converter);
}
function libxml_display_error($error, $ifxslt) {
$return = "<br/>\n";
if (!$ifxslt) {
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "<b>Warning $error->code</b>: ";
break;
case LIBXML_ERR_ERROR:
$return .= "<b>Error $error->code</b>: ";
break;
case LIBXML_ERR_FATAL:
$return .= "<b>Fatal Error $error->code</b>: ";
break;
}
}
if ($ifxslt) {
if (strpos($error->message, 'ERR ') !== false) {
$return .= trim($error->message);
} else {
$return = '';
}
} else {
$return .= trim($error->message);
}
if (!$ifxslt) {
if ($error->file) {
$return .= " in <b>$error->file</b>";
}
$return .= " on line <b>$error->line</b>\n";
}
return $return;
}
function libxml_display_errors($filename, $title, $ifxslt = false) {
$errors = libxml_get_errors();
file_put_contents($filename,'<h1>'.$title.'</h1>', FILE_APPEND);
foreach ($errors as $error) {
file_put_contents($filename, libxml_display_error($error, $ifxslt), FILE_APPEND);
}
libxml_clear_errors();
}
function xml_attribute($object, $attribute) {
if(isset($object[$attribute]))
return (string) $object[$attribute];
}
//Во всех названиях namespase заменяем кириллицу на латиницу
function translit_namespace($filename){
$f = file_get_contents($filename);
preg_match_all("'\"urn:(.*?)\"'si", $f, $match);
foreach($match[0] as $m) {
$newm = rus2translit($m);
$f = str_replace($m, $newm, $f);
}
file_put_contents($filename, $f);
}
function add_xs($filename) {
$sxml = simplexml_load_file($filename);
$namespaces = $sxml->getDocNamespaces();
$xmlscnemans = '';
foreach ($namespaces as $nskey => $nsvalue) {
if ($nsvalue=='http://www.w3.org/2001/XMLSchema'){
$xmlscnemans = $nskey;
}
}
//Если не задан префикс для стандартного namespace, то добавляем его
if (isset($namespaces[''])&&($xmlscnemans=='')) {
$xml = new DOMDocument;
$xml->load($filename);
unset($namespaces['']);
$insertstring = '';
foreach ($namespaces as $nskey => $nsvalue) {
$insertstring .= 'xmlns:' . $nskey . '="' . $nsvalue . '" ';
}
$xsl = new DOMDocument;
$xsl->load('./xsl/add_namespace_xs.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$result = $proc->transformToXML($xml);
// $s = strpos($result, 'xmlns:xs="http://www.w3.org/2001/XMLSchema"');
// $result = substr($result, 0, $s) . $insertstring . substr($result, $s);
file_put_contents($filename, $result);
}
}
function add_root_element($filename) {
$xml = new DOMDocument;
$xml->load($filename);
$xsl = new DOMDocument;
$xsl->load('./xsl/add_root_element.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$result = $proc->transformToXML($xml);
file_put_contents($filename, $result);
}
libxml_use_internal_errors(true);
$all_namespaces = [];
$xsd_files = [];
if(isset($_FILES)) {
//Создаем папку преобразования
$uid = uniqid();
$path = './forms/'.$uid.'/';
mkdir($path, 0777, true);
foreach ($_FILES as $file){
//Перемещаем файл в папку преобразования
$tfilename = rus2translit($file['name']);
move_uploaded_file($file['tmp_name'],$path.$tfilename);
translit_namespace($path.$tfilename);
//Сохраняем namespace - имя файла
$sxml = simplexml_load_file($path.$tfilename);
$all_namespaces[xml_attribute($sxml, 'targetNamespace')] = $tfilename;
$xsd_files[] = $tfilename;
}
file_put_contents($path.'log.html','<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><title>Log file</title></head><body>');
$selected_file = rus2translit($_POST["SelectedFile"]);
$xml = new DOMDocument;
$xml->load($path.$selected_file);
$sxml = simplexml_import_dom($xml);
$namespaces = $sxml->getDocNamespaces();
$targetNamespace = xml_attribute($sxml, 'targetNamespace');
foreach ($namespaces as $nskey=>$nsvalue){
if ($nskey!='') {
if (($nsvalue != $targetNamespace)&&($nsvalue != 'http://www.w3.org/2001/XMLSchema')) {
if (isset($all_namespaces[$nsvalue])) {
$import = $xml->createElement('xs:import');
$import->setAttribute('namespace', $nsvalue);
$import->setAttribute('schemaLocation', $all_namespaces[$nsvalue]);
$first = $xml->documentElement->firstChild;
$new_import = $xml->documentElement->insertBefore($import, $first);
add_xs($path.$all_namespaces[$nsvalue]);
} else {
file_put_contents($path . 'log.html', '<br><b>Не найден файл с пространоством имен ' . $nsvalue . '</b>', FILE_APPEND);
}
}
}
}
$xml->save($path.$selected_file);
add_root_element($path.$selected_file);
// add_xs($path.$selected_file);
libxml_display_errors($path.'log.html', 'LOAD XML LOG');
$xml = new DOMDocument;
$xml->load($path.$selected_file);
$xsl = new DOMDocument;
$xsl->load('./xsd2html2xml/xsd2html2xml.xsl');
// Настройка преобразования
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
libxml_display_errors($path.'log.html', 'LOAD XSL LOG');
$form = $proc->transformToXML($xml);
file_put_contents($path.'form.html', $form);
libxml_display_errors($path.'log.html', 'XSLT LOG', true);
file_put_contents($path.'log.html','</body></html>', FILE_APPEND);
$result['uid'] = $uid;
$result['xsds'] = $xsd_files;
print_r(json_encode($result));
}
?>