Search code examples
xquerybasexflwor

Handling undefined number of external variables


I'm using baseX in a REST environment and I'm quite stuck trying to run an .xq script with an undefined number of GET variables (could be 1 but could be 10) I'd like to make my xq script generic about that and construct my query independently. Is there a way to achieve that, playing with array or sending differently my variables, or I dunno how ?


here is my API call

http://basex:8984/rest/?run=WEB-INF/data/test.xq&$tag=p&value=sciences&tag2=p&value2=test&tag3=testdzq

here is my text.xq

declare variable $tag external;
declare variable $value external;

declare variable $tag2 external;
declare variable $value2 external;

<documents>
  {for $doc in collection("testdb2")
    where $doc//*[name() eq $tag]/text()[matches(., $value )]
      and  $doc//*[name() eq $tag2]/text()[matches(., $value2 )]

    return <doc>{$doc//titleStmt/title/text()}</doc>
  }
</documents>

Thanks !


Solution

  • found this here (see http-params function) https://www.balisage.net/Proceedings/vol18/print/Murray01/BalisageVol18-Murray01.html

    (: BaseX example :)
    
    (: In the controller ... :)
    module namespace c = "http://balisage.net/ns/Bal2016murr0319/controller";
    import module namespace request = "http://exquery.org/ns/request";
    import module namespace item = "http://balisage.net/ns/Bal2016murr0319/views/item" at "views/item.xqm";
    
    (:~ Returns a map containing the HTTP request parameters. :)
    declare function c:http-params()
    as map(*)
    {
      map:merge(
        for $name in request:parameter-names()
        let $value := request:parameter($name)
        return map:entry($name, $value)
      )
    };
    
    (:~ Calls the appropriate view, based on user input. :)
    declare function c:get-view()
    as element(html)
    {
      (: get HTTP request parameters :)
      let $params := c:http-params()
      return
        if (map:get($params, "id")) then
          (: the presence of "id" indicates that the user is requesting the item-level page for this unique identifier :)
          (: call the item-level view :)
          item:get-html($params)
        else if ... (: call some other view :)
        else if ... (: call some other view :)
        else (: call the view for the home page ... :)
    };