You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Brad Cox <bc...@virtualschool.edu> on 2002/03/06 23:03:39 UTC

Extension: how to return java values from templates?

Please pardon the newbie questions. Still rather new to this.

This cut down example intends to compute instances of XMLFile in the 
second template to be returned to the first template. However this 
shows they being returned as #NODESETs, not java instances. How do I 
get a plain old java-style return value from a xslt template?

Exception:javax.xml.transform.TransformerException: 
java.lang.NoSuchMethodException: For extension function, could not 
find method 
edu.virtualschool.jile.ctlr.SiteMap.addController([ExpressionContext,] 
#UNKNOWN (java.util.ArrayList), #NODESET, #NODESET).

<xsl:template match="ile:page">
	<xsl:variable name="content" select="ile:content"/>
	<xsl:variable name="template" select="ile:template"/>
	<xsl:variable name="this" select="java:addController(
		$sitemap,
		$content,
		$template
	)"/>
	<xsl:apply-templates select="ile:page"/>
</xsl:template>

<xsl:template match="ile:content|ile:template">
	<xsl:variable name="xsl" select="ile:xsl"/>
	<xsl:variable name="xml" select="ile:xml"/>
	<xsl:value-of select="java:edu.virtualschool.jile.ctlr.XMLFile.new(
		string($xsl),
		string($xml)
)"/>
</xsl:template>

PS: ile is my project's namespace.
-- 
Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
o For industrial age goods there were checks and credit cards.
    For everything else there is http://virtualschool.edu/mybank
o Java Interactive Learning Environment http://virtualschool.edu/jile
o Java Web Application Architecture: http://virtualschool.edu/jwaa



Re: Extension: how to return java values from templates?

Posted by Brad Cox <bc...@virtualschool.edu>.
To rephrase my question in perhaps more familiar terms: how does one 
accomplish java-style subroutine return semantics in xslt/extensions. 
This table reflects my (possibly incorrect) understanding of how 
templates relate

	xslt				java
	call-template			subroutine()
	with-parameter			subroutine(argument)
	value-of			return ..
	apply-templates			no direct equivalent

That said, apparently some kind of conversion is imposed by xslt that 
coverts the return value from XMLFile to NODESET. My question is, how 
can this be avoided. Or is the above mental model the problem and 
things don't work this way at all?

At 5:03 PM -0500 3/6/02, Brad Cox wrote:
>Please pardon the newbie questions. Still rather new to this.
>
>This cut down example intends to compute instances of XMLFile in the 
>second template to be returned to the first template. However this 
>shows they being returned as #NODESETs, not java instances. How do I 
>get a plain old java-style return value from a xslt template?
>
>Exception:javax.xml.transform.TransformerException: 
>java.lang.NoSuchMethodException: For extension function, could not 
>find method 
>edu.virtualschool.jile.ctlr.SiteMap.addController([ExpressionContext,] 
>#UNKNOWN (java.util.ArrayList), #NODESET, #NODESET).
>
><xsl:template match="ile:page">
>	<xsl:variable name="content" select="ile:content"/>
>	<xsl:variable name="template" select="ile:template"/>
>	<xsl:variable name="this" select="java:addController(
>		$sitemap,
>		$content,
>		$template
>	)"/>
>	<xsl:apply-templates select="ile:page"/>
></xsl:template>
>
><xsl:template match="ile:content|ile:template">
>	<xsl:variable name="xsl" select="ile:xsl"/>
>	<xsl:variable name="xml" select="ile:xml"/>
>	<xsl:value-of select="java:edu.virtualschool.jile.ctlr.XMLFile.new(
>		string($xsl),
>		string($xml)
>)"/>
></xsl:template>
>
>PS: ile is my project's namespace.
>--
>Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
>o For industrial age goods there were checks and credit cards.
>    For everything else there is http://virtualschool.edu/mybank
>o Java Interactive Learning Environment http://virtualschool.edu/jile
>o Java Web Application Architecture: http://virtualschool.edu/jwaa


-- 
Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
o For industrial age goods there were checks and credit cards.
    For everything else there is http://virtualschool.edu/mybank
o Java Interactive Learning Environment http://virtualschool.edu/jile
o Java Web Application Architecture: http://virtualschool.edu/jwaa


Mixing SAXSource and StreamResult?

Posted by Thad Humphries <th...@mindwrap.com>.
I have an XML file and a stylesheet that work from the command line
using org.apache.xalan.xslt.Process.  Now I'm trying to put do the work
via a JSP and display the results in the browser.  To make things
interesting, my JSP is explicity ignoring the DTD in the XML document
because I can't count on it being around in all cases.  My code looks
like this:

<%!
public class DTDResolver implements EntityResolver
{
    public InputSource resolveEntity( String publicId, String systemId )
    {
        if ( systemId.toLowerCase().endsWith( "dtd" ) )
        {
            return new InputSource( new StringReader( "" ) );
        }
        return null;
    }
};
%>      
<%
    byte [] docBuffer = // my XML document
    byte [] xsltBuffer = // my XXL document
...
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer( new 
        StreamSource( new ByteArrayInputStream( xsltBuffer ) ) );

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating( false );
    XMLReader reader = factory.newSAXParser().getXMLReader();
    reader.setEntityResolver( new DTDResolver() );
    SAXSource source = new SAXSource( reader, 
        new InputSource( new ByteArrayInputStream( docBuffer ) ) );

    transformer.transform( source,
       new StreamResult( out ) );
%>

My problem is that the output is *just* the XML docs' text data with
none of the XSL applied.  I have tested xstlBuffer and it contains my
XSL document.

What am I missing?  Can I mix a SAXSource and a StreamResult in my
transformer.transform()?  I've used Xalan 1 for a long while but just
got startd with Xalan 2 yesterday.  My other JSPs using StreamSource and
StreamResult (and following the example
http://xml.apache.org/xalan-j/usagepatterns.html#basic) works but this
one doesn't.

Thanks!

-- 
------------------------------------------------------------------------
Thad Humphries              "...no religious test shall ever be required
Web Development Manager      as a qualification to any office or public
Phone: 540/675-3015, x225    trust under the United States." -Article VI


Re: Extension: how to return java values from templates?

Posted by "Frank E. Weiss" <fr...@well.com>.
Oh man, am I ever embarassed. I just realized what's wrong with this. I mistakenly thought that the select attributes in
the xsl:variable elements in the first xsl:template would invoke the second xsl:template. Nope. These select attributes
merely get ile:content and ile:template nodes from the input document and assign them to variables. I was confusing them
with the select attribute of an xsl:apply-templates element. Only in that case is a template from the stylesheet applied
to the selected nodes from the input document. So your description "compute instances of XMLFile in the second template
to be returned to the first template" does not describe the behavior of the stylesheet snippet you gave in the example.

Brad Cox wrote:

> Please pardon the newbie questions. Still rather new to this.
>
> This cut down example intends to compute instances of XMLFile in the
> second template to be returned to the first template. However this
> shows they being returned as #NODESETs, not java instances. How do I
> get a plain old java-style return value from a xslt template?
>
> Exception:javax.xml.transform.TransformerException:
> java.lang.NoSuchMethodException: For extension function, could not
> find method
> edu.virtualschool.jile.ctlr.SiteMap.addController([ExpressionContext,]
> #UNKNOWN (java.util.ArrayList), #NODESET, #NODESET).
>
> <xsl:template match="ile:page">
>         <xsl:variable name="content" select="ile:content"/>
>         <xsl:variable name="template" select="ile:template"/>
>         <xsl:variable name="this" select="java:addController(
>                 $sitemap,
>                 $content,
>                 $template
>         )"/>
>         <xsl:apply-templates select="ile:page"/>
> </xsl:template>
>
> <xsl:template match="ile:content|ile:template">
>         <xsl:variable name="xsl" select="ile:xsl"/>
>         <xsl:variable name="xml" select="ile:xml"/>
>         <xsl:value-of select="java:edu.virtualschool.jile.ctlr.XMLFile.new(
>                 string($xsl),
>                 string($xml)
> )"/>
> </xsl:template>
>
> PS: ile is my project's namespace.
> --
> Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
> o For industrial age goods there were checks and credit cards.
>     For everything else there is http://virtualschool.edu/mybank
> o Java Interactive Learning Environment http://virtualschool.edu/jile
> o Java Web Application Architecture: http://virtualschool.edu/jwaa


Re: Extension: how to return java values from templates?

Posted by Brad Cox <bc...@virtualschool.edu>.
I'm still stuck on this one. Some further details I accidentally 
omitted are that $sitemap is passed in as a parameter. Its an 
instance of the java class $SiteMap. Also the #UNKNOWN 
(java.util.ArrayList) refers to a Java ArrayList instance that is 
constructed in the first template and passed as the second argument 
to addController; I omitted it in stripping this example down. The 
question concerns the last two arguments, $content and $template, 
which are instantiated in the second template. The question is, how 
do I return them from of the second template so that they remain 
XMLFile instances. The value-of statements converts them to #NODESET 
which is not what I want.

At 5:03 PM -0500 3/6/02, Brad Cox wrote:
>Please pardon the newbie questions. Still rather new to this.
>
>This cut down example intends to compute instances of XMLFile in the 
>second template to be returned to the first template. However this 
>shows they being returned as #NODESETs, not java instances. How do I 
>get a plain old java-style return value from a xslt template?
>
>Exception:javax.xml.transform.TransformerException: 
>java.lang.NoSuchMethodException: For extension function, could not 
>find method 
>edu.virtualschool.jile.ctlr.SiteMap.addController([ExpressionContext,] 
>#UNKNOWN (java.util.ArrayList), #NODESET, #NODESET).
>
><xsl:template match="ile:page">
>	<xsl:variable name="content" select="ile:content"/>
>	<xsl:variable name="template" select="ile:template"/>
>	<xsl:variable name="this" select="java:addController(
>		$sitemap,
>		$content,
>		$template
>	)"/>
>	<xsl:apply-templates select="ile:page"/>
></xsl:template>
>
><xsl:template match="ile:content|ile:template">
>	<xsl:variable name="xsl" select="ile:xsl"/>
>	<xsl:variable name="xml" select="ile:xml"/>
>	<xsl:value-of select="java:edu.virtualschool.jile.ctlr.XMLFile.new(
>		string($xsl),
>		string($xml)
>)"/>
></xsl:template>
>
>PS: ile is my project's namespace.
>--
>Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
>o For industrial age goods there were checks and credit cards.
>    For everything else there is http://virtualschool.edu/mybank
>o Java Interactive Learning Environment http://virtualschool.edu/jile
>o Java Web Application Architecture: http://virtualschool.edu/jwaa

-- 
Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
o For industrial age goods there were checks and credit cards.
    For everything else there is http://virtualschool.edu/mybank
o Java Interactive Learning Environment http://virtualschool.edu/jile
o Java Web Application Architecture: http://virtualschool.edu/jwaa