Open main menu

Wikibooks β

XQuery/Getting URL Parameters

< XQuery



You want to create an XQuery that takes a parameter from the calling URL.


The format of a calling URL that uses the HTTP Get or POST command is:


Where param1 is the first parameter with a value of 123 and param2 is the second parameter with a value of 456.

Note that question mark is used to start the parameters and the ampersand is used to separate parameters. Remember to include "amp;" following the &.

xquery version "1.0";

let $param1:= request:get-parameter('param1', '')
let $param2:= request:get-parameter('param2', "")

   if ($param2 = '0') then (
       <message>param2 is empty</message>
   ) else (
      <message>default message</message>

Checking Data TypesEdit

Additionally you can check the data types using the XML Schema data types and the castable as operator.

xquery version "1.0";
declare namespace request="";
declare namespace xs="";

let $myint := request:get-parameter("myint",0)
let $myint := if ($myint castable as  xs:integer)
              then xs:integer($myint)
              else 0
let $mydecimal := request:get-parameter("mydecimal", 0.0)
let $mydecimal := if ($mydecimal castable as  xs:decimal)
                  then xs:decimal($mydecimal)
                  else 0.0

   <message>Got  myint: {$myint} and  mydecimal: {$mydecimal} </message>

Script to echo all URL parametersEdit


xquery version "1.0";

(: echo a list of all the URL parameters  :)
let $parameters :=  request:get-parameter-names()

   {for $parameter in $parameters
      <value>{request:get-parameter($parameter, '')}</value>

Here are the results of sending the parameters "a=1&b=2" to this XQuery:

    <parameters>b a</parameters>

Adding a Debug ParameterEdit

It is very common that you want to conditionally turn on part of a transform to get additional information during the debugging process.

let $debug := xs:boolean(request:get-parameter('debug', ''))