|
next
|
 Subject: XSLT Extension Functions & Xalan Author: Brendan Mulrooney Date: 18 Dec 2006 12:51 PM
|
Hi Support,
Previously I have successfully implemented extension functions using the MSXML processor & VBScript without any issues (was approx. 3 years ago!).
Basically, all I needed to do was create a Java class containing a wrapper function declaration which then enabled the function to be registered with Stylus Studio (via classpath) so that it was available to the developer within the Mapper GUI. The only requirement for the Java class was to enable registration with Stylus, at run-time it is the actual VBScript that is executed, is this a valid statement?
Anyway, I declared my functions in the class file as below:
File 1: master.class (subset of functions)
import org.w3c.dom.*;
class master {
public Object getDate()
{
return "";
}
}
Then to be clever (re-use, maintenance etc.), I created a single XSLT file to contain all VBScript functions. Example below:
File 2: master.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:master="master">
<msxsl:script implements-prefix="master" language="VBScript">
<![CDATA[
Function getDate()
Dim sOutput
sOutput = CStr(Now)
getDate = sOutput
End Function
]]>
</msxsl:script>
</xsl:stylesheet>
Note: I believe the script needs to be within the CDATA section to ensure the script is ignored by the processor, is this correct?
Then include reference to master.xslt from any other xslt and the function could be utilised. Example below:
File 2: test.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet exclude-result-prefixes="master" version="1.0" xmlns:master="master" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:include href="master.xslt"/>
<xsl:template match="/">
<Date> <xsl:value-of select="master:getDate()"/> </Date>
</xsl:template>
</xsl:stylesheet>
As mentioned, this worked well but we were using the MSXML parser. I am now attempting to do the equivalent this time using the Xalan processor, what a nightmare! I am having numerous issues even getting a simple date function working.
I had hoped to implement the same type of setup whereby all extension functions could be contained within a single xslt file, but at present I cannot even invoke the function from the same stylesheet.
Steps Taken (so far):
1. Created Java class file & registered with Stylus
import org.w3c.dom.*;
import java.util.Date;
class Ext_Lib
{
public Object setDate()
{
Date d = new Date();
String s = d.toString();
return s;
}
}
2. Created the following XSLT file to test function
<?xml version='1.0' encoding='UTF-16' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" xmlns:my-ext="ext1" extension-element-prefixes="my-ext">
<xsl:output method="xml" encoding="UTF-16"/>
<xalan:component prefix="my-ext" functions="getdate">
<xalan:script lang="javascript" src="http://xml.apache.org/xalan/java">
function getdate()
{
return Date();
}
</xalan:script>
</xalan:component>
<xsl:template match="/">
<Catalog>
<Book>
<PubDate>
<xsl:value-of select="my-ext:getdate()"/>
</PubDate>
</Book>
</Catalog>
</xsl:template>
</xsl:stylesheet>
The XalanJ 2.5.2 processor is returning numerous errors and believe a) the environment is not setup correctly or b) I'm just dumb.
Can you provide any assistance? I have consulted both the Stylus guide on extension functions (which is not specific based on processor - and should not be!) and the Xalan website (http://xml.apache.org/xalan-j/extensions.html) which was raving on about requiring the Bean Scripting Framework (BSF) etc. etc, I had the impression these extentions are packaged already??
Anyway, at present I'm quite confused and any assistance would be appreciated. It has to be easier than this!, or I could go back to MSXML (which I know works).
Regards
Brendan
|
|
|