#!/usr/bin/perl
# Relational pipes
# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use XML::LibXML; # documentation: https://metacpan.org/pod/XML::LibXML
if (@ARGV == 0) {
print "1\n";
if ($ENV{type}) { print "$ENV{type}\n"; } else { print "string\n"; }
} else {
my $dom = XML::LibXML->new->parse_file($ARGV[0]);
my $xpath = XML::LibXML::XPathContext->new($dom);
# You can add your favorite XML namespaces here:
# $xpath->registerNs('relpipe', 'tag:globalcode.info,2018:relpipe');
# $xpath->registerNs('xhtml', 'http://www.w3.org/1999/xhtml');
# $xpath->registerNs('svg', 'http://www.w3.org/2000/svg');
# $xpath->registerNs('atom', 'http://www.w3.org/2005/Atom');
# $xpath->registerNs('maven', 'http://maven.apache.org/POM/4.0.0');
#
# Or set environmental variables:
# export xmlns_r='tag:globalcode.info,2018:relpipe'
# Load XML namespaces from options:
# usage: --option 'env:xmlns_r' 'tag:globalcode.info,2018:relpipe' → r="tag:globalcode.info,2018:relpipe"
for my $name (keys %ENV) {
if ($name =~ /xmlns_(.*)/) { $xpath->registerNs($1, $ENV{$name}); }
}
# Execute XPath and concatenate results (usually should be only one):
# usage: --option env:xpath '//r:name'
for my $value ($xpath->find($ENV{xpath})) {
print $value;
}
}