Search code examples
character-encodingxquerybasex

Can't query baseX using an Xquery with "&"


I'm trying to use the baseX REST API with python's requests post method, using a saved .xq file which contains a query with an &. When running this saved query directly on baseX, there's no problem.

The request as presented in the response also includes the & as it is and not as an &, but I still get the following error (response code is 400):

" Stopped at C:/Program Files (x86)/BaseX/webapp, 37/37:\n[XPST0003] Invalid entity: '&&", "||", "!")) the...'.' "

The relevant part of the request's body is:

<rest:query xmlns:rest="http://basex.org/rest"> <rest:text>declare function local:enrich_node($attr, $supertype) {
    $attr, attribute {"supertype"} {$supertype}
};

declare function local:enrich($n as node()) as node() {
   typeswitch($n)
      ...
      case $e as element(operator)
         return 
              ...
              else if ($e/text()=("&amp;&amp;", "||", "!")) then
                element {name($e)}
                    {local:enrich_node($e/@*, "boolop"), for $c in $e/(* | text())
                         return local:enrich($c) } 
              else
                 ...
 };

declare variable $assign_id as xs:string external;
declare variable $submission_id as xs:string external;
for $node in db:open($assign_id, $submission_id)
return local:enrich($node)</rest:text><variable name="assign_id" value="val1"/><variable name="submission_id" value="val2"/></rest:query>

When I remove the &amp;&amp; part from the query it works.

I tried to look for relevant questions but didn't find anything, other then a suggestion to "escape" it with another &amp; which I tried but then the returned error was with 4 &s. Any ideas?


Solution

  • As the content of rest:text has to be evaluated as XQuery code but should not be parsed as XML it should help to wrap the XQuery code inside of rest:text in a CDATA section.