Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 1st, 2010, 04:05 AM
Authorized User
 
Join Date: Nov 2010
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing normal Java Objects to Transformer.setParameters

Hi,
Can one pass plain java objects to setParameters and use them in a stylesheet without having them transformed into a string?

I don't know how to define the parameter in the stylesheet in order to be able to access as a normal java object. Is it possible?

The only examples I saw were always using Strings, Double, Integers or values. Can we also pass a java object like com.myown.GreatJavaObject?

Thanks
 
Old December 1st, 2010, 05:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The JAXP interface doesn't define what kinds of Java object you can pass to setParameter(), or how such objects are mapped to XPath values. It depends which implementation of JAXP you are using.

If you are using Saxon, the rules can be found at http://www.saxonica.com/documentatio...ion-result.xml (the rules are expressed in terms of converting the result of an extension function, but the same rules apply for setParameter()). Note the last rule ("If none of the above, it's passed as a wrapped Java object") - in such cases, the only thing your stylesheet can usefully do with the value is to pass it to a Java extension function.

If you are using Xalan, the rules will be different.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 1st, 2010, 06:34 AM
Authorized User
 
Join Date: Nov 2010
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.
As I said I am using saxon-EE, and today I am using in my stylesheet something similar to this:

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
	xmlns:abc="java:com.abc.MyClass"
>
	
	<xsl:output method="xml" indent="yes" encoding='UTF-8' />
	<xsl:variable name="myObject" select="abc:new()" />

...

	<xsl:template match="i18n_translate">
		<xsl:value-of select="abc:myMethod($myObject, 'aparameter')" />
	</xsl:template>

</xsl:stylesheet>
So my question is really, is it possible to instantiate the object myObject in my Java program and pass it as a parameter to my stylesheet with the transformer's method setParameters, instead of instantiating in my stylesheet where my object is used as a variable? In code I would like my solution to look like this:

My Java program
Code:
MyClass myObject = new MyClass();
transformer.setParameters("myObject", myObject);
In my Stylesheet it would be something like this:

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
	xmlns:abc="java:com.abc.MyClass"
>
	
	<xsl:output method="xml" indent="yes" encoding='UTF-8' />
	<xsl:param name="myObject" />

...

	<xsl:template match="i18n_translate">
		<xsl:value-of select="abc:myMethod($myObject, 'aparameter')" />
	</xsl:template>

</xsl:stylesheet>
Could something similar to this make sence?
 
Old December 1st, 2010, 06:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

a) If you look at your original post you didn't say you were using Saxon at all.

b) Have you tried the code you posted? Seems the quickest and easiest way to work out if you can do it is to try and see if it works. I would suspect from what Michael said that it will work exactly as you expect.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 2nd, 2010, 06:18 AM
Authorized User
 
Join Date: Nov 2010
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again for your answer.
Sorry if I forgot to mention Saxon.

Thanks for giving me inspiration to continue my quest. You are right my code is working. I had forgotten actually to place, in the property list, the object I wanted to pass!!! Very embarrassing, I was making loads of tests and I have forgotten the most important. I had gotten confused when I saw the error message:

Code:
net.sf.saxon.trans.XPathException: Cannot convert from xs:string to myObject
and really got confused. That is when I thought it was not accepting objects. Stupid thought....

Anyhow passing objects to stylesheet through properties and referring the object in the stylesheet works like a charm, and with this in hand I can solve all problems.

I will solve one of the problems I had posted (Fetching variable from an XSLT transformation) with this technique.

One last thing. Saxon is REALLY GREAT. I like it a lot, fast and as you guys told me before much better than the old XSLT V 1.0 I use to work with. So thanks a lot.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing / Referencing Objects echovue Javascript 2 September 28th, 2007 09:28 AM
XSLT 2.0 Transformer for Java albinjoseph XSLT 1 March 2nd, 2007 04:05 AM
passing jagged array of objects nutrino Beginning VB 6 0 January 27th, 2006 11:58 AM
Passing Objects by value Ibn_Aziz C# 7 December 30th, 2003 06:07 AM
Passing Objects nayan_k Classic ASP Basics 1 September 16th, 2003 01:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.