You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询在Saxon的XSL样式表中通过XQuery GET/POST访问eXist-db的方法

在Saxon XSL样式表中调用eXist-db XQL查询的最优方案

核心前提

现代Saxon(9.8及以上版本,PE/EE版原生支持,HE版需额外加载扩展模块)完全兼容EXPath HTTP Client标准,这是当前实现HTTP请求(GET/POST)与eXist-db交互的推荐方式,无需担心兼容性问题。


1. GET请求执行XQL并处理认证

问题分析

  • 使用doc('xmldb:exist:///db/test.xql')会直接读取XQL文件源码而非执行查询,因为xmldb协议仅用于资源访问,不触发查询执行。
  • 直接访问REST接口http://localhost:8080/exist/rest/db/test.xql失败是因为缺少HTTP基础认证信息。

解决方案

通过EXPath HTTP Client发送带认证的GET请求,触发eXist-db的REST接口执行XQL:

<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:http="http://expath.org/ns/http-client">

    <!-- 声明HTTP客户端命名空间 -->
    <xsl:variable name="exist-auth" select="'Basic ' || encode-for-uri('admin:password')"/>
    <xsl:variable name="xql-url" select="'http://localhost:8080/exist/rest/db/test.xql'"/>

    <!-- 发送GET请求并获取查询结果 -->
    <xsl:variable name="get-result">
        <http:send-request>
            <http:request method="GET" href="{$xql-url}">
                <http:header name="Authorization" value="{$exist-auth}"/>
                <http:header name="Accept" value="application/xml"/>
            </http:request>
        </http:send-request>
    </xsl:variable>

    <!-- 使用查询结果 -->
    <xsl:template match="/">
        <xsl:copy-of select="$get-result/http:response/http:body/node()"/>
    </xsl:template>
</xsl:stylesheet>

2. POST发送XML片段给XQL处理

实现逻辑

  • 在XSL中构造XML片段作为POST请求体
  • eXist-db的XQL通过内置的request模块获取请求体内容

XSL端代码(发送POST请求)

<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:http="http://expath.org/ns/http-client">

    <xsl:variable name="exist-auth" select="'Basic ' || encode-for-uri('admin:password')"/>
    <xsl:variable name="xql-url" select="'http://localhost:8080/exist/rest/db/process-xml.xql'"/>

    <!-- 待发送的XML片段 -->
    <xsl:variable name="input-xml">
        <data>
            <item>test content</item>
        </data>
    </xsl:variable>

    <!-- 发送POST请求 -->
    <xsl:variable name="post-result">
        <http:send-request>
            <http:request method="POST" href="{$xql-url}">
                <http:header name="Authorization" value="{$exist-auth}"/>
                <http:header name="Content-Type" value="application/xml"/>
                <http:body>
                    <xsl:copy-of select="$input-xml"/>
                </http:body>
            </http:request>
        </http:send-request>
    </xsl:variable>

    <xsl:template match="/">
        <xsl:copy-of select="$post-result/http:response/http:body/node()"/>
    </xsl:template>
</xsl:stylesheet>

eXist-db端XQL代码(处理POST请求体)

xquery version "3.1";
declare namespace request="http://exist-db.org/xquery/request";

<!-- 获取POST请求体的XML内容 -->
let $input := request:get-body()
<!-- 处理逻辑示例 -->
return <processed>{$input/data/item/text()}</processed>

注意事项

  • 如果使用Saxon HE版,需要自行下载EXPath HTTP Client模块并在启动Saxon时指定加载路径。
  • 认证信息中的用户名密码需替换为你的eXist-db实际凭据,生产环境建议避免硬编码,可通过外部参数传入。
  • eXist-db的REST接口默认启用基础认证,确保你的访问账号有对应资源的执行权限。

内容的提问来源于stack exchange,提问作者lschult2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:01:00