3

I have a bash-script, that traverses through multiple directories and parses data from a few XML files. I am using XQilla to execute my XQueries.

echo "---|Reading names|---"
../../xqilla .._name.fcs >> /var/lib/mysql-files/name.txt

And this is the XQuery:

for $fw in doc("./network_objects.xml")/network_objects/network_object
where $fw/interfaces/interfaces/ipaddr
return (data($fw/Name))

How can I pass a variable from the bash-script to XQuery, so that I can search for different items, depending on the cwd, for example?

zx485
  • 28,498
  • 28
  • 50
  • 59

2 Answers2

2

Use xqilla's -v option to pass an external variable (which is a standardized XQuery concept):

-v <name> <value> : Bind the name value pair as an external variable

In XQuery, make sure to declare that variable as external.

For example, call an XQuery script passing the $foo variable with value bar:

xqilla -v foo bar test.xq

And use this variable by declaring it in the script's header:

declare variable $foo external;

concat("Value of $foo is: ", $foo)
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
0
#!/bin/dash

echo "Alpha
Bravo
Charlie
Delta" |
while read name; do
    echo "/network_objects/network_object[Name='$name'][interfaces/interface/ipaddr]/Name/string()" |
        xqilla -i ./network_objects.xml /dev/stdin
done
Kit
  • 79
  • 1
  • 4
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – jmattheis Apr 23 '17 at 18:17