eXistdb REST api,带_xsl和_查询参数,不返回text/html
我刚刚开始学习eXist数据库,对其中内置的RESTAPI很感兴趣 我使用的应用程序需要查询一个文件,然后对结果应用xslt样式表,但RESTAPI没有返回正确的媒体类型(text/html)。我正在使用eXistdb REST api,带_xsl和_查询参数,不返回text/html,rest,xslt,exist-db,media-type,Rest,Xslt,Exist Db,Media Type,我刚刚开始学习eXist数据库,对其中内置的RESTAPI很感兴趣 我使用的应用程序需要查询一个文件,然后对结果应用xslt样式表,但RESTAPI没有返回正确的媒体类型(text/html)。我正在使用\u xsl和\u query参数(以及\u wrap=no以避免在使用\u query时存在包装结果) 根据Erik Siegel和Adam Retter(O'Reilly,2014)的eXist,当使用\u xsl参数时 以这种方式应用XSLT样式表总是会更改 响应的Internet媒体类型
\u xsl
和\u query
参数(以及\u wrap=no
以避免在使用\u query
时存在包装结果)
根据Erik Siegel和Adam Retter(O'Reilly,2014)的eXist,当使用\u xsl
参数时
以这种方式应用XSLT样式表总是会更改
响应的Internet媒体类型为text/html
(第98页,我加了粗体),但是,我得到了application/xml(使用Firefox中的LiveHTTPHeaders插件确认)
考虑数据库中的以下文档
/db/apps/testing/example.xml
返回html页面(正确呈现为媒体类型为text/html),但返回此url(首先仅选择一个项目元素,然后应用样式表)
返回xml文件(应用程序/xml)
测试
- 这是项目1
这里的结果是正确的,但它使用了错误的媒体类型
似乎添加\u query
参数以首先仅选择文档的一部分会导致媒体类型为application/xml而不是text/html,并且只有当\u xsl
参数本身出现时,Siegel和Retter描述的行为才是正确的
有没有办法使第一个过滤文件的第二个url也返回text/html?还是RESTAPI不可能实现这种行为
(注意:我知道我可以将参数传递给XQuery脚本,并让该脚本过滤和转换数据,但如果可能的话,我对使用REST api来完成这项工作感兴趣。)我查看了eXist的代码,没有看到任何方法可以通过在REST接口上使用
get
请求来获得想要的结果。但是,您可以执行POST
。下面是如何使用wget
完成的示例。请注意,虽然您将文件放在/db/apps/testing/
下,但当我将您的文件上载到我的eXist实例进行测试时,我将它们放在/db/testing/
下(无apps
)。根据需要在以下步骤中调整路径
使用以下内容创建名为post.xml
的文件:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns="http://exist.sourceforge.net/NS/exist"
wrap="no">
<text>/items/item[1]</text>
<properties>
<property name="stylesheet" value="/db/testing/example.xsl"/>
<property name="media-type" value="text/html"/>
</properties>
</query>
或同等标准。您需要将post.xml
的内容作为post
请求的内容,并且content-Type
头必须是application/xml
。-S
选项将响应标题转储到屏幕。因此,当我运行上述操作时,我会在控制台上看到:
--2016-06-15 12:12:10-- http://localhost:8080/exist/rest/db/testing/example.xml
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8080... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Wed, 15 Jun 2016 16:12:10 GMT
Set-Cookie: [...]
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Last-Modified: Tue, 14 Jun 2016 12:21:56 GMT
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Server: Jetty(8.1.9.v20130131)
Length: unspecified [text/html]
Saving to: ‘example.xml’
example.xml [ <=> ] 154 --.-KB/s in 0s
2016-06-15 12:12:10 (6.67 MB/s) - ‘example.xml’ saved [154]
目标是让我可以提供一个列表页面,比如:
,然后提供我在问题中列出的链接。如果我必须使用post模式,我不能提供可点击的链接。现在,我正在将url转发到一个xquery脚本(带有controller.xqy),该脚本运行查询,然后执行转换,但我希望让RESTAPI完成繁重的工作,因为它已经完成了我所需要的一切。然而,如果不能做到这一点,那么我将不得不坚持这样做(无论如何,这可能更安全)。+1因为这个答案很有帮助。我会让这个问题保持一段时间,看看是否有人知道我想要的方法,否则,我会接受这个。如果你不接受我的答案,我不会感到生气。:)也许有更好的办法。我很确定,用eXist 2.2和问题中显示的GET
类型做您想做的事情是不可能的。当我查看代码时,我看到当没有\u查询
时采用的代码路径强制MIME类型为text/html
,如果没有请求显式MIME类型,但指定了样式表。当您指定\u query
时,所采用的代码路径非常不同,并且没有任何等效项来设置MIME类型。
http://localhost:8080/exist/rest/db/apps/testing/example.xml?_xsl=/db/apps/testing/example.xsl
http://localhost:8080/exist/rest/db/apps/testing/example.xml?_query=/items/item[1]&wrap=no&_xsl=/db/apps/testing/example.xsl
<html>
<head>
<title>Testing</title>
</head>
<body>
<ul>
<li>This is item 1</li>
</ul>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns="http://exist.sourceforge.net/NS/exist"
wrap="no">
<text>/items/item[1]</text>
<properties>
<property name="stylesheet" value="/db/testing/example.xsl"/>
<property name="media-type" value="text/html"/>
</properties>
</query>
wget -S --post-file=post.xml --header='Content-Type: application/xml'
http://localhost:8080/exist/rest/db/testing/example.xml
--2016-06-15 12:12:10-- http://localhost:8080/exist/rest/db/testing/example.xml
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8080... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Wed, 15 Jun 2016 16:12:10 GMT
Set-Cookie: [...]
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Last-Modified: Tue, 14 Jun 2016 12:21:56 GMT
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Server: Jetty(8.1.9.v20130131)
Length: unspecified [text/html]
Saving to: ‘example.xml’
example.xml [ <=> ] 154 --.-KB/s in 0s
2016-06-15 12:12:10 (6.67 MB/s) - ‘example.xml’ saved [154]
<html>
<head>
<title>Testing</title>
</head>
<body>
<ul>
<li>This is item 1</li>
</ul>
</body>
</html>