【问题标题】:Decoding base64 with java.util.Base64.Decoder使用 java.util.Base64.Decoder 解码 base64
【发布时间】:2018-03-20 16:03:48
【问题描述】:

我正在尝试在我的 XSLT 中将输入字符串解码为 base64。

我已经定义了 2 个命名空间:

xmlns:base64Decoder="java:java.util.Base64.Decoder"
xmlns:base64="java:java.util.Base64"

这是我的简单尝试:

        <xsl:variable name="base64Value" select="'aGVsbG8='"/>
        <xsl:variable name="deco" select="base64:getDecoder()"/>
        <xsl:value-of select="base64Decoder:decode($deco, $base64Value)"/>

执行没有编译并向我显示消息:

找不到名为 {java:java.util.Base64.Decoder}decode() 的匹配 2 参数函数

我知道还有其他方法,但我特别想知道为什么这个方法不起作用。

提前致谢。

添加氧气诊断信息以解决方法调用(请参阅跟踪:“类 java.util.Base64.Decoder 无法加载:java.util.Base64.Decoder”几乎在最后):

Looking for function {java:java.util.Base64}getDecoder
Trying net.sf.saxon.functions.SystemFunctionLibrary
Trying net.sf.saxon.style.StylesheetFunctionLibrary
Trying com.saxonica.functions.extfn.ExtraFunctionLibrary
Trying net.sf.saxon.functions.ConstructorFunctionLibrary
Trying net.sf.saxon.query.XQueryFunctionLibrary
Trying net.sf.saxon.functions.IntegratedFunctionLibrary
Trying com.saxonica.config.JavaExtensionLibrary
Looking for method getDecoder in namespace java:java.util.Base64
Number of actual arguments = 0
Loading java.util.Base64
Looking in Java class class java.util.Base64
Trying method getMimeDecoder: name does not match
Trying method getDecoder: name matches
Method is static
Method has 0 arguments; expecting 0
Found a candidate method:
    public static java.util.Base64$Decoder java.util.Base64.getDecoder()
Trying method getEncoder: name does not match
Trying method getMimeEncoder: name does not match
Trying method getMimeEncoder: name does not match
Trying method getUrlDecoder: name does not match
Trying method getUrlEncoder: name does not match
Trying method wait: name does not match
Trying method wait: name does not match
Trying method wait: name does not match
Trying method equals: name does not match
Trying method toString: name does not match
Trying method hashCode: name does not match
Trying method getClass: name does not match
Trying method notify: name does not match
Trying method notifyAll: name does not match
Looking for function {java:java.util.Base64.Decoder}decode
Trying net.sf.saxon.functions.SystemFunctionLibrary
Trying net.sf.saxon.style.StylesheetFunctionLibrary
Trying com.saxonica.functions.extfn.ExtraFunctionLibrary
Trying net.sf.saxon.functions.ConstructorFunctionLibrary
Trying net.sf.saxon.query.XQueryFunctionLibrary
Trying net.sf.saxon.functions.IntegratedFunctionLibrary
Trying com.saxonica.config.JavaExtensionLibrary
Looking for method decode in namespace java:java.util.Base64.Decoder
Number of actual arguments = 2
Loading java.util.Base64.Decoder
The class java.util.Base64.Decoder could not be loaded: java.util.Base64.Decoder
Trying net.sf.saxon.style.StylesheetFunctionLibrary
Function {java:java.util.Base64.Decoder}decode not found!
Loading java.util.Base64.Decoder
The class java.util.Base64.Decoder could not be loaded: java.util.Base64.Decoder

【问题讨论】:

  • 那么您到底使用哪个版本的 XSLT 处理器?对 Java 的调用不是标准化的,如何调用也不取决于特定的 XSLT 处理器。
  • 感谢您的关注:我在 XSLT 2.0 中使用 Saxon PE 9.4 并在 Java 8 下运行。

标签: xslt saxon


【解决方案1】:

如果使用Java内部类名,则找到该类:

xmlns:base64Decoder="java:java.util.Base64$Decoder"

不幸的是,你又遇到了另一个问题:

Static error in xsl:value-of/@select on line 16 column 71 of test.xsl:
  XPST0017: There is more than one method matching the function call base64Decoder:decode,
  and there is insufficient type information to determine which one should be used

您可以通过将调用编写为:

<xsl:value-of select="base64Decoder:decode($deco, $base64Value treat as xs:string)"/>

然后代码成功运行并打印输出

104 101 108 108 111

【讨论】:

  • 非常感谢凯先生。现在,如果我想将该字节数组打印为可读文本,我会在 XSLT 中使用等价的:String result = new String(),但 XSLT 编译器会说“有不止一种方法匹配函数调用 javaString:new,并且没有足够的类型信息来确定应该使用哪一个”。有没有办法在 SAXON XSLT 中指定类型“byte[]”以便“new String()”匹配?再次感谢。问候。
  • 已修复!似乎使用构造函数:String(byte[] bytes, String charsetName) 没有歧义:。无论如何,知道如何在 XSLT 中区分一个 byte[] 对象来解决应该使用哪种方法会很有趣。谢谢!
  • 这是一个有点学术性的问题,因为您可以使用 expath Binary 模块中定义的函数来完成所有这些工作,而不是自己开发......
  • 更具体地说,请参阅expath.org/spec/binary#decode-string,它在最近的 Saxon 版本中实现,但我不确定 9.4。但也有更早的saxonica.com/documentation/index.html#!functions/saxon/…
  • 再次非常感谢您,Kay 先生:非常感谢您的帮助。无论如何,我最关心的是如何在 Saxon XSL 中解决这种 Java 类型冲突,但最好的选择肯定是尽可能避免它们。最好的问候。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 2018-09-23
  • 2015-07-14
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多