2

just trying to create and use an XSL function which display the content of the node if any or a dash if empty.

Here is some parts of the file :

<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>

The error :

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

Some idea?

1
  • Good question, +1. See my answer for the most likely cause of the problem. :) Feb 7, 2011 at 17:14

3 Answers 3

1

You need to define some parameters for your function. You have defined a function qes:textOrDash() you need to add <xsl:param name="input"/> to your function definition and then reference $input instead of . so you have:

<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>
3
  • +1 good answer. The signature of the function as referenced didn't match the signature of the function as defined.
    – LarsH
    Feb 7, 2011 at 16:16
  • Thank you for you fast reply. I already tryed that, but i had the same issue :( Feb 7, 2011 at 16:19
  • There are no context node in function body, so . abbreviation of self::node() rise an error.
    – user357812
    Feb 7, 2011 at 16:41
1

First, I don't think you need a function for that. As example, this stylesheet:

<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>

With this input:

<test>string</test>

Output:

string

With this input:

<test></test>

Output:

-

With this input:

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

Output:

-

And this input:

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

Output

-

About your function: you are only striping space character...

1
  • Shorter: (.[normalize-space()],'-')[1]
    – user357812
    Feb 7, 2011 at 17:06
0

It seems that you are trying to execute an XSLT 2.0 transformation using an XSLT 1.0 processor.

In XSLT 1.0 there is no <xsl:function> instruction, but one may use templates:

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

and

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

<!-- Processing here -->
</xsl:template>
3
  • you right! i am using AXIS 1! Any solution for creating function in XSLT 1.0? Feb 7, 2011 at 17:45
  • @Patrick-Ferreira: In XSLT1.0 just use <xsl:call-template> for any named template. Feb 7, 2011 at 17:53
  • @Patrick-Ferreira: I have updated my answer to show how to write and call named templates in XSLT 1.0 Feb 7, 2011 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.