|
1 #!/usr/bin/perl |
|
2 |
|
3 # Relational pipes |
|
4 # Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
5 # |
|
6 # This program is free software: you can redistribute it and/or modify |
|
7 # it under the terms of the GNU General Public License as published by |
|
8 # the Free Software Foundation, version 3 of the License. |
|
9 # |
|
10 # This program is distributed in the hope that it will be useful, |
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 # GNU General Public License for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
|
18 use strict; |
|
19 use warnings; |
|
20 |
|
21 use XML::LibXML; # documentation: https://metacpan.org/pod/XML::LibXML |
|
22 |
|
23 if (@ARGV == 0) { |
|
24 print "1\n"; |
|
25 if ($ENV{type}) { print "$ENV{type}\n"; } else { print "string\n"; } |
|
26 } else { |
|
27 my $dom = XML::LibXML->new->parse_file($ARGV[0]); |
|
28 my $xpath = XML::LibXML::XPathContext->new($dom); |
|
29 |
|
30 # You can add your favorite XML namespaces here: |
|
31 # $xpath->registerNs('relpipe', 'tag:globalcode.info,2018:relpipe'); |
|
32 # $xpath->registerNs('xhtml', 'http://www.w3.org/1999/xhtml'); |
|
33 # $xpath->registerNs('svg', 'http://www.w3.org/2000/svg'); |
|
34 # $xpath->registerNs('atom', 'http://www.w3.org/2005/Atom'); |
|
35 # $xpath->registerNs('maven', 'http://maven.apache.org/POM/4.0.0'); |
|
36 # |
|
37 # Or set environmental variables: |
|
38 # export xmlns_r='tag:globalcode.info,2018:relpipe' |
|
39 |
|
40 # Load XML namespaces from options: |
|
41 # usage: --option 'env:xmlns_r' 'tag:globalcode.info,2018:relpipe' → r="tag:globalcode.info,2018:relpipe" |
|
42 for my $name (keys %ENV) { |
|
43 if ($name =~ /xmlns_(.*)/) { $xpath->registerNs($1, $ENV{$name}); } |
|
44 } |
|
45 |
|
46 # Execute XPath and concatenate results (usually should be only one): |
|
47 # usage: --option env:xpath '//r:name' |
|
48 for my $value ($xpath->find($ENV{xpath})) { |
|
49 print $value; |
|
50 } |
|
51 } |