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

在DataPower中使用XSLT操作temporary://下token.jsonx文件的问题

DataPower XSLT 检查并读取temporary目录文件的正确实现

你之前的错误分析

  1. dp:url-open参数错误:你在target属性里额外添加了单引号,导致URL解析失败。正确的target应该直接写路径temporary:///token.jsonx,不需要包裹单引号;同时读取本地文件时不需要http-method="get"参数,该参数仅用于HTTP请求场景。
  2. document()函数未做存在性检查:document()函数在文件不存在时会直接抛出错误,必须先判断文件存在后再调用。

正确实现步骤

DataPower提供了专门的dp:file-exists()函数用于检查文件是否存在,结合dp:url-open读取文件后执行替换操作。以下是完整示例:

完整XSLT代码示例

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dp="http://www.datapower.com/extensions"
    extension-element-prefixes="dp">

    <xsl:template match="/">
        <!-- 检查temporary目录下的token.jsonx是否存在 -->
        <xsl:if test="dp:file-exists('temporary:///token.jsonx')">
            <!-- 读取文件内容 -->
            <xsl:variable name="token-content">
                <dp:url-open target="temporary:///token.jsonx" response="xml" datatype="filename"/>
            </xsl:variable>

            <!-- 执行替换操作示例:替换jsonx中的token节点值 -->
            <xsl:apply-templates select="$token-content" mode="replace-token"/>
        </xsl:if>
        <xsl:if test="not(dp:file-exists('temporary:///token.jsonx'))">
            <!-- 文件不存在时的处理逻辑 -->
            <xsl:message terminate="no">Warning: token.jsonx does not exist in temporary:/// directory</xsl:message>
        </xsl:if>
    </xsl:template>

    <!-- 匹配需要替换的节点 -->
    <xsl:template match="*[@name='token']" mode="replace-token">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <!-- 替换为新的token值 -->
            <xsl:text>new-valid-token-123</xsl:text>
        </xsl:copy>
    </xsl:template>

    <!-- 复制其他节点的默认模板 -->
    <xsl:template match="@*|node()" mode="replace-token">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="replace-token"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

关键说明

  • 文件存在性检查:dp:file-exists('temporary:///token.jsonx')是DataPower扩展函数,专门用于判断本地文件系统(包括temporary域)的文件是否存在,从根源避免读取不存在文件导致的报错。
  • dp:url-open正确用法:读取本地文件时,指定target为文件路径,datatype="filename"表示读取本地文件,response="xml"适配jsonx的XML格式(如果是纯JSON可改为response="json"),无需http-method参数。
  • 替换操作:通过模板匹配定位需要修改的节点,修改后保留原节点属性与其他结构,保证输出的完整性。

内容的提问来源于stack exchange,提问作者François Bagein

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 07:45:03