【问题标题】:error on declaring and using function on xsl在 xsl 上声明和使用函数时出错
【发布时间】:2011-02-07 16:09:02
【问题描述】:

只是尝试创建和使用一个 XSL 函数,该函数显示节点的内容(如果有)或短划线(如果为空)。

这是文件的一些部分:

<xsl:stylesheet version="2.0" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:qes="http://www.qwamci.com">

  <xsl:function name="qes:textOrDash" as="xs:string">
    <xsl:param name="mynode" />
    <xsl:sequence select="if (fn:compare(translate($mynode, ' ', ''), '')=0) then '-' else $mynode" />
  </xsl:function>

  <xsl:template match="Response">
    <xsl:value-of select="qes:textOrDash(./SOME/OTHER/XPATH/TO/NODE)" />
  </xsl:template>

</xsl:stylesheet>

错误:

Erreur:java.lang.NoSuchMethodException: For extension function, could not find method org.apache.xml.utils.NodeVector.textOrDash([ExpressionContext,] ).

有什么想法吗?

【问题讨论】:

  • 好问题,+1。请参阅我的答案以了解问题的最可能原因。 :)

标签: templates function xslt xslt-2.0


【解决方案1】:

您需要为您的函数定义一些参数。您已经定义了一个函数qes:textOrDash(),您需要将&lt;xsl:param name="input"/&gt; 添加到您的函数定义中,然后引用$input 而不是.,这样您就有:

<xsl:function name="qes:textOrDash" as="xs:string">
  <xsl:param name="input" />
  <xsl:sequence select="if (fn:compare(translate($input, ' ', ''), '')=0) then '-' else ." />
</xsl:function>

【讨论】:

  • +1 好答案。引用的函数签名与定义的函数签名不匹配。
  • 感谢您的快速回复。我已经尝试过了,但我遇到了同样的问题:(
  • 函数体中没有上下文节点,所以.self::node()的缩写会报错。
【解决方案2】:

首先,我认为您不需要为此提供功能。例如,这个样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="test">
        <xsl:value-of select="(.,'-')[normalize-space(.)][1]"/>
    </xsl:template>
</xsl:stylesheet>

有了这个输入:

<test>string</test>

输出:

string

有了这个输入:

<test></test>

输出:

-

有了这个输入:

<test><not-string-value/></test>

输出:

-

这个输入:

<test>&#x20;&#xA;&#x9;&#xD;</test>

输出

-

关于您的功能:您只是剥离空格字符...

【讨论】:

  • 短:(.[normalize-space()],'-')[1]
【解决方案3】:

您似乎正在尝试使用 XSLT 1.0 处理器执行 XSLT 2.0 转换

在 XSLT 1.0 中没有 &lt;xsl:function&gt; 指令,但可以使用模板:

<xsl:call-template name="textOrDash">
 <xsl:with-param name="mynode" select="SomeXPath-Expression"/>
</xsl:call-template>

<xsl:template name="textOrDash">
 <xsl:param name="mynode" select="someDefault"/>

<!-- Processing here -->
</xsl:template>

【讨论】:

  • 你说得对!我正在使用轴 1!在 XSLT 1.0 中创建函数的任何解决方案?
  • @Patrick-Ferreira:在 XSLT1.0 中,只需对任何命名模板使用 &lt;xsl:call-template&gt;
  • @Patrick-Ferreira:我已经更新了我的答案,以展示如何在 XSLT 1.0 中编写和调用命名模板
猜你喜欢
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
  • 2018-02-24
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
相关资源
最近更新 更多