======Command xquery======
====Name====
**xquery** evaluate an XQUERY expression and print the result
====Synopsis====
xquery [ [[SerializationOptions serialization options]] ] [options] [-v var value [var value]]
====Description====
Evaluates an XQuery expression
====Options====
||-f,-file xquery-file||read xquery script from file||
||-i,-input context||(depreciated) use context as the source xml document, otherwise stdin if context is an XML expression which is an item then use it directly as the context item.||
||-c,-context context||Use context as a literal context||
||-cf,-context-file file||Use file as the name of an XML file for the context, or "-" for stdin||
||-q query|| use argument as xquery-expr||
||-n|| do not use a source context||
||-v|| if -v is given then the remaining args are treated as name/value pairs and assigned to xquery external variables||
||-nons ||Do not import global namespace declarations from the shell environment||
||-ns prefix=uri|| Add a predeclared namespace declaration||
||-s,-string||convert every node value in the result to a string||
||-b,-bool||Do not output result. Exit with effective boolean value||
||-baseuri uri||Specify the base uri for the xquery||
||-noxmlsh||Do not predeclare the xmlsh namespace for the [[XPathExtension xmlsh:eval]] xpath extension function||
xquery-expr xquery expression
Supports the standard [ [[SerializationOptions serialization options]] ]
If no -q or -f is given then the next argument is treated as the xquery expression/script.
-i is depreciated. It is a convenience option that does the following
- If context is an atomic value then treat as a filename to be loaded as the context item
- Otherwise treat as the literal context for the context item
-c or -cf are suggested which explicitly indicate if to use the expression literally or as a filename
if -b is supplied then no output, instead the return value is 0 if the effective boolean value of the result is true otherwise 1.
if -baseuri is supplied then the arguement is used as a relative or absolute uri. If relative it is relative to the current directory.
If no -baseuri is supplied, then if -f is supplied the absolute path of the script is used as the base uri, otherwise the current directory
is used as the base uri. The base URI is used for all URI resolution in the xquery, including modules and documents.
Not all options are compatible. You cannot read both the query and the context from stdin.
If you supply variables then -q must be supplied.
If the result (or any node of the result if a sequence) is an Attribute, or if the -s option is specified the it is converted to a string.
====Example====
%%(shell)
xls | xquery '//file[1]/@name/string()'
%%
Result
%%
.classpath
%%
Simplified example, you dont need string()
%%(shell)
xls | xquery '//file[1]/@name'
%%
Result
%%
.classpath
%%
Example
%%
xquery -i <[] .
%%
Result
%%
%%
Namespace example using a renamed prefix for a namespace
%%
echo "" | xquery -nons -ns y=foo /y:a
%%
Result
%%
%%
When using large XQuery expressions consider storing the xquery in a file such as
%%
xquery -f query.xquery
%%
Or using the Block Quotes which makes managing the various quote options in XPath and XQuery easier
%%
xquery -n -q <{{
declare variable $A external ;
declare function local:foo( $a )
{
{ concat("foo",$a,'bar') }
};
local:foo( $A/spam[2] )
}}> -v A <[ texttext2 ]>
%%
Result
%%
footext2bar
%%
====Return Value====
Returns 0 if the the xquery expression executed successfully. otherwise 1
----
[[Commands]]
[[CategoryCommands]]