Project

Profile

Help

Calling method from external Java class

Added by Anonymous over 16 years ago

Legacy ID: #4874594 Legacy Poster: Szymon Kamycki (szykam)

Hello! I'm newbie to Saxon and XSL parsing so my question would be lame. I have my custom class in folder d:\java\utils and xslt file in d:\xslt\parser. Example Java code: package java.utils; public class MyClass{ public static String getString(){ return "my string"; } } What code should be in XSLT file's header (<xsl:stylesheet version="2.0" .... ), that I could use getString() method in XSL file. PS. I'm using Saxon 9.0


Replies (3)

Please register to reply

RE: Calling method from external Java class - Added by Anonymous over 16 years ago

Legacy ID: #4874609 Legacy Poster: Michael Kay (mhkay)

First, let's just check that you know where the documentation is: http://www.saxonica.com/documentation/extensibility/functions/staticmethods.html All you need is a namespace declaration: xmlns:String="java:java.utils.String" and then you can call your function <xsl:value-of select="String:getString()"/> Incidentally, java.utils seems rather poor choice of package name - the prefix java is generally associated with classes in the system class library.

RE: Calling method from external Java class - Added by Anonymous over 16 years ago

Legacy ID: #4874667 Legacy Poster: Szymon Kamycki (szykam)

OK... So in d:\xslt\parser folder I have XML and XSL files. My Java class has package declaration "package com.utils" and is to be found in d:\java\com\utils. XSL file <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:func="java:MyClass" exclude-result-prefixes = "java"> ...etc. The line <xsl:variable name="myString"><xsl:value-of select="func:getString()"/></xsl:variable> produces error: Error at xsl:value-of on line 31 XPST0017: XPath syntax error at char 13 on line 31 in {func:getString()}: Cannot find a matching 0-argument function named {java:MyClass}getString() Error at xsl:stylesheet on line 6 XTSE0808: Namespace prefix java has not been declared

RE: Calling method from external Java class - Added by Anonymous over 16 years ago

Legacy ID: #4874695 Legacy Poster: Michael Kay (mhkay)

The error on line 6 relates to exclude-result-prefixes = "java" As it says, there is no namespace declaration xmlns:java="....". The error on line 13 is because your class is not called MyClass, it is called com.utils.MyClass. The -TJ option gives feedback on where Saxon searched for your method, which you may find helps to explain why it isn't found.

    (1-3/3)

    Please register to reply