1

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.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223

1 Answers1

2

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
Reino
  • 3,203
  • 1
  • 13
  • 21
  • Thanks, but what means `||` ? I know XPath separators `,` and `|`. Maybe a `OR`? – Gilles Quénot Mar 03 '23 at 23:35
  • In a real use case: `xidel -se 'xp:=//div[@data-click-id="background"]/div[@data-adclicklocation="title"]/div/a[@data-click-id="body"]' -e '$xp/@href|$xp/div/h3/text()' "https://www.reddit.com/r/bash"` but I have annoying newlines – Gilles Quénot Mar 03 '23 at 23:36
  • It not works as `xidel -e '//div[@data-click-id="background"]/div[@data-adclicklocation="title"]/div/a[@data-click-id="body"]/@href|//div[@data-click-id="background"]/div[@data-adclicklocation="title"]/div/a[@data-click-id="body"]/div/h3/text()' "https://www.reddit.com/r/bash"` – Gilles Quénot Mar 03 '23 at 23:49
  • 1
    `||` --> [3.6 String Concatenation Expressions](https://www.w3.org/TR/xpath-31/#id-string-concat-expr). – Reino Mar 04 '23 at 11:10
  • I'm not what sure what you're trying to do, but I'd say this is beyond the scope of this question. So, please start a new question. – Reino Mar 04 '23 at 11:12
  • This might help `var=bar xidel --variable=var -se '"foo"||$var'` – potong Aug 13 '23 at 09:29
  • @potong That's how you can use _external_ environmental variables, while Gilles asked for _internal_ variables. I've added a `--variable` example nonetheless. – Reino Aug 13 '23 at 14:25