To call an xquery stored in a file, you can use xdmp:invoke function.
you need to specify the file name and the parameters to send.
syntax
xdmp:invoke(
filepath as xs:string
[params as item()*]
[options as node()?]
) as item()*
for eg.
Invoking xquery file with parameters
xdmp:invoke("xqueryfilename.xqy", (xs:QName("parameter name1"), "parameter value",xs:QName("parameter name2"), "parameter value”))
Invoking xquery file without parameters
xdmp:invoke("xqueryfilename.xqy")
Invoking xquery file with a URI and namespace
Xquery file at URI http://marklogicsite.com/modules with filename xqueryfilename.xqy
Note – options should always be in xdmp:eval namespace
---------------------------------------------------
xquery version "1.0-ml";
declare namespace exns="example-namespace";
declare variable $exns:docuriparam as xs:string external;
fn:doc($exns:docuriparam)
---------------------------------------------------
It returns the document with the specified path.
---------------------------------------------------
xquery version "1.0-ml";
declare namespace exns="example-namespace";
xdmp:invoke("xqueryfilename.xqy",(xs:QName(“exns:docuriparam”), “modulename/xmldocname.xml”) , <options xmlns=”xdmp:eval”><modules>{xdmp:modules-database()}</modules><root>http://marklogicsite.com/modules</root></options>)
----------------------------------------------------
Here xdmp:modules-database() returns the ID of the modules database.
root is for root path of the modules. To return the current root path use xdmp:modules-root()
Best Practice regarding passing parameters to xquery.
If the xquery requires several parameters to be sent, its better to fragment all the parameters inside a single XML Node which in turn will get passed as a parameter. The xquery then in turn will process the node and xpath to get the parameter as and when required.
For eg. consider the following variables declaration in xquery.
-----------------------------------
xquery version "1.0-ml";
declare variable $employeeName as xs:string external;
declare variable $companyName as xs:string external;
declare variable $departmentName as xs:string external;
declare variable $registered as xs:string external;
declare variable $dateLastAccessed as xs:string external;
-----------------------------------
We can modify the above and use a single parameter to get all the details sought
-----------------------------------
xquery version "1.0-ml";
declare variable $parameters as xs:string external;
-----------------------------------
and the parameter value could be
-----------------------------------
<parameters>
<parameter><employeeName>empname</employeeName></parameter>
<parameter><companyName>compname</companyName></parameter>
<parameter><departmentName>dname</departmentName></parameter>
<parameter><registered>Yes</sortparam></registered>
<parameter><dateLastAccessed>2006-05-05</dateLastAccessed></parameter>
</parameters>
-----------------------------------
In the xquery you can xpath $parameters to get your value
for eg. $parameters//employeeName will get you the value of first parameter.
This makes the code better and passing the parameters will be clean and nice.

No comments:
Post a Comment