Passing variable from bash to XQuery

657 Views Asked by At

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?

2

There are 2 best solutions below

0
Jens Erat On BEST ANSWER

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)
1
Kit On
#!/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