bashfor-loopxpathxidel

Xidel: How to process multiple result from one node?


Say, we have a code:

xidel -s https://www.example.com -e '(//span[@class="number"])'

and the output are:

111111
222222
333333

can I do this one below?

for ((n=1;n<=3;n++))
do
   a=$(xidel -s https://www.example.com -e '(//span[@class="number"]) [$n]')
   b=$a+1
   echo $b
done

I expect it to print out 3 edited numbers like this:

111112
222223
333334

it might be a little absurb to download the webpage 3 times, but the reason here is to process each value of the output one by one, using ForLoop.


Solution

  • xidel fully supports XPath/XQuery 3.0 (support for XPath/XQuery 3.1 is in development), so you can use all the features and filters it has to offer.
    I can recommend the following websites:


    Without a "Minimal, Reproducible Example" I'll just put your above mentioned output in a sequence and show you some examples.

    xidel -se 'let $a:=(111111,222222,333333) return $a ! (. + 1)'
    #or
    xidel -se 'for $x in (111111,222222,333333) return $x + 1'
    111112
    222223
    333334
    
    xidel -se 'let $a:=("a abc","a sdf","a wef","a vda","a gdr") return $a ! substring-after(.,"a ")'
    #or
    xidel -se 'let $a:=("a abc","a sdf","a wef","a vda","a gdr") return $a ! replace(.,"a ","")'
    #or
    xidel -se 'for $x in ("a abc","a sdf","a wef","a vda","a gdr") return substring-after($x,"a ")'
    #or
    xidel -se 'for $x in ("a abc","a sdf","a wef","a vda","a gdr") return replace($x,"a ","")'
    abc
    sdf
    wef
    vda
    gdr