xsl-list
[Top] [All Lists]

Re: XSL java extension newbie question

2003-10-02 14:28:50
Thanks for the info...

...what i am trying to do down below might look strange, but actually i am trying to use XSL to convert XML into java objects - and am not really interested in the output from xsl. We have a case here where there can be multiple xml documents conforming to different schemas but each representing the same information. And we dont want to write a different xml to (our standard) java object parser for each schema. The idea was to write a parser that used an xsl corresponding to a schema and that way the next time we find a new schema, all we do is write a corresponding xsl. We got this going by calling the common set of functions in a java class from the correct places in the xsl. But in the first pass we had all the functions static which meant the parser had to be synchronized so 2 trees could not be broken down at the same time. we were hoping by giving each xsl transformer its own object of the class that provides the set of functions, we could achieve multiple tasks at once...that was the whole idea behind trying the example i was trying below.

i find examples where objects are created inside an xsl stylesheet and are gone when the transformation is done....i want one where the java class invoking the transform and the xsl can share a non-static object

thanks.


From: Tim Meals <tmeals(_at_)attbi(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] XSL java extension newbie question
Date: Thu, 02 Oct 2003 13:26:08 -0700

Sivaram --

See inline comments ...

On Thu, 2003-10-02 at 12:38, sivaram g wrote:
> Hi,
>
> I am trying to write a simple test xsl file using java extensions. I have a
> simple xml file that says
>
> <items>
>    <item>
>       <name>a</name>
>    </item>
>    <item>
>       <name>b</name>
>    </item>
> </item>
>
> I want to use xsl to gather all the item names into a class called MyTest
>
> so i have MyTest.java as
>
> public class MyTest
> {
>    int itemsCount=0;
>    public void foundItem()
>    {
>       itemsCount++;
>    }
> }
>

Have you visited
http://xml.apache.org/xalan-j/extensions.html#java-namespace already?
They have several useful examples on how to write an extension class.
The easiest thing to do would make itemsCount and foundItem both static,
being as the class doesn't really exhibit any behavior like an object --
it's just a static counter.  (Realize, of course, you can do the same
thing with <xsl:value-of select="count(/items/item/name)"/>, which will
give you a count of all /items/item/name nodes.)

> I want to create an object of this class and pass to an xsl stylesheet so it > can update it when the transformation is being done. And I cant find a way
> to get the stylesheet to access the object i have passed in. it seems to
> create a new object of type MyTest and update it. A lot of examples i've
> seen of passing objects as parameters into XSLs only seem to print it out in
> the xsl...how do i update the passed in parameter in the xsl.
>
> my main reads...
> public static void main (String[]args)
>   {
>     String xmlFile = "input.xml";
>     String xslFile = "input.xsl";
>     MyTransformer myt;
>     Source xml;
>     Source xsl;
>     Result result = new StreamResult (System.out);
>     HashMap params = new HashMap ();
>
>     MyTest x=new MyTest();
>
>     try
>     {
>       myt = new MyTransformer ();
>       xml = myt.SourceFromFilename (xmlFile);
>       xsl = myt.SourceFromFilename (xslFile);
>       params.put ("changeme", x);
>       myt.transform (xml, xsl, result, params);
>       System.out.println("%%%%%%%%%%%\n"+x.itemsCount);
>     } catch (Exception e)
>     {
>       System.out.println ("Exception: " + e.getMessage ());
>     }
>
> and the xsl reads as
> <?xml version="1.0"  ?>
>
> <xsl:stylesheet version="1.0"
>   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>   xmlns:xalan="http://xml.apache.org/xalan";
>   xmlns:mytest="MyTest"
>   extension-element-prefixes="mytest">
>   <xsl:param name="changeme" />
>   <xsl:template match="/">
>         <!-- this does print the object as in main -->
>         <xsl:value-of select="$changeme"/>
>         <xsl:apply-templates select="items">
>           <xsl:with-param name="changeme"  select="$changeme" />
>         </xsl:apply-templates>
>   </xsl:template>
>   <xsl:template match="items">

According to the above <xsl:apply-templates>, you should have
<xsl:param> defined here too.  I imagine Xalan isn't complaining because
changeme is a global parameter to the stylesheet.  Your <xsl:with-param>
line isn't really necessary.

>       <xsl:apply-templates select="item">
>       </xsl:apply-templates>
>   </xsl:template>
>   <xsl:template match="item">
>       <xsl:value-of select="mytest:foundItem()"/>

In your MyTest class, foundItem returns nothing.  I'm not sure what
you're trying to accomplish here.

>   </xsl:template>
>
> </xsl:stylesheet>
>
>
> please point to a doc/example which shows how to use the object passed into
> the xsl.

Please see the Xalan link above.  I think you'll find everything you
need there.  Also, take a look in your Xalan distribution.  There's a
samples/extensions directory with source code.

>
> thanks
> sivaram
>
> _________________________________________________________________
> Share your photos without swamping your Inbox. Get Hotmail Extra Storage
> today! http://join.msn.com/?PAGE=features/es
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
--
Tim Meals <tmeals(_at_)attbi(_dot_)com>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


_________________________________________________________________
Share your photos without swamping your Inbox. Get Hotmail Extra Storage today! http://join.msn.com/?PAGE=features/es


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>