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

Saxon XSLT调用Files.copy()因枚举类型报错的解决咨询

在Saxon XSLT中调用Files.copy()时枚举类型的类型匹配问题

问题描述

我在Saxon XSLT里调用Java方法时,File.renameTo()这类方法能正常运行,但调用Files.copy()时因为枚举类型的问题失败了。我的XSLT代码如下:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:files="java:java.nio.file.Files" xmlns:path="java:java.nio.file.Path" xmlns:copyopt="java:java.nio.file.StandardCopyOption" exclude-result-prefixes="files path copyopt" version="3.0" expand-text="yes">
    <xsl:template match="/">
        <xsl:if test="files:copy(path:of(xs:anyURI('file:///from.txt')),path:of(xs:anyURI('file:///to.txt')),copyopt:REPLACE_EXISTING)"/>
    </xsl:template>
</xsl:stylesheet>

报错信息:

There is more than one method matching the function call Q{java:java.nio.file.Files}copy#3, and there is insufficient type information to determine which one should be used

请问该如何正确为枚举REPLACE_EXISTING指定类型?

解决方法

Saxon无法自动推断枚举参数的类型,核心原因是Files.copy()存在多个重载方法,其中一个接受可变参数StandardCopyOption...,类型歧义导致无法匹配正确的方法。你可以通过以下两种方式明确类型:

1. 显式转换枚举类型

用cast as将枚举值转换为java:java.nio.file.CopyOption类型(StandardCopyOption是CopyOption的子类,对应Files.copy()接受的参数类型),修改后的代码如下:

<xsl:if test="files:copy(
    path:of(xs:anyURI('file:///from.txt')),
    path:of(xs:anyURI('file:///to.txt')),
    copyopt:REPLACE_EXISTING cast as java:java.nio.file.CopyOption
)"/>

2. 将枚举包装为序列传递

由于Java可变参数本质是数组,把枚举值放入序列中可以让Saxon更清晰识别参数类型,代码如下:

<xsl:if test="files:copy(
    path:of(xs:anyURI('file:///from.txt')),
    path:of(xs:anyURI('file:///to.txt')),
    (copyopt:REPLACE_EXISTING)
)"/>

两种方式都能让Saxon精准匹配到正确的Files.copy()重载方法,解决类型歧义问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 08:02:10