-
Notifications
You must be signed in to change notification settings - Fork 6
/
phxpath
executable file
·62 lines (48 loc) · 1.32 KB
/
phxpath
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
#!/usr/bin/php
<?php
/***
* Original author: Ondrej Brablc
* Location: https://github.com/brablc/tools/blob/master/phxpath
*/
ini_set('display_errors','On');
error_reporting(E_ALL);
if ($argc < 3) {
$err = <<<__USAGE
Usage: phxpath FILE XPATH-EXPRESSION [/ENCLOSING/ELEMENTS]
If /ENCLOSING/ELEMENTS are used, valid XML will be printed.
Otherwise just values.
Please be aware of namespaces. The easiest way is to remove them
from the originating document:
sed -e '2 s/xmlns/ignore/g' -i .orig FILE
Output can be formatted using:
... | xmllint --format -
__USAGE;
fprintf(STDERR, $err);
exit(22);
}
if (!is_file($argv[1])) {
fprintf(STDERR, "File '$argv[1]' not found!\n");
exit(2);
}
$xml = new SimpleXMLElement(file_get_contents($argv[1]));
$result = $xml->xpath($argv[2]);
if (!empty($argv[3])) {
$enclosure = explode('/', $argv[3]);
if ($enclosure[0] == '') {
array_shift($enclosure);
}
for ($i=0; $i<count($enclosure);$i++) {
echo str_repeat(' ',$i) . "<".$enclosure[$i].">\n";
}
foreach ($result as $node) {
echo $node->asXml();
}
for ($i=count($enclosure)-1; $i>=0; $i--) {
echo str_repeat(' ',$i) . "</".$enclosure[$i].">\n";
}
}
else {
while(list( , $node) = each($result)) {
echo $node,"\n";
}
}