Creating an alias for xpath expression in xidel with regex and bash

If you have already used Xidel, you will often need to locate nodes that have a certain class. To do this more easy, I want to create has-class("class") function that serves as an alias for the expression:

contains(concat(" ", normalize-space(@class), " "), " class ").

Example:

$ e-xidel.sh example.com '//article/p//img[has-class("wp-image")]'

e-xidel.sh contains this code:

#!/bin/bash

echo -e “$(tput setaf 2) Checking… $(tput sgr0)”

path=$1
expression=$2

expression = ‘//article/p//img[has-class(“wp-image”)]’

Regex to replace every * has-class(“class”) * by * contains(concat(" ", normalize-space(@class), " "), " class ") *

expression = ‘//article/p//img[contains(concat(" ", normalize-space(@class), " "), " wp-image ")]’

xoutput=$(xidel $path --printed-node-format=html --output-declaration= -e “$expression”)

echo -e “$(tput setaf 1) $xoutput $(tput sgr0)”


#regex #bash #macos

6.05 GEEK