This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes Passing an XPath Param value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Passing an XPath Param value" Watch "Passing an XPath Param value" New topic
Author

Passing an XPath Param value

Mark Young
Greenhorn

Joined: Oct 22, 2003
Posts: 5
Hi all,
First off, great site. Really useful.
At the risk of sounding completely stupid, how can I pass an XPath parameter from Java to my XSLT page?
Essentially, this is the param value I want:

Using setParameter, I'm wanting to pass in this value, however xalan converts any strings directly to strings. What objects do I need to use?
Thanks a heap,
Mark
[ October 22, 2003: Message edited by: Mark Young ]
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Originally posted by Mark Young:
Hi all,
how can I pass an XPath parameter from Java to my XSLT page?
Essentially, this is the param value I want:

Using setParameter, I'm wanting to pass in this value, however xalan converts any strings directly to strings. What objects do I need to use?

If you are using stylesheet embbeded in servlets, then please have a look at this example provided by Apache XALAN


Spritle Software Blogs
Mark Young
Greenhorn

Joined: Oct 22, 2003
Posts: 5
Thanks Balaji,
I had already seen that example that you pointed me to, however I've realised now that my problem was with the xsl parameters, not passing them from Java.
Just another xsl newbie baffled by its xpath and variable use.
All working now thanks
Mark
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Mark:
Can you provide more details? I am interested to know the problem. I don't quite understand how you are using the param in your xsl context.
Regards,


Tony Yan<br /> <br />IBM Certified Developer XML and Related Technology<br />Sun Certified Web Component Developer For J2EE Platform<br />Sun Certified Programmer For Java 2 Platform
Mark Young
Greenhorn

Joined: Oct 22, 2003
Posts: 5
Hi Tony,
Yes I think the problem was that I wasn't sure how I was using the param in my context either. I'm a newbie at xml/xsl, but have been using Java and other languages for a long time.
Basically, what I was attempting to do was to replicate this static code using a param:

I had assumed I would be able to substitute a param for subField/text like this:

but couldn't get it to work.
I am now using

which works correctly.
I had thought it was the way Java passed the strings to the stylesheet, but it was my faulty xsl understanding.
If you have any better ways of doing things I would be keen to know.
Regards,
Mark
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Originally posted by Mark Young:

I had assumed I would be able to substitute a param for subField/text like this:

but couldn't get it to work.
I am now using



Mark: I am actually very confused. Looks like you get the same results for the checked string value with *[name()=$param1] and subField/text(), since you said it worked. I don't see why that would work, at least in general, unless the data (xml) happens to be that way. Without the XML, can't say much about it. You should use variable instead of param so there is no default issues and should pick up the right nodes. BTW, text() gives you child text nodes, not text string per se. So checking whether that contains certain string is not very intuitive to me, though may "work" in instances.
Regards,
Mark Young
Greenhorn

Joined: Oct 22, 2003
Posts: 5
Tony,
You'll have to forgive me for being unclear - its currently almost 3am and I've barely slept all week. I thought in the interests of quickly clarifying this up and learning from it, I'd post all my xsl and xml. I'm not going to change this current implementation, as it works, however I would appreciate learning for next time.
This should be reasonably self-explanatory. I'm calling the xsl from Java, and modifying the params to search or sort for different things.
XSL Stylesheet:

Here is the xml DTD:

(Sorry about the long lines). If the DTD and element names seem contrived its because they are. It's taken from a large JSP site for a university assignment.
This is not an urgent matter, just something I'd like to learn from.
Many thanks,
Mark
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Sorry to say that I am confused more! *[name()=$searchKey]/text() and $searchList both should be empty string to me, given this DTD and xsl. So the if test will be true all the time?
Mark Young
Greenhorn

Joined: Oct 22, 2003
Posts: 5
Tony,
Those parameters are all being set by Java before I use the transformation using setParameter.
So the parameters will all hold names of elements or strings.
searchList is the text to be searched for inside the element searchKey.
sortKey is the element to sort by, and searchMatch is a variable saying what type of search to perform. Presently it is working how I intended - ie searching is performed on the xml, and a sorted list returned.
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
I see. That's why param is used here. I think if it works, it works. Just xpath issues here. Either way would work fine. Performance is not hindered anyways. I have learned useful things from your xsl.
Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing an XPath Param value