You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

按文档在BaseX REST POST传变量却提示$which未定义,原因何在?

Fixing "Undefined variable $which" in BaseX REST Queries

Looks like you're hitting a common gotcha with external variables in BaseX's REST API! The error [XPST0008] Undefined variable $which happens because your XQuery doesn't explicitly declare that $which is an external variable meant to be passed in via the REST request.

The Root Cause

In XQuery, any variable you plan to inject from outside the query itself needs to be marked as external. Without this declaration, the BaseX parser doesn't recognize the variable as something that will be bound later, so it throws the "undefined variable" error.

The Fix

Update your query text to include the external variable declaration, then keep your <variable> element as-is. Here's the corrected version of your query:

<query xmlns="http://basex.org/rest">
  <text>declare variable $which external; //greeting[position()=$which]</text>
  <variable name="which" value="1"/>
  <context><xml><greeting>Hello!</greeting></xml></context>
</query>

A Quick Note on position()

I also changed the value from 0 to 1 because XQuery's position() function starts counting at 1, not 0. A value of 0 would never match any elements, even after fixing the variable issue.

Verifying Your CURL Request

Make sure your CURL command is sending the XML with the correct Content-Type header, like this:

curl -X POST -H "Content-Type: application/xml" -d @your-query-file.xml http://localhost:8984/rest/

That should resolve the undefined variable error and let your query use the injected value correctly.

内容的提问来源于stack exchange,提问作者kdojeteri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:37:36