Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing an XPath Param value

 
Mark Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Mark Young
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic