You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Glenn Barney <gb...@hotmail.com> on 2002/08/06 20:42:21 UTC

Using a local JSP String in a taglib var for xsl transforms

Hi all,

I'm new to this taglibs stuff so any help would be greatly appreciated.  I 
searched all over this list but found no answer to my question.  My goal is 
to transform a xml doucment, stored as a string, using an xsl file and xsl 
taglibs.

I have a database full of xml documents.  I am pulling each document out in 
a list and saving the document into a Java string called message. IE i have
	    <%! String message; %> and later...
	<%message = rs.getString("message"); %>

I have two test files working correctly when I call these lines:

	<c:import url="/xml/tester.xml" var="xml" />
	<c:import url="/xml/real.xsl" var="xslt" />
	<x:transform xml="${xml}" xslt="${xslt}" />

This works great and i get the output i'd exepected.  But I can't for the 
life of me, get the String "message" to be parsed by xslt!  As per searching 
the newsgroup I tried (sometime randomly):

<x:transform xml="${message}" xslt="${xslt}" />
<x:transform xml="${session.message}" xslt="${xslt}" />
<x:transform xml="${param.message}" xslt="${xslt}" />
<x:transform xml="<%= message =>" xslt="${xslt}" />

But nothing works.  I tried each of these above methods while message had an 
xml document in it and again when it had a pointer to that tester.xml file 
just to see if I could get anything working but I can't.  I am running 
tomcat 4.0, got everything set up ok, i think.  Thanks a lot for your help.

-Glenn




_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Using a local JSP String in a taglib var for xsl transforms

Posted by Shawn Bayern <ba...@essentially.net>.
On Tue, 6 Aug 2002, Glenn Barney wrote:

> I'm new to this taglibs stuff so any help would be greatly
> appreciated.  I searched all over this list but found no answer to my
> question.  My goal is to transform a xml doucment, stored as a string,
> using an xsl file and xsl taglibs.

Sounds like a good start!

> I have a database full of xml documents.  I am pulling each document out in 
> a list and saving the document into a Java string called message. IE i have
> 	    <%! String message; %> and later...

Just in case it's useful, it's worth noting that this might not be what
you want.  This declaration associates 'message' with an instance of your
JSP page's implementation class.  Unless your JSP page is marked as single
threaded, the 'message' variable may be overwritten if multiple threads
are running through the underlying servlet's methods at the same time.  
Though I can't say if it's a real problem without seeing the rest of your
code, you probably want to declare 'message' in a regular scriptlet block
-- that is, "<%" instead of "<%!".  Don't be fooled by the fact that "<%!"
are called "scripting declarations"; they are indeed useful only for some
kinds of declarations, but not all declarations should use such blocks.  
(Only those that are truly meant to be instance-wide variables -- or, say,
method declarations -- should use such blocks.)

> 	<%message = rs.getString("message"); %>
> 
> I have two test files working correctly when I call these lines:
> 
> 	<c:import url="/xml/tester.xml" var="xml" />
> 	<c:import url="/xml/real.xsl" var="xslt" />
> 	<x:transform xml="${xml}" xslt="${xslt}" />
> 
> This works great and i get the output i'd exepected.  But I can't for the 
> life of me, get the String "message" to be parsed by xslt!  As per searching 
> the newsgroup I tried (sometime randomly):
> 
> <x:transform xml="${message}" xslt="${xslt}" />
> <x:transform xml="${session.message}" xslt="${xslt}" />
> <x:transform xml="${param.message}" xslt="${xslt}" />
> <x:transform xml="<%= message =>" xslt="${xslt}" />

The problem is that there's actually no way for a JSP tag to read a local
Java variable.  In short, this is because in the general case, a tag
invocation turns into Java method invocations, and local variables in a
caller are not accessible to methods it calls.  That is, consider

  String message;
  foo(bar);

foo() has no access to message.  In tags' case, foo() corresponds to
methods like doStartTag(), doEndTag(), and so forth.

That's just background.  The answer to your specific question is that you
need to turn the local variable into a scoped variable, which you'll
probably want to do manually from a scriptlet:

  <% pageContext.setAttribute("message", message); %>

In fact, you don't need the local variable at all; you can simply say

  <% pageContest.setAttribute("message", rs.getString(...)); %>

Hope that helps,

-- 
Shawn Bayern
"JSTL in Action"   http://www.jstlbook.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>