1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Requires PHP5, uses built-in DOM extension. |
4
|
|
|
To be used in PHP4 scripts using DOMXML extension: allows PHP4/DOMXML scripts to run on PHP5/DOM. |
5
|
|
|
(Optional: requires PHP5/XSL extension for domxml_xslt functions, PHP>=5.1 for XPath evaluation functions, and PHP>=5.1/libxml for DOMXML error reports) |
6
|
|
|
|
7
|
|
|
Typical use: |
8
|
|
|
{ |
9
|
|
|
if (PHP_VERSION>='5') |
10
|
|
|
require_once('domxml-php4-to-php5.php'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
Version 1.21, 2008-12-05, http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/ |
14
|
|
|
|
15
|
|
|
------------------------------------------------------------------ |
16
|
|
|
Written by Alexandre Alapetite, http://alexandre.alapetite.net/cv/ |
17
|
|
|
|
18
|
|
|
Copyright 2004-2008, GNU Lesser General Public License, |
19
|
|
|
http://www.gnu.org/licenses/lgpl.html |
20
|
|
|
|
21
|
|
|
This program is free software: you can redistribute it and/or modify |
22
|
|
|
it under the terms of the GNU Lesser General Public License as published by |
23
|
|
|
the Free Software Foundation, either version 3 of the License, or |
24
|
|
|
(at your option) any later version. |
25
|
|
|
This program is distributed in the hope that it will be useful, |
26
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
27
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28
|
|
|
GNU Lesser General Public License for more details. |
29
|
|
|
You should have received a copy of the GNU Lesser General Public License |
30
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/lgpl.html> |
31
|
|
|
|
32
|
|
|
== Rights and obligations == |
33
|
|
|
- Attribution: You must give the original author credit. |
34
|
|
|
- Share Alike: If you alter or transform this library, |
35
|
|
|
you may distribute the resulting library only under the same license GNU/LGPL. |
36
|
|
|
- In case of jurisdiction dispute, the French law is authoritative. |
37
|
|
|
- Any of these conditions can be waived if you get permission from Alexandre Alapetite. |
38
|
|
|
- Not required, but please send to Alexandre Alapetite the modifications you make, |
39
|
|
|
in order to improve this file for the benefit of everybody. |
40
|
|
|
|
41
|
|
|
If you want to distribute this code, please do it as a link to: |
42
|
|
|
http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/ |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
define('DOMXML_LOAD_PARSING', 0); |
46
|
|
|
define('DOMXML_LOAD_VALIDATING', 1); |
47
|
|
|
define('DOMXML_LOAD_RECOVERING', 2); |
48
|
|
|
define('DOMXML_LOAD_SUBSTITUTE_ENTITIES', 4); |
49
|
|
|
//define('DOMXML_LOAD_COMPLETE_ATTRS',8); |
50
|
|
|
define('DOMXML_LOAD_DONT_KEEP_BLANKS', 16); |
51
|
|
|
|
52
|
|
|
function domxml_new_doc($version) |
53
|
|
|
{ |
54
|
|
|
return new php4DOMDocument(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function domxml_new_xmldoc($version) |
58
|
|
|
{ |
59
|
|
|
return new php4DOMDocument(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
function domxml_open_file($filename, $mode = DOMXML_LOAD_PARSING, &$error = null) |
63
|
|
|
{ |
64
|
|
|
$dom = new php4DOMDocument($mode); |
65
|
|
|
$errorMode = (func_num_args() > 2) && defined('LIBXML_VERSION'); |
66
|
|
|
if ($errorMode) { |
67
|
|
|
libxml_use_internal_errors(true); |
68
|
|
|
} |
69
|
|
|
if (!$dom->myDOMNode->load($filename)) { |
|
|
|
|
70
|
|
|
$dom = null; |
71
|
|
|
} |
72
|
|
|
if ($errorMode) { |
73
|
|
|
$error = array_map('_error_report', libxml_get_errors()); |
74
|
|
|
libxml_clear_errors(); |
75
|
|
|
} |
76
|
|
|
return $dom; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
function domxml_open_mem($str, $mode = DOMXML_LOAD_PARSING, &$error = null) |
80
|
|
|
{ |
81
|
|
|
$dom = new php4DOMDocument($mode); |
82
|
|
|
$errorMode = (func_num_args() > 2) && defined('LIBXML_VERSION'); |
83
|
|
|
if ($errorMode) { |
84
|
|
|
libxml_use_internal_errors(true); |
85
|
|
|
} |
86
|
|
|
if (!$dom->myDOMNode->loadXML($str)) { |
|
|
|
|
87
|
|
|
$dom = null; |
88
|
|
|
} |
89
|
|
|
if ($errorMode) { |
90
|
|
|
$error = array_map('_error_report', libxml_get_errors()); |
91
|
|
|
libxml_clear_errors(); |
92
|
|
|
} |
93
|
|
|
return $dom; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
function html_doc($html_doc, $from_file = false) |
97
|
|
|
{ |
98
|
|
|
$dom = new php4DOMDocument(); |
99
|
|
|
if ($from_file) { |
100
|
|
|
$result = $dom->myDOMNode->loadHTMLFile($html_doc); |
|
|
|
|
101
|
|
|
} else { |
102
|
|
|
$result = $dom->myDOMNode->loadHTML($html_doc); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
return $result ? $dom : null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
function html_doc_file($filename) |
108
|
|
|
{ |
109
|
|
|
return html_doc($filename, true); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
function xmldoc($str) |
113
|
|
|
{ |
114
|
|
|
return domxml_open_mem($str); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
function xmldocfile($filename) |
118
|
|
|
{ |
119
|
|
|
return domxml_open_file($filename); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
function xpath_eval($xpath_context, $eval_str, $contextnode = null) |
123
|
|
|
{ |
124
|
|
|
return $xpath_context->xpath_eval($eval_str, $contextnode); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
function xpath_new_context($dom_document) |
128
|
|
|
{ |
129
|
|
|
return new php4DOMXPath($dom_document); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
function xpath_register_ns($xpath_context, $prefix, $namespaceURI) |
133
|
|
|
{ |
134
|
|
|
return $xpath_context->myDOMXPath->registerNamespace($prefix, $namespaceURI); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
function _entityDecode($text) |
138
|
|
|
{ |
139
|
|
|
return html_entity_decode(strtr($text, array(''' => '\'')), ENT_QUOTES, 'UTF-8'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
function _error_report($error) |
143
|
|
|
{ |
144
|
|
|
return array( |
145
|
|
|
'errormessage' => $error->message, |
146
|
|
|
'nodename' => '', |
147
|
|
|
'line' => $error->line, |
148
|
|
|
'col' => $error->column |
149
|
|
|
) + ($error->file == '' ? array() : array('directory' => dirname($error->file), 'file' => basename($error->file))); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
class php4DOMAttr extends php4DOMNode |
153
|
|
|
{ |
154
|
|
|
function __get($name) |
155
|
|
|
{ |
156
|
|
|
if ($name === 'name') { |
157
|
|
|
return $this->myDOMNode->name; |
158
|
|
|
} else { |
159
|
|
|
return parent::__get($name); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
function name() |
164
|
|
|
{ |
165
|
|
|
return $this->myDOMNode->name; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
function set_content($text) |
169
|
|
|
{ |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
//function set_value($content) {return $this->myDOMNode->value=htmlspecialchars($content,ENT_QUOTES);} |
173
|
|
|
function specified() |
174
|
|
|
{ |
175
|
|
|
return $this->myDOMNode->specified; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
function value() |
179
|
|
|
{ |
180
|
|
|
return $this->myDOMNode->value; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
class php4DOMDocument extends php4DOMNode |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
function php4DOMDocument($mode = DOMXML_LOAD_PARSING) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$this->myDOMNode = new DOMDocument(); |
189
|
|
|
$this->myOwnerDocument = $this; |
190
|
|
|
if ($mode & DOMXML_LOAD_VALIDATING) { |
191
|
|
|
$this->myDOMNode->validateOnParse = true; |
192
|
|
|
} |
193
|
|
|
if ($mode & DOMXML_LOAD_RECOVERING) { |
194
|
|
|
$this->myDOMNode->recover = true; |
195
|
|
|
} |
196
|
|
|
if ($mode & DOMXML_LOAD_SUBSTITUTE_ENTITIES) { |
197
|
|
|
$this->myDOMNode->substituteEntities = true; |
198
|
|
|
} |
199
|
|
|
if ($mode & DOMXML_LOAD_DONT_KEEP_BLANKS) { |
200
|
|
|
$this->myDOMNode->preserveWhiteSpace = false; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
function add_root($name) |
205
|
|
|
{ |
206
|
|
|
if ($this->myDOMNode->hasChildNodes()) { |
207
|
|
|
$this->myDOMNode->removeChild($this->myDOMNode->firstChild); |
208
|
|
|
} |
209
|
|
|
return new php4DOMElement($this->myDOMNode->appendChild($this->myDOMNode->createElement($name)), |
|
|
|
|
210
|
|
|
$this->myOwnerDocument); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
function create_attribute($name, $value) |
214
|
|
|
{ |
215
|
|
|
$myAttr = $this->myDOMNode->createAttribute($name); |
|
|
|
|
216
|
|
|
$myAttr->value = htmlspecialchars($value, ENT_QUOTES); |
217
|
|
|
return new php4DOMAttr($myAttr, $this); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
function create_cdata_section($content) |
221
|
|
|
{ |
222
|
|
|
return new php4DOMNode($this->myDOMNode->createCDATASection($content), $this); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
function create_comment($data) |
226
|
|
|
{ |
227
|
|
|
return new php4DOMNode($this->myDOMNode->createComment($data), $this); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
function create_element($name) |
231
|
|
|
{ |
232
|
|
|
return new php4DOMElement($this->myDOMNode->createElement($name), $this); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
function create_element_ns($uri, $name, $prefix = null) |
236
|
|
|
{ |
237
|
|
|
if ($prefix == null) { |
238
|
|
|
$prefix = $this->myDOMNode->lookupPrefix($uri); |
239
|
|
|
} |
240
|
|
|
if (($prefix == null) && (($this->myDOMNode->documentElement == null) || (!$this->myDOMNode->documentElement->isDefaultNamespace($uri)))) { |
241
|
|
|
$prefix = 'a' . sprintf('%u', crc32($uri)); |
242
|
|
|
} |
243
|
|
|
return new php4DOMElement($this->myDOMNode->createElementNS($uri, |
|
|
|
|
244
|
|
|
$prefix == null ? $name : $prefix . ':' . $name), $this); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
function create_entity_reference($content) |
248
|
|
|
{ |
249
|
|
|
return new php4DOMNode($this->myDOMNode->createEntityReference($content), $this); |
|
|
|
|
250
|
|
|
} //By Walter Ebert 2007-01-22 |
251
|
|
|
|
252
|
|
|
function create_processing_instruction($target, $data = '') |
253
|
|
|
{ |
254
|
|
|
return new php4DomProcessingInstruction($this->myDOMNode->createProcessingInstruction($target, $data), $this); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
function create_text_node($content) |
258
|
|
|
{ |
259
|
|
|
return new php4DOMText($this->myDOMNode->createTextNode($content), $this); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
function document_element() |
263
|
|
|
{ |
264
|
|
|
return parent::_newDOMElement($this->myDOMNode->documentElement, $this); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
function dump_file($filename, $compressionmode = false, $format = false) |
268
|
|
|
{ |
269
|
|
|
$format0 = $this->myDOMNode->formatOutput; |
270
|
|
|
$this->myDOMNode->formatOutput = $format; |
271
|
|
|
$res = $this->myDOMNode->save($filename); |
|
|
|
|
272
|
|
|
$this->myDOMNode->formatOutput = $format0; |
273
|
|
|
return $res; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
function dump_mem($format = false, $encoding = false) |
277
|
|
|
{ |
278
|
|
|
$format0 = $this->myDOMNode->formatOutput; |
279
|
|
|
$this->myDOMNode->formatOutput = $format; |
280
|
|
|
$encoding0 = $this->myDOMNode->encoding; |
281
|
|
|
if ($encoding) { |
282
|
|
|
$this->myDOMNode->encoding = $encoding; |
283
|
|
|
} |
284
|
|
|
$dump = $this->myDOMNode->saveXML(); |
|
|
|
|
285
|
|
|
$this->myDOMNode->formatOutput = $format0; |
286
|
|
|
if ($encoding) { |
287
|
|
|
$this->myDOMNode->encoding = $encoding0 == '' ? 'UTF-8' : $encoding0; |
288
|
|
|
} //UTF-8 is XML default encoding |
289
|
|
|
return $dump; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
function free() |
293
|
|
|
{ |
294
|
|
|
if ($this->myDOMNode->hasChildNodes()) { |
295
|
|
|
$this->myDOMNode->removeChild($this->myDOMNode->firstChild); |
296
|
|
|
} |
297
|
|
|
$this->myDOMNode = null; |
298
|
|
|
$this->myOwnerDocument = null; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
function get_element_by_id($id) |
302
|
|
|
{ |
303
|
|
|
return parent::_newDOMElement($this->myDOMNode->getElementById($id), $this); |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
function get_elements_by_tagname($name) |
307
|
|
|
{ |
308
|
|
|
$myDOMNodeList = $this->myDOMNode->getElementsByTagName($name); |
309
|
|
|
$nodeSet = array(); |
310
|
|
|
$i = 0; |
311
|
|
|
if (isset($myDOMNodeList)) { |
312
|
|
|
while ($node = $myDOMNodeList->item($i++)) { |
313
|
|
|
$nodeSet[] = new php4DOMElement($node, $this); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
return $nodeSet; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
function html_dump_mem() |
320
|
|
|
{ |
321
|
|
|
return $this->myDOMNode->saveHTML(); |
|
|
|
|
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
function root() |
325
|
|
|
{ |
326
|
|
|
return parent::_newDOMElement($this->myDOMNode->documentElement, $this); |
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
function xinclude() |
330
|
|
|
{ |
331
|
|
|
return $this->myDOMNode->xinclude(); |
|
|
|
|
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
function xpath_new_context() |
335
|
|
|
{ |
336
|
|
|
return new php4DOMXPath($this); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
class php4DOMElement extends php4DOMNode |
|
|
|
|
341
|
|
|
{ |
342
|
|
|
function add_namespace($uri, $prefix) |
343
|
|
|
{ |
344
|
|
|
if ($this->myDOMNode->hasAttributeNS('http://www.w3.org/2000/xmlns/', $prefix)) { |
|
|
|
|
345
|
|
|
return false; |
346
|
|
|
} else { |
347
|
|
|
$this->myDOMNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $prefix, |
|
|
|
|
348
|
|
|
$uri); //By Daniel Walker 2006-09-08 |
349
|
|
|
return true; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
function get_attribute($name) |
354
|
|
|
{ |
355
|
|
|
return $this->myDOMNode->getAttribute($name); |
|
|
|
|
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
function get_attribute_node($name) |
359
|
|
|
{ |
360
|
|
|
return parent::_newDOMElement($this->myDOMNode->getAttributeNode($name), $this->myOwnerDocument); |
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
View Code Duplication |
function get_elements_by_tagname($name) |
364
|
|
|
{ |
365
|
|
|
$myDOMNodeList = $this->myDOMNode->getElementsByTagName($name); |
366
|
|
|
$nodeSet = array(); |
367
|
|
|
$i = 0; |
368
|
|
|
if (isset($myDOMNodeList)) { |
369
|
|
|
while ($node = $myDOMNodeList->item($i++)) { |
370
|
|
|
$nodeSet[] = new php4DOMElement($node, $this->myOwnerDocument); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
return $nodeSet; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
function has_attribute($name) |
377
|
|
|
{ |
378
|
|
|
return $this->myDOMNode->hasAttribute($name); |
|
|
|
|
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
function remove_attribute($name) |
382
|
|
|
{ |
383
|
|
|
return $this->myDOMNode->removeAttribute($name); |
|
|
|
|
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
function set_attribute($name, $value) |
387
|
|
|
{ |
388
|
|
|
//return $this->myDOMNode->setAttribute($name,$value); //Does not return a DomAttr |
389
|
|
|
$myAttr = $this->myDOMNode->ownerDocument->createAttribute($name); |
390
|
|
|
$myAttr->value = htmlspecialchars($value, ENT_QUOTES); //Entity problem reported by AL-DesignWorks 2007-09-07 |
391
|
|
|
$this->myDOMNode->setAttributeNode($myAttr); |
392
|
|
|
return new php4DOMAttr($myAttr, $this->myOwnerDocument); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/*function set_attribute_node($attr) |
396
|
|
|
{ |
397
|
|
|
$this->myDOMNode->setAttributeNode($this->_importNode($attr)); |
398
|
|
|
return $attr; |
399
|
|
|
}*/ |
400
|
|
|
function set_name($name) |
401
|
|
|
{ |
402
|
|
|
if ($this->myDOMNode->prefix == '') { |
403
|
|
|
$newNode = $this->myDOMNode->ownerDocument->createElement($name); |
404
|
|
|
} else { |
405
|
|
|
$newNode = $this->myDOMNode->ownerDocument->createElementNS($this->myDOMNode->namespaceURI, |
406
|
|
|
$this->myDOMNode->prefix . ':' . $name); |
407
|
|
|
} |
408
|
|
|
$myDOMNodeList = $this->myDOMNode->attributes; |
409
|
|
|
$i = 0; |
410
|
|
|
if (isset($myDOMNodeList)) { |
411
|
|
|
while ($node = $myDOMNodeList->item($i++)) { |
412
|
|
|
if ($node->namespaceURI == '') { |
413
|
|
|
$newNode->setAttribute($node->name, $node->value); |
|
|
|
|
414
|
|
|
} else { |
415
|
|
|
$newNode->setAttributeNS($node->namespaceURI, $node->nodeName, $node->value); |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
$myDOMNodeList = $this->myDOMNode->childNodes; |
420
|
|
|
if (isset($myDOMNodeList)) { |
421
|
|
|
while ($node = $myDOMNodeList->item(0)) { |
422
|
|
|
$newNode->appendChild($node); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
$this->myDOMNode->parentNode->replaceChild($newNode, $this->myDOMNode); |
426
|
|
|
$this->myDOMNode = $newNode; |
427
|
|
|
return true; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
function tagname() |
431
|
|
|
{ |
432
|
|
|
return $this->tagname; |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
class php4DOMNode |
|
|
|
|
437
|
|
|
{ |
438
|
|
|
public $myDOMNode; |
439
|
|
|
public $myOwnerDocument; |
440
|
|
|
|
441
|
|
|
function php4DOMNode($aDomNode, $aOwnerDocument) |
|
|
|
|
442
|
|
|
{ |
443
|
|
|
$this->myDOMNode = $aDomNode; |
444
|
|
|
$this->myOwnerDocument = $aOwnerDocument; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
function __get($name) |
448
|
|
|
{ |
449
|
|
|
switch ($name) { |
450
|
|
|
case 'type': |
451
|
|
|
return $this->myDOMNode->nodeType; |
452
|
|
|
case 'tagname': |
453
|
|
|
return ($this->myDOMNode->nodeType === XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->tagName; //Avoid namespace prefix for DOMElement |
|
|
|
|
454
|
|
|
case 'content': |
455
|
|
|
return $this->myDOMNode->textContent; |
456
|
|
|
case 'value': |
457
|
|
|
return $this->myDOMNode->value; |
|
|
|
|
458
|
|
|
default: |
459
|
|
|
$myErrors = debug_backtrace(); |
460
|
|
|
trigger_error('Undefined property: ' . get_class($this) . '::$' . $name . ' [' . $myErrors[0]['file'] . ':' . $myErrors[0]['line'] . ']', |
461
|
|
|
E_USER_NOTICE); |
462
|
|
|
return false; |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
function add_child($newnode) |
467
|
|
|
{ |
468
|
|
|
return append_child($newnode); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
function add_namespace($uri, $prefix) |
472
|
|
|
{ |
473
|
|
|
return false; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
function append_child($newnode) |
477
|
|
|
{ |
478
|
|
|
return self::_newDOMElement($this->myDOMNode->appendChild($this->_importNode($newnode)), |
479
|
|
|
$this->myOwnerDocument); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
function append_sibling($newnode) |
483
|
|
|
{ |
484
|
|
|
return self::_newDOMElement($this->myDOMNode->parentNode->appendChild($this->_importNode($newnode)), |
485
|
|
|
$this->myOwnerDocument); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
function attributes() |
489
|
|
|
{ |
490
|
|
|
$myDOMNodeList = $this->myDOMNode->attributes; |
491
|
|
|
if (!(isset($myDOMNodeList) && $this->myDOMNode->hasAttributes())) { |
492
|
|
|
return null; |
493
|
|
|
} |
494
|
|
|
$nodeSet = array(); |
495
|
|
|
$i = 0; |
496
|
|
|
while ($node = $myDOMNodeList->item($i++)) { |
497
|
|
|
$nodeSet[] = new php4DOMAttr($node, $this->myOwnerDocument); |
498
|
|
|
} |
499
|
|
|
return $nodeSet; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
View Code Duplication |
function child_nodes() |
503
|
|
|
{ |
504
|
|
|
$myDOMNodeList = $this->myDOMNode->childNodes; |
505
|
|
|
$nodeSet = array(); |
506
|
|
|
$i = 0; |
507
|
|
|
if (isset($myDOMNodeList)) { |
508
|
|
|
while ($node = $myDOMNodeList->item($i++)) { |
509
|
|
|
$nodeSet[] = self::_newDOMElement($node, $this->myOwnerDocument); |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
return $nodeSet; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
function children() |
516
|
|
|
{ |
517
|
|
|
return $this->child_nodes(); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
function clone_node($deep = false) |
521
|
|
|
{ |
522
|
|
|
return self::_newDOMElement($this->myDOMNode->cloneNode($deep), $this->myOwnerDocument); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
//dump_node($node) should only be called on php4DOMDocument |
526
|
|
|
function dump_node($node = null) |
527
|
|
|
{ |
528
|
|
|
return $node == null ? $this->myOwnerDocument->myDOMNode->saveXML($this->myDOMNode) : $this->myOwnerDocument->myDOMNode->saveXML($node->myDOMNode); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
function first_child() |
532
|
|
|
{ |
533
|
|
|
return self::_newDOMElement($this->myDOMNode->firstChild, $this->myOwnerDocument); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
function get_content() |
537
|
|
|
{ |
538
|
|
|
return $this->myDOMNode->textContent; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
function has_attributes() |
542
|
|
|
{ |
543
|
|
|
return $this->myDOMNode->hasAttributes(); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
function has_child_nodes() |
547
|
|
|
{ |
548
|
|
|
return $this->myDOMNode->hasChildNodes(); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
function insert_before($newnode, $refnode) |
552
|
|
|
{ |
553
|
|
|
return self::_newDOMElement($this->myDOMNode->insertBefore($this->_importNode($newnode), |
554
|
|
|
$refnode == null ? null : $refnode->myDOMNode), $this->myOwnerDocument); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
function is_blank_node() |
558
|
|
|
{ |
559
|
|
|
return ($this->myDOMNode->nodeType === XML_TEXT_NODE) && preg_match('%^\s*$%', $this->myDOMNode->nodeValue); |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
function last_child() |
563
|
|
|
{ |
564
|
|
|
return self::_newDOMElement($this->myDOMNode->lastChild, $this->myOwnerDocument); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
function new_child($name, $content) |
568
|
|
|
{ |
569
|
|
|
$mySubNode = $this->myDOMNode->ownerDocument->createElement($name); |
570
|
|
|
$mySubNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($content))); |
571
|
|
|
$this->myDOMNode->appendChild($mySubNode); |
572
|
|
|
return new php4DOMElement($mySubNode, $this->myOwnerDocument); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
function next_sibling() |
576
|
|
|
{ |
577
|
|
|
return self::_newDOMElement($this->myDOMNode->nextSibling, $this->myOwnerDocument); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
function node_name() |
581
|
|
|
{ |
582
|
|
|
return ($this->myDOMNode->nodeType === XML_ELEMENT_NODE) ? $this->myDOMNode->localName : $this->myDOMNode->nodeName; |
583
|
|
|
} //Avoid namespace prefix for DOMElement |
584
|
|
|
|
585
|
|
|
function node_type() |
586
|
|
|
{ |
587
|
|
|
return $this->myDOMNode->nodeType; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
function node_value() |
591
|
|
|
{ |
592
|
|
|
return $this->myDOMNode->nodeValue; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
function owner_document() |
596
|
|
|
{ |
597
|
|
|
return $this->myOwnerDocument; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
function parent_node() |
601
|
|
|
{ |
602
|
|
|
return self::_newDOMElement($this->myDOMNode->parentNode, $this->myOwnerDocument); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
function prefix() |
606
|
|
|
{ |
607
|
|
|
return $this->myDOMNode->prefix; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
function previous_sibling() |
611
|
|
|
{ |
612
|
|
|
return self::_newDOMElement($this->myDOMNode->previousSibling, $this->myOwnerDocument); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
function remove_child($oldchild) |
616
|
|
|
{ |
617
|
|
|
return self::_newDOMElement($this->myDOMNode->removeChild($oldchild->myDOMNode), $this->myOwnerDocument); |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
function replace_child($newnode, $oldnode) |
621
|
|
|
{ |
622
|
|
|
return self::_newDOMElement($this->myDOMNode->replaceChild($this->_importNode($newnode), $oldnode->myDOMNode), |
623
|
|
|
$this->myOwnerDocument); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
function replace_node($newnode) |
627
|
|
|
{ |
628
|
|
|
return self::_newDOMElement($this->myDOMNode->parentNode->replaceChild($this->_importNode($newnode), |
629
|
|
|
$this->myDOMNode), $this->myOwnerDocument); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
function set_content($text) |
633
|
|
|
{ |
634
|
|
|
return $this->myDOMNode->appendChild($this->myDOMNode->ownerDocument->createTextNode(_entityDecode($text))); |
635
|
|
|
} //Entity problem reported by AL-DesignWorks 2007-09-07 |
636
|
|
|
|
637
|
|
|
//function set_name($name) {return $this->myOwnerDocument->renameNode($this->myDOMNode,$this->myDOMNode->namespaceURI,$name);} |
638
|
|
|
function set_namespace($uri, $prefix = null) |
639
|
|
|
{//Contributions by Daniel Walker 2006-09-08 |
640
|
|
|
$nsprefix = $this->myDOMNode->lookupPrefix($uri); |
641
|
|
|
if ($nsprefix == null) { |
642
|
|
|
$nsprefix = $prefix == null ? $nsprefix = 'a' . sprintf('%u', crc32($uri)) : $prefix; |
643
|
|
|
if ($this->myDOMNode->nodeType === XML_ATTRIBUTE_NODE) { |
644
|
|
|
if (($prefix != null) && $this->myDOMNode->ownerElement->hasAttributeNS('http://www.w3.org/2000/xmlns/', |
645
|
|
|
$nsprefix) && |
646
|
|
|
($this->myDOMNode->ownerElement->getAttributeNS('http://www.w3.org/2000/xmlns/', $nsprefix) != $uri) |
647
|
|
|
) {//Remove namespace |
648
|
|
|
$parent = $this->myDOMNode->ownerElement; |
|
|
|
|
649
|
|
|
$parent->removeAttributeNode($this->myDOMNode); |
650
|
|
|
$parent->setAttribute($this->myDOMNode->localName, $this->myDOMNode->nodeValue); |
651
|
|
|
$this->myDOMNode = $parent->getAttributeNode($this->myDOMNode->localName); |
652
|
|
|
return; |
653
|
|
|
} |
654
|
|
|
$this->myDOMNode->ownerElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $nsprefix, |
655
|
|
|
$uri); |
656
|
|
|
} |
657
|
|
|
} |
658
|
|
|
if ($this->myDOMNode->nodeType === XML_ATTRIBUTE_NODE) { |
659
|
|
|
$parent = $this->myDOMNode->ownerElement; |
660
|
|
|
$parent->removeAttributeNode($this->myDOMNode); |
661
|
|
|
$parent->setAttributeNS($uri, $nsprefix . ':' . $this->myDOMNode->localName, $this->myDOMNode->nodeValue); |
662
|
|
|
$this->myDOMNode = $parent->getAttributeNodeNS($uri, $this->myDOMNode->localName); |
663
|
|
|
} elseif ($this->myDOMNode->nodeType === XML_ELEMENT_NODE) { |
664
|
|
|
$NewNode = $this->myDOMNode->ownerDocument->createElementNS($uri, |
665
|
|
|
$nsprefix . ':' . $this->myDOMNode->localName); |
666
|
|
|
foreach ($this->myDOMNode->attributes as $n) { |
667
|
|
|
$NewNode->appendChild($n->cloneNode(true)); |
668
|
|
|
} |
669
|
|
|
foreach ($this->myDOMNode->childNodes as $n) { |
670
|
|
|
$NewNode->appendChild($n->cloneNode(true)); |
671
|
|
|
} |
672
|
|
|
$xpath = new DOMXPath($this->myDOMNode->ownerDocument); |
673
|
|
|
$myDOMNodeList = $xpath->query('namespace::*[name()!="xml"]', $this->myDOMNode); //Add old namespaces |
674
|
|
|
foreach ($myDOMNodeList as $n) { |
675
|
|
|
$NewNode->setAttributeNS('http://www.w3.org/2000/xmlns/', $n->nodeName, $n->nodeValue); |
676
|
|
|
} |
677
|
|
|
$this->myDOMNode->parentNode->replaceChild($NewNode, $this->myDOMNode); |
678
|
|
|
$this->myDOMNode = $NewNode; |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
function unlink_node() |
683
|
|
|
{ |
684
|
|
|
if ($this->myDOMNode->parentNode != null) { |
685
|
|
|
if ($this->myDOMNode->nodeType === XML_ATTRIBUTE_NODE) { |
686
|
|
|
$this->myDOMNode->parentNode->removeAttributeNode($this->myDOMNode); |
687
|
|
|
} else { |
688
|
|
|
$this->myDOMNode->parentNode->removeChild($this->myDOMNode); |
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
protected function _importNode($newnode) |
694
|
|
|
{ |
695
|
|
|
return $this->myOwnerDocument === $newnode->myOwnerDocument ? $newnode->myDOMNode : $this->myOwnerDocument->myDOMNode->importNode($newnode->myDOMNode, |
696
|
|
|
true); |
697
|
|
|
} //To import DOMNode from another DOMDocument |
698
|
|
|
|
699
|
|
|
static function _newDOMElement($aDOMNode, $aOwnerDocument) |
700
|
|
|
{//Check the PHP5 DOMNode before creating a new associated PHP4 DOMNode wrapper |
701
|
|
|
if ($aDOMNode == null) { |
702
|
|
|
return null; |
703
|
|
|
} |
704
|
|
|
switch ($aDOMNode->nodeType) { |
705
|
|
|
case XML_ELEMENT_NODE: |
706
|
|
|
return new php4DOMElement($aDOMNode, $aOwnerDocument); |
707
|
|
|
case XML_TEXT_NODE: |
708
|
|
|
return new php4DOMText($aDOMNode, $aOwnerDocument); |
709
|
|
|
case XML_ATTRIBUTE_NODE: |
710
|
|
|
return new php4DOMAttr($aDOMNode, $aOwnerDocument); |
711
|
|
|
case XML_PI_NODE: |
712
|
|
|
return new php4DomProcessingInstruction($aDOMNode, $aOwnerDocument); |
713
|
|
|
default: |
714
|
|
|
return new php4DOMNode($aDOMNode, $aOwnerDocument); |
715
|
|
|
} |
716
|
|
|
} |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
class php4DomProcessingInstruction extends php4DOMNode |
|
|
|
|
720
|
|
|
{ |
721
|
|
|
function data() |
722
|
|
|
{ |
723
|
|
|
return $this->myDOMNode->data; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
function target() |
727
|
|
|
{ |
728
|
|
|
return $this->myDOMNode->target; |
729
|
|
|
} |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
class php4DOMText extends php4DOMNode |
|
|
|
|
733
|
|
|
{ |
734
|
|
|
function __get($name) |
735
|
|
|
{ |
736
|
|
|
if ($name === 'tagname') { |
737
|
|
|
return '#text'; |
738
|
|
|
} else { |
739
|
|
|
return parent::__get($name); |
740
|
|
|
} |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
function tagname() |
744
|
|
|
{ |
745
|
|
|
return '#text'; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
function set_content($text) |
749
|
|
|
{ |
750
|
|
|
$this->myDOMNode->nodeValue = $text; |
751
|
|
|
return true; |
752
|
|
|
} |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
if (!defined('XPATH_NODESET')) { |
756
|
|
|
define('XPATH_UNDEFINED', 0); |
757
|
|
|
define('XPATH_NODESET', 1); |
758
|
|
|
define('XPATH_BOOLEAN', 2); |
759
|
|
|
define('XPATH_NUMBER', 3); |
760
|
|
|
define('XPATH_STRING', 4); |
761
|
|
|
/*define('XPATH_POINT',5); |
762
|
|
|
define('XPATH_RANGE',6); |
763
|
|
|
define('XPATH_LOCATIONSET',7); |
764
|
|
|
define('XPATH_USERS',8); |
765
|
|
|
define('XPATH_XSLT_TREE',9);*/ |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
class php4DOMNodelist |
|
|
|
|
769
|
|
|
{ |
770
|
|
|
private $myDOMNodelist; |
771
|
|
|
public $nodeset; |
772
|
|
|
public $type = XPATH_UNDEFINED; |
773
|
|
|
public $value; |
774
|
|
|
|
775
|
|
|
function php4DOMNodelist($aDOMNodelist, $aOwnerDocument) |
|
|
|
|
776
|
|
|
{ |
777
|
|
|
if (!isset($aDOMNodelist)) { |
778
|
|
|
return; |
779
|
|
|
} elseif (is_object($aDOMNodelist) || is_array($aDOMNodelist)) { |
780
|
|
|
if ($aDOMNodelist->length > 0) { |
781
|
|
|
$this->myDOMNodelist = $aDOMNodelist; |
782
|
|
|
$this->nodeset = array(); |
783
|
|
|
$this->type = XPATH_NODESET; |
784
|
|
|
$i = 0; |
785
|
|
|
while ($node = $this->myDOMNodelist->item($i++)) { |
786
|
|
|
$this->nodeset[] = php4DOMNode::_newDOMElement($node, $aOwnerDocument); |
787
|
|
|
} |
788
|
|
|
} |
789
|
|
|
} elseif (is_int($aDOMNodelist) || is_float($aDOMNodelist)) { |
790
|
|
|
$this->type = XPATH_NUMBER; |
791
|
|
|
$this->value = $aDOMNodelist; |
792
|
|
|
} elseif (is_bool($aDOMNodelist)) { |
793
|
|
|
$this->type = XPATH_BOOLEAN; |
794
|
|
|
$this->value = $aDOMNodelist; |
795
|
|
|
} elseif (is_string($aDOMNodelist)) { |
796
|
|
|
$this->type = XPATH_STRING; |
797
|
|
|
$this->value = $aDOMNodelist; |
798
|
|
|
} |
799
|
|
|
} |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
class php4DOMXPath |
|
|
|
|
803
|
|
|
{ |
804
|
|
|
public $myDOMXPath; |
805
|
|
|
private $myOwnerDocument; |
806
|
|
|
|
807
|
|
|
function php4DOMXPath($dom_document) |
|
|
|
|
808
|
|
|
{ |
809
|
|
|
//TODO: If $dom_document is a DomElement, make that default $contextnode and modify XPath. Ex: '/test' |
810
|
|
|
$this->myOwnerDocument = $dom_document->myOwnerDocument; |
811
|
|
|
$this->myDOMXPath = new DOMXPath($this->myOwnerDocument->myDOMNode); |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
function xpath_eval($eval_str, $contextnode = null) |
815
|
|
|
{ |
816
|
|
|
if (method_exists($this->myDOMXPath, 'evaluate')) { |
817
|
|
|
$xp = isset($contextnode) ? $this->myDOMXPath->evaluate($eval_str, |
818
|
|
|
$contextnode->myDOMNode) : $this->myDOMXPath->evaluate($eval_str); |
819
|
|
|
} else { |
820
|
|
|
$xp = isset($contextnode) ? $this->myDOMXPath->query($eval_str, |
821
|
|
|
$contextnode->myDOMNode) : $this->myDOMXPath->query($eval_str); |
822
|
|
|
} |
823
|
|
|
$xp = new php4DOMNodelist($xp, $this->myOwnerDocument); |
824
|
|
|
return ($xp->type === XPATH_UNDEFINED) ? false : $xp; |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
function xpath_register_ns($prefix, $namespaceURI) |
828
|
|
|
{ |
829
|
|
|
return $this->myDOMXPath->registerNamespace($prefix, $namespaceURI); |
830
|
|
|
} |
831
|
|
|
} |
832
|
|
|
|
833
|
|
|
if (extension_loaded('xsl')) {//See also: http://alexandre.alapetite.net/doc-alex/xslt-php4-php5/ |
834
|
|
|
function domxml_xslt_stylesheet($xslstring) |
835
|
|
|
{ |
836
|
|
|
return new php4DomXsltStylesheet(DOMDocument::loadXML($xslstring)); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
function domxml_xslt_stylesheet_doc($dom_document) |
840
|
|
|
{ |
841
|
|
|
return new php4DomXsltStylesheet($dom_document); |
842
|
|
|
} |
843
|
|
|
|
844
|
|
|
function domxml_xslt_stylesheet_file($xslfile) |
845
|
|
|
{ |
846
|
|
|
return new php4DomXsltStylesheet(DOMDocument::load($xslfile)); |
847
|
|
|
} |
848
|
|
|
|
849
|
|
|
class php4DomXsltStylesheet |
|
|
|
|
850
|
|
|
{ |
851
|
|
|
private $myxsltProcessor; |
852
|
|
|
|
853
|
|
|
function php4DomXsltStylesheet($dom_document) |
|
|
|
|
854
|
|
|
{ |
855
|
|
|
$this->myxsltProcessor = new xsltProcessor(); |
856
|
|
|
$this->myxsltProcessor->importStyleSheet($dom_document); |
857
|
|
|
} |
858
|
|
|
|
859
|
|
|
function process($dom_document, $xslt_parameters = array(), $param_is_xpath = false) |
860
|
|
|
{ |
861
|
|
|
foreach ($xslt_parameters as $param => $value) { |
862
|
|
|
$this->myxsltProcessor->setParameter('', $param, $value); |
863
|
|
|
} |
864
|
|
|
$myphp4DOMDocument = new php4DOMDocument(); |
865
|
|
|
$myphp4DOMDocument->myDOMNode = $this->myxsltProcessor->transformToDoc($dom_document->myDOMNode); |
866
|
|
|
return $myphp4DOMDocument; |
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
function result_dump_file($dom_document, $filename) |
870
|
|
|
{ |
871
|
|
|
$html = $dom_document->myDOMNode->saveHTML(); |
872
|
|
|
file_put_contents($filename, $html); |
873
|
|
|
return $html; |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
function result_dump_mem($dom_document) |
877
|
|
|
{ |
878
|
|
|
return $dom_document->myDOMNode->saveHTML(); |
879
|
|
|
} |
880
|
|
|
} |
881
|
|
|
} |
882
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: