带有_xsl和_query参数的eXistdb REST api不返回text/html



我刚刚开始学习eXist数据库,并对其中内置的RESTapi很感兴趣。

我使用的应用程序需要查询一个文件,然后对结果应用xslt样式表,但RESTapi没有返回正确的媒体类型(text/html/strong>)。我正在使用_xsl_query参数(以及_wrap=no,以避免在使用_query时出现eXist包装结果)。

根据Erik Siegel和Adam Retter的eXist(O’Reilly,2014),当使用_xsl参数时,

以这种方式应用XSLT样式表总是会更改响应的Internet媒体类型为text/html/strong>。

(第98页,我添加了粗体),但是,我正在返回application/xml(使用Firefox中的LiveHTTPHeaders插件确认)。

考虑数据库中的以下文档

/db/apps/testing/example.xml

<items>
<item>This is item 1</item>
<item>This is item 2</item>
</items>

/db/apps/testing/example.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head><title>Testing</title></head>
<body>                
<ul>
<xsl:apply-templates select="*"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="items">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="item">
<li><xsl:value-of select="."/></li>
</xsl:template>
</xsl:stylesheet>

然后这个url(将样式表应用于整个文档)

http://localhost:8080/exist/rest/db/apps/testing/example.xml?_xsl=/db/apps/testing/example.xsl

返回一个html页面(正确呈现为媒体类型为text/html/strong>),但是这个url(首先只选择一个item元素,然后应用样式表)

http://localhost:8080/exist/rest/db/apps/testing/example.xml?_query=/items/item[1]&wrap=no&_xsl=/db/apps/testing/example.xsl

返回一个xml文件(application/xml)

<html>
<head>
<title>Testing</title>
</head>
<body>
<ul>
<li>This is item 1</li>
</ul>
</body>
</html>

这里的结果是正确的,但使用了错误的媒体类型。

似乎添加_query参数以首先只选择文档的一部分会导致媒体类型为application/xml,而不是text/html/strong>,并且只有当_xsl参数本身发生时,Siegel和Retter描述的行为才是真的。

有没有办法让第一个过滤文件的第二个url也返回text/html/strong>?或者RESTapi不可能实现这种行为?

(注意:我知道我可以将参数传递给XQuery脚本,并让该脚本过滤和转换数据,但如果可能的话,我有兴趣使用RESTapi来完成这里的工作。)

我查看了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>

然后发出以下命令:

wget -S --post-file=post.xml --header='Content-Type: application/xml'
http://localhost:8080/exist/rest/db/testing/example.xml

或者同等的。您需要将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]

注意响应的Content-Typetext/htmlwget将结果保存在名为example.xml的本地文件中,该文件包含预期数据:

<html>
<head>
<title>Testing</title>
</head>
<body>
<ul>
<li>This is item 1</li>
</ul>
</body>
</html>

最新更新