Custom XPath function with JDom - convert exception

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

Custom XPath function with JDom - convert exception

Wojciech Gdela
Hello,

I'm creating a custom XPath function in Java. I've added to my stylesheet:

xmlns:fx="java:com.example.XPathFunctions"

And I'm invoking my function like this:

<xsl:template match="/">
   <foo>
     <xsl:value-of select="fx:an-function(.)"/>
   </foo
</xsl:template>

I find Saxon object model a little bit difficult to use, so I've written
this funcion using JDom (simplified):

class XPathFunctions {
   public static void anFunction(org.jdom.Element element) {
     System.out.println(element.getName());
   }
}

But during execution I'm getting this exception:

Error in call to extension function {public static void
pl.rynekmedyczny.catalogue.XPathFunctions.anFunction(org.jdom.Element)}:
Cannot convert supplied XPath value to the required type for the
extension function
at
net.sf.saxon.functions.ExtensionFunctionCall.iterate(ExtensionFunctionCall.java:116)

I'm using Saxon 8.5.1 (saxon8.jar, saxon8-dom.jar, saxon8-jdom.jar) and
JDom 1.0, and everything runs inside Cocoon 2.1.7.

What did I wrong?

--
Pozdrawiam,
Wojciech Gdela.




-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help
Reply | Threaded
Open this post in threaded view
|

RE: Custom XPath function with JDom - convert exception

Michael Kay
In your match="/" template the context node "." will be a document node, not
an element node, so you can't pass it to a function that expects an element.

However, I'm not sure this will fix the problem: as far as I can see, the
code to recognize org.jdom.Element on extension function calls isn't
actually present in the JDOM interface module. A far bit of work was done to
get this working for XOM, but the same hasn't been done for JDOM.

You should be able to get it working, however, by declaring the expected
type of the argument as Object or List: you will then be passed an ArrayList
containing a single item, which is the JDOM node.

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

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of
> Wojciech Gdela
> Sent: 22 October 2005 13:18
> To: [hidden email]
> Subject: [saxon] Custom XPath function with JDom - convert exception
>
> Hello,
>
> I'm creating a custom XPath function in Java. I've added to
> my stylesheet:
>
> xmlns:fx="java:com.example.XPathFunctions"
>
> And I'm invoking my function like this:
>
> <xsl:template match="/">
>    <foo>
>      <xsl:value-of select="fx:an-function(.)"/>
>    </foo
> </xsl:template>
>
> I find Saxon object model a little bit difficult to use, so
> I've written
> this funcion using JDom (simplified):
>
> class XPathFunctions {
>    public static void anFunction(org.jdom.Element element) {
>      System.out.println(element.getName());
>    }
> }
>
> But during execution I'm getting this exception:
>
> Error in call to extension function {public static void
> pl.rynekmedyczny.catalogue.XPathFunctions.anFunction(org.jdom.
> Element)}:
> Cannot convert supplied XPath value to the required type for the
> extension function
> at
> net.sf.saxon.functions.ExtensionFunctionCall.iterate(Extension
> FunctionCall.java:116)
>
> I'm using Saxon 8.5.1 (saxon8.jar, saxon8-dom.jar,
> saxon8-jdom.jar) and
> JDom 1.0, and everything runs inside Cocoon 2.1.7.
>
> What did I wrong?
>
> --
> Pozdrawiam,
> Wojciech Gdela.
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by:
> Power Architecture Resource Center: Free content, downloads,
> discussions,
> and more. http://solutions.newsforge.com/ibmarch.tmpl
> _______________________________________________
> saxon-help mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help
>




-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help
Reply | Threaded
Open this post in threaded view
|

Re: Custom XPath function with JDom - convert exception

Wojciech Gdela
Hello,

> In your match="/" template the context node "." will be a document node, not
> an element node, so you can't pass it to a function that expects an element.

In my code it is an element node, I've simplified it for purpose of this
mail.

> You should be able to get it working, however, by declaring the expected
> type of the argument as Object or List: you will then be passed an ArrayList
> containing a single item, which is the JDOM node.

I've used:

public static void anFunction(Object obj) {
   System.out.println(obj.getClass());
   List lst = (List) obj;
   Iterator it = lst.iterator();
   while (it.hasNext()) {
     System.out.println(it.next().getClass());
   }
}

And I've got this in output:

class java.util.ArrayList
class net.sf.saxon.tinytree.TinyElementImpl

I'm confused. Do I have to parse the file using JDom parser to be able
to use custom XPath functions with JDom?

--
Pozdrawiam,
Wojciech Gdela.


-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help
Reply | Threaded
Open this post in threaded view
|

RE: Custom XPath function with JDom - convert exception

Michael Kay
>
> I'm confused. Do I have to parse the file using JDom parser
> to be able
> to use custom XPath functions with JDom?

Yes, you do. If you supply JDOM nodes as input, then you can use JDOM
interfaces to process the nodes in an extension function. If you supply the
input in a different format, then Saxon isn't going to convert the tree to a
JDOM tree just because your extension functions expect it that way.

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




-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help