eXist db REST api with _xsl and query parameter not returning text/html
eXistdb REST api with _xsl and _query parameter not returning text/html
我刚刚开始学习 eXist 数据库,并对其中内置的 REST api 很感兴趣。
我正在使用的应用程序需要查询一个文件,然后将 xslt 样式表应用于结果,但是 REST api 没有 return 正确的媒体类型(text/html)。我正在使用 _xsl
和 _query
参数(以及 _wrap=no
以避免在使用 _query
时出现 eXist 包装结果)。
根据 Erik Siegel 和 Adam Retter 的 eXist(O'Reilly,2014),当使用 _xsl
参数时,
Applying an XSLT stylesheet in this manner always changes the
response's Internet media type to text/html.
(第 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
returns 一个 html 页面(由于媒体类型是 text/html 而正确呈现),但是这个 url(首先 select 仅使用一个项目元素,然后应用样式表)
http://localhost:8080/exist/rest/db/apps/testing/example.xml?_query=/items/item[1]&wrap=no&_xsl=/db/apps/testing/example.xsl
return一个 xml 文件 (application/xml)
<html>
<head>
<title>Testing</title>
</head>
<body>
<ul>
<li>This is item 1</li>
</ul>
</body>
</html>
这里的结果是正确的,但是使用了错误的媒体类型。
似乎添加 _query
参数以便首先 select 文档的一部分导致媒体类型为 application/xml 而不是 text/html,Siegel 和 Retter 描述的行为仅在 _xsl
参数单独出现时才为真。
有没有办法让第二个 url 也首先过滤文件 return text/html?或者这种行为对于 REST api 是不可能的?
(注意:我知道我可以将参数传递给 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>
然后发出这个命令:
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
header必须是application/xml
。 -S
选项将响应 header 转储到屏幕。所以当我 运行 上面的内容时,我在控制台上得到了这个:
--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-Type
是 text/html
。 wget
将结果保存在名为 example.xml
的本地文件中,其中包含预期数据:
<html>
<head>
<title>Testing</title>
</head>
<body>
<ul>
<li>This is item 1</li>
</ul>
</body>
</html>
我刚刚开始学习 eXist 数据库,并对其中内置的 REST api 很感兴趣。
我正在使用的应用程序需要查询一个文件,然后将 xslt 样式表应用于结果,但是 REST api 没有 return 正确的媒体类型(text/html)。我正在使用 _xsl
和 _query
参数(以及 _wrap=no
以避免在使用 _query
时出现 eXist 包装结果)。
根据 Erik Siegel 和 Adam Retter 的 eXist(O'Reilly,2014),当使用 _xsl
参数时,
Applying an XSLT stylesheet in this manner always changes the response's Internet media type to text/html.
(第 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
returns 一个 html 页面(由于媒体类型是 text/html 而正确呈现),但是这个 url(首先 select 仅使用一个项目元素,然后应用样式表)
http://localhost:8080/exist/rest/db/apps/testing/example.xml?_query=/items/item[1]&wrap=no&_xsl=/db/apps/testing/example.xsl
return一个 xml 文件 (application/xml)
<html>
<head>
<title>Testing</title>
</head>
<body>
<ul>
<li>This is item 1</li>
</ul>
</body>
</html>
这里的结果是正确的,但是使用了错误的媒体类型。
似乎添加 _query
参数以便首先 select 文档的一部分导致媒体类型为 application/xml 而不是 text/html,Siegel 和 Retter 描述的行为仅在 _xsl
参数单独出现时才为真。
有没有办法让第二个 url 也首先过滤文件 return text/html?或者这种行为对于 REST api 是不可能的?
(注意:我知道我可以将参数传递给 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>
然后发出这个命令:
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
header必须是application/xml
。 -S
选项将响应 header 转储到屏幕。所以当我 运行 上面的内容时,我在控制台上得到了这个:
--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-Type
是 text/html
。 wget
将结果保存在名为 example.xml
的本地文件中,其中包含预期数据:
<html>
<head>
<title>Testing</title>
</head>
<body>
<ul>
<li>This is item 1</li>
</ul>
</body>
</html>