Having problems with extension function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Having problems with extension function

Yeliz Yesilada
Hi All,

I am trying to create a Java extension function (which accepts a  
string and returns a Node) and call it from an XSLT template. I have  
managed to successfully do that before with Saxon but now with my  
very simple example I am having problems and not sure why.

I am using Saxon 8.4 and JDK 1.4.2-50.

My stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">
<xsl:output method="html" version="4.0"/>

<xsl:variable name="genericLinker" select="genericLinker:new()"  
xmlns:genericLinker="java:GenericLinker"/>

<xsl:template match="text()">
   <xsl:copy-of select="genericLinker:addLinks($genericLinker,.)"  
xmlns:genericLinker="java:GenericLinker"/>
</xsl:template>
</xsl:stylesheet>

My GenericLinker java class:
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class GenericLinker{
     public GenericLinker (){
     }

     public Element test(String a){
        Element returnNode=null;
         try{
             org.w3c.dom.Document doc =  
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
             returnNode = doc.createElement("div");
             returnNode.appendChild(doc.createTextNode("test"));
         }
         catch(Exception e ){
         }
         return returnNode;
     }
}

But applying the stylesheet above to a test document returns  
something like:
&lt;div&gt;test&lt;/div&gt;

I was hoping to get back a proper <div> node, instead of the returned  
result. Can anybody spot an error either with my stylesheet or the  
Java code?

Regards,
Yeliz
--------------------------------------------------------
School of Computer Science
The University of Manchester
Manchester
M13 9PL
--------------------------------------------------------




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help
Reply | Threaded
Open this post in threaded view
|

RE: Having problems with extension function

Michael Kay
I suspect you haven't got the DOM support module saxon8-dom.jar on your
classpath. Because of incompatible changes in the DOM between JDK 1.4 and
JDK 1.5, Saxon now recognizes DOM classes only if this module is present.

I'd also suggest moving to Saxon 8.6.

Michael Kay
http://www.saxonica.com/

>
> I am trying to create a Java extension function (which accepts a  
> string and returns a Node) and call it from an XSLT template. I have  
> managed to successfully do that before with Saxon but now with my  
> very simple example I am having problems and not sure why.
>
> I am using Saxon 8.4 and JDK 1.4.2-50.
>
> My stylesheet:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                  version="1.0">
> <xsl:output method="html" version="4.0"/>
>
> <xsl:variable name="genericLinker" select="genericLinker:new()"  
> xmlns:genericLinker="java:GenericLinker"/>
>
> <xsl:template match="text()">
>    <xsl:copy-of select="genericLinker:addLinks($genericLinker,.)"  
> xmlns:genericLinker="java:GenericLinker"/>
> </xsl:template>
> </xsl:stylesheet>
>
> My GenericLinker java class:
> import org.w3c.dom.Element;
> import org.w3c.dom.Node;
> import org.w3c.dom.Document;
> import javax.xml.parsers.DocumentBuilder;
> import javax.xml.parsers.DocumentBuilderFactory;
>
> public class GenericLinker{
>      public GenericLinker (){
>      }
>
>      public Element test(String a){
>         Element returnNode=null;
>          try{
>              org.w3c.dom.Document doc =  
> DocumentBuilderFactory.newInstance().newDocumentBuilder().newD
> ocument();
>              returnNode = doc.createElement("div");
>              returnNode.appendChild(doc.createTextNode("test"));
>          }
>          catch(Exception e ){
>          }
>          return returnNode;
>      }
> }
>
> But applying the stylesheet above to a test document returns  
> something like:
> &lt;div&gt;test&lt;/div&gt;
>
> I was hoping to get back a proper <div> node, instead of the
> returned  
> result. Can anybody spot an error either with my stylesheet or the  
> Java code?
>
> Regards,
> Yeliz
> --------------------------------------------------------
> School of Computer Science
> The University of Manchester
> Manchester
> M13 9PL
> --------------------------------------------------------
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep
> through log files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  
> DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> saxon-help mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help
>




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help
Reply | Threaded
Open this post in threaded view
|

Re: Having problems with extension function

Yeliz Yesilada
In reply to this post by Yeliz Yesilada
On 24 Nov 2005, at 09:24, Michael Kay wrote:

> I suspect you haven't got the DOM support module saxon8-dom.jar on  
> your
> classpath. Because of incompatible changes in the DOM between JDK  
> 1.4 and
> JDK 1.5, Saxon now recognizes DOM classes only if this module is  
> present.

Thanks for your quick reply. This solved my problem.

I realised that my command line script to run saxon Transform was  
only addressing to saxon8.jar and even though I had saxon8-dom in my  
generic classpath, I guess this was overriding it.

> I'd also suggest moving to Saxon 8.6.
>
I will try to download and use this new version.
> Michael Kay
> http://www.saxonica.com/
>
By the way, your books are great.

Regards,
Yeliz.


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help