#!/bin/bash shellfu import pretty # # Poor man's xpath tool # # Most good xpath qruery tools won't work on RHEL5; Perl's XML::XPath # is even missing aarch64 in RHEL7 (not sure if this is intentional). # # For this reason this simple script exists; it should only require # libxml2 and its Python bindings. # pxpath() { # # Run XPath query $1 on file $2, register namespaces $3.. # # Usage: # # pxpath [-f FMT] EXPR FILE [NSNAME:NSURI].. # # Run EXPR expression on XML FILE using Python bindings to libxml2. # Optionally, you can specify own namespaces; each has to have name # and reference URI. # # FMT is printf-like format string with single '%s' to which every # match is inserted prior to printing. Default is '%s\n', that is. # each match is terminated with single newline. # # FILE can be '-' (single dash), in which case standard input # is (cached and) parsed. # # If DEBUG environment variable is set to '1', will print match # debug info to standard error output. # local query local file local tmp local es=0 while true; do case $1 in -f) fmt+=("$2"); shift 2 || { __pxpath__usage; return 2; } ;; -*) __pxpath__usage; return 2 ;; *) break ;; esac done query=$1; file=$2; shift 2 test -n "$query" || { __pxpath__usage; return 2; } case $file in "") __pxpath__usage return 2 ;; -) tmp=$(mktemp -t pxpath.stdin.XXXXXXXX) cat > "$tmp" file=$tmp ;; *) test -f "$file" || { warn "no such file, ignoring query: $file" return 1 } esac #shellcheck disable=SC2154 "$_PXPATH__BIN" \ "$query" "$file" "$@"; es=$? test -n "$tmp" && rm "$tmp" return $es } # # code below should not be called # # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # # not even as XPath expression # # # Path to pxpath python script # _PXPATH__BIN=${_PXPATH__BIN:-__SHELLFU_LIBEXEC__/pxpath} __pxpath__usage() { # # Print usage message for function pxpath__$1 # local base="usage: pxpath" mkusage -E "$base [-f FMT] EXPR FILE [NSNAME:NSURI]" } #shellfu module-version=__MKIT_PROJ_VERSION__