shellxpathxidel

xidel: is it possible to declare a variable for a XPath expression?


Like

xmlstarlet sel --var xp 'xpathExpression' -t -v '$xp' file.xml

is it possible to use internal variables in xidel?

I know I can use shell and "double quotes", but that's not the question.


Solution

  • readme, or xidel --usage:

    Variable assignment:                                         $var := value
    
      adds $var to a set of global variables, which can be created and accessed everywhere.
      (Xidel prints the value of all variables to stdout, unless you use the --extract-exclude option)
    

    So, for instance:

    $ xidel -se 'var:="bar"' -e '"foo"||$var'
    var := bar
    foobar
    
    $ xidel -s --extract-exclude=var -e 'var:="bar"' -e '"foo"||$var'
    foobar
    

    Or of course with the XQuery Let Clause (not global):

    $ xidel -se 'let $var:="bar" return "foo"||$var'
    foobar
    

    xidel --help:

    --variable=<string>    Declares a variable (value taken from environment if not
                           given explicitely) (multiple variables are preliminary)
    

    So, for instance:

    $ xidel -s --variable="var=bar" -e '"foo"||$var'
    $ xidel -s --variable var=bar -e '"foo"||$var'
    foobar