这是
的后续问题Getting hold of tag content in XQuery 3.0 (exist-db)
假设这样的xquery脚本应该能够基于查询参数(如
)将结果作为XML,JSON或HTML返回http://host/exist/rest/db/myscript.xql?mode=xml|html|json
我知道如何从XML更改序列化程序 - > JSON以及如何申请 使用transform:transform()。
进行XSLT转换为结果封装XML生成然后根据请求参数将其转换为输出格式之一的最佳方法是什么?
xquery version "3.0";
module namespace services = "http://my/services";
import module namespace transform = "http://exist-db.org/xquery/transform";
declare namespace rest = "http://exquery.org/ns/restxq";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare
%rest:GET
%rest:path("/data.html")
%output:media-type("text/html")
%output:method("html5")
function services:home() {
transform:transform(services:example1(), doc("/db/my-xml-to-html.xslt"), ())
};
declare
%rest:GET
%rest:path("/data.json")
%rest:produces("application/json")
%output:method("json")
function services:home-json() {
services:example1()
};
declare
%rest:GET
%rest:path("/data.xml")
%rest:produces("application/xml")
function services:home-xml() {
services:example1()
};
declare
%private
function services:example1() {
<some>
<data>hello world</data>
</some>
};
答案 0 :(得分:4)
我建议在eXist中查看RESTXQ,因为这样可以根据内容协商或任何其他HTTP参数或标题轻松控制结果格式。例如,使用内容协商来生成XML,JSON和HTML表现形式:
xquery version "3.0";
module namespace services = "http://my/services";
import module namespace transform = "http://exist-db.org/xquery/transform";
declare namespace rest = "http://exquery.org/ns/restxq";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare
%rest:GET
%rest:path("/data")
%rest:produces("text/html")
%output:method("html5")
function services:home() {
transform:transform(services:example1(), doc("/db/my-xml-to-html.xslt"), ())
};
declare
%rest:GET
%rest:path("/data")
%rest:produces("application/json")
%output:method("json")
function services:home-json() {
services:example1()
};
declare
%rest:GET
%rest:path("/data")
%rest:produces("application/xml")
function services:home-xml() {
services:example1()
};
declare
%private
function services:example1() {
<some>
<data>here</data>
</some>
};