Search code examples
xmlbashxqueryxqilla

Passing variable from bash to XQuery


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?


Solution

  • 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)