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