【发布时间】:2014-08-22 22:25:40
【问题描述】:
这是
的后续问题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>
};
【问题讨论】: