我正在尝试使用http接口访问BaseX数据库。
设置如下:
` <?xml version="1.0" encoding="UTF-8"?>
<musicCollection>
<entry>
<Type>CD</Type>
<Name>Greatest Hits</Name>
<Artist>The Music Band</Artist>
<Year>1998</Year>
<Songs>
<Song>
<Name>Song 1</Name>
<Author>John Songwriter</Author>
<Length>4:30</Length>
</Song>
<Song>
<Name>Song 2</Name>
<Author>Lisa Lyricist</Author>
<Length>3:45</Length>
</Song>
<Song>
<Name>Song 3</Name>
<Author>Lisa Lyricist</Author>
<Length>1:45</Length>
</Song>
</Songs>
</entry>
.... more entries
</musicCollection>
`
java -cp BaseX.jar org.basex.BaseXClient ...以测试/测试身份登录...
出现提示:
xquery for $e in doc('music')/musicCollection/entry where $e/Type = "LP" return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry>
结果:
<entry><Type>LP</Type><Name>Rock Revolution</Name><Artist>The Rockers</Artist></entry>
<entry><Type>LP</Type><Name>Rock Revolution</Name><Artist>The Other Ones</Artist></entry>
Query "BaseX" executed in 6.44 ms.
"D:\Programs\BaseX\bin\basexhttp.bat" -h123
curl -u test:test -X POST -d "for $e in doc('music')/musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry>" "http://localhost:123/rest"
还有这个:
curl -u test:test -X POST -d "for $e in /musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry>" "http://localhost:123/rest/music"
` 我收到此错误回复:
"" (Line 1): Content ist nicht zulässig in Prolog. (Line 1: content is illegal in prolog)
尝试这个:
curl -u test:test -X POST -d "<query>for $e in /musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry></query>" "http://localhost:123/rest/music"
我得到了:
Stopped at D:/Programs/BaseX/webapp, 1/1:
[XPST0003] Empty query.
这样:
curl -u test:test -X POST -d "<query><text>for $e in /musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry></text></query>" "http://localhost:123/rest/music"
我收到此错误:
Stopped at D:/Programs/BaseX/webapp, 1/41:
[XPST0003] Expecting return value.
发送此查询的正确方式是什么?
您上次尝试使用 POST 方法发送查询的结果非常接近:如果您的 XQuery 字符串包含尖括号或 XML 实体,则您需要使用 CDATA 来防止查询字符被解释为 XML:
curl
-u test:test
-X POST
-d "<query><text><![CDATA[for $e in /musicCollection/entry return <entry> {$e/Type} {$e/Name} {$e/Artist} </entry>]]></text></query>"
"http://localhost:123/rest/music"
如果您的查询字符串明确,则不需要 CDATA 部分:
curl
-u test:test
-X POST
-d "<query><text>/musicCollection/entry/(element entry {Type,Name,Artist})</text></query>"
"http://localhost:123/rest/music"