XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Brendan MulrooneySubject: 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


Postnext
Ivan PedruzziSubject: XSLT Extension Functions & Xalan
Author: Ivan Pedruzzi
Date: 18 Dec 2006 03:18 PM
Brendan,

Did you look at the example IntDate.xsl that ships with Stylus Studio?

To use a Java extesion function you need the following:

- The namespace declaration to bind a prefix to the Java class name
xmlns:IntDate="IntDate"

- The Java class compiled (.class) in the CLASSPATH

- The invocation looks like any other built-in XPath functions
<xsl:value-of select="IntDate:getDate(number(@year), number(@month), number(@day), string(@format))"/>


Hope this helps
Ivan Pedruzzi
Stylus Studio Team

Postnext
Brendan MulrooneySubject: XSLT Extension Functions & Xalan
Author: Brendan Mulrooney
Date: 18 Dec 2006 05:21 PM
Originally Posted: 18 Dec 2006 05:19 PM
Hi Ivan,

I have reviewed the sample and this differs slightly from what I am attempting to achieve in the fact that in the sample, the Java class is used at run-time (and hence I assume the class is required on the server - prod). I am attempting to implement the extension function via JavaScript, therefore I only need to create a Java class basically as a function declaration which returns nothing (to make the function accessible via Stylus mapper UI). At run-time the actual JavaScript (contained in the xsl:include XX.xslt file) would be used by the processor.

Can you see any issues with this approach? If I follow the example (IntDate.xsl), am I correct in assuming the java class will need to be accessible? This could be an issue as we are performing the XSLT transformation from within a proprietry Siebel business service (XSLT transform) which I believe will not be able to access the required Java class. This is why I intended to use javascript which can be interpreted from within the xslt file directly.

Interested in your thoughts.

Regards

Brendan

Posttop
Ivan PedruzziSubject: XSLT Extension Functions & Xalan
Author: Ivan Pedruzzi
Date: 18 Dec 2006 06:09 PM

Script extensions are not part of the XSLT standard, some XSLT processor support them other don't.

In order to run Java Script extensions you will need to add the script engine Java library and the Bean Scripting Framework to the CLASSPATH.

Can you determine which XSLT processor Siebel uses?
If the processor is not XalanJ, you are out of luck.

Do you really need to call an extension?
Which task you are not able to do in XSLT?

Ivan Pedruzzi
Stylus Studio Team

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.