7

I am trying to create a custom XSLT function, but every time I receive this error:

'The first argument to the non-static Java function 'compareCI' is not a valid object reference.'

  <xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:foo="http://whatever">

  <xsl:function name="foo:compareCI">
    <xsl:param name="string1"/>
    <xsl:param name="string2"/>
    <xsl:value-of select="compare(upper-case($string1),upper-case($string2))"/>
  </xsl:function>

  <xsl:template match="/">
      <xsl:value-of select="foo:compareCI('red','blue')"/>
  </xsl:template>

</xsl:stylesheet> 

I hope someone of you can help me .Thanks a lot in advance.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
javagc
  • 133
  • 6
  • 13
  • 1
    It is working fine at my end in Oxygen. It is giving result '1' when comparing 'red' with 'blue', and '0' when comparing 'red' withn'red' – Navin Rawat Apr 23 '13 at 08:42
  • 1
    How do you run your XSLT and with which processor? (I used Saxon-HE 9.4.0.6 and it works) – Philipp Apr 23 '13 at 08:52
  • I don't know which version used Java. My Java code is TransformerFactory factory = TransformerFactory.newInstance(); Source xslt = new StreamSource(new File("/home/xxx/xxx/aliformater-1.xslt")); Transformer transformer = factory.newTransformer(xslt); – javagc Apr 23 '13 at 10:17

1 Answers1

8

I think you are trying to run this using Xalan, which is an XSLT 1.0 processor and therefore doesn't recognize xsl:function. What's happening is that (a) Xalan ignores the xsl:function, because an XSLT 1.0 processor that is given a stylesheet specifying version="2.0" is supposed to ignore things it doesn't understand (called "forwards compatibility mode" in the spec); and then when it sees the function call to foo:compareCI() it thinks this must be a call to an external Java method.

You need to run this with an XSLT 2.0 processor, typically Saxon.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164