1

I'm trying to query an exist-db with xquery by taking parameters from the URL and building up seach parameters

xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare option exist:serialize "method=xml media-type=text/xml omit-xml-declaration=no indent=yes";

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

let $person :=
    if($param1 = '0') 
    then "'*'"
    else concat('contributions/person/@val="',$param1,'"')

return

<xml>
{
    for $x in subsequence(//foo/bar[$person],1,3)
    return $x
}
</xml>

The code above shows that I get the parameter from the url $param1.

variable $person checks to see if there was a parameter and based on that creates a query parameter. This variable works fine, from testing it prints out either '*' for no param or

contributions/person/@val='hello, world'

When I run the query it prints out as if the value is '*'. In the for $x part, can I pass a variable like that? I've tried putting concat($person,'') with the same results. Hardcoding the full path gives me the results I'm looking for, but I'm looking to create something more dynamic.

To note: there is only one variable, $person, but there will be others once I get it to work

4

1 回答 1

1

I think ideally you would avoid dynamic string evaluation. In this example, some pretty simple reorganization would solve the problem without it:

<xml>
{
  for $x in subsequence(//foo/bar[
    if ($param1 = '0')
    then *
    else (contributions/person/@val = $param1)
  ],1,3)
  return $x
}
</xml>

However, you can use eval(), but keep in mind there are security risks:

<xml>
{
  for $x in subsequence(eval(
    concat('//foo/bar[',$person,']')
  ),1,3)
  return $x
}
</xml>
于 2012-10-22T15:17:37.423 回答