Re: [xsl] Accessing a Java Object after instatiating as a variable

Subject: Re: [xsl] Accessing a Java Object after instatiating as a variable
From: cfisher@xxxxxxx
Date: Fri, 17 Dec 2004 08:50:05 -0600
             Marian Olteanu                                                
             <mou_softwin@yaho                                             
             o.com>                                                     To 
                                       xsl-list@xxxxxxxxxxxxxxxxxxxxxx     
             12/16/2004 03:55                                           cc 
             PM                                                            
                                                                   Subject 
                                       --- Potential SPAM ---: Re: [xsl]   
             Please respond to         Accessing a Java Object after       
             xsl-list@xxxxxxxx         instatiating as a variable          
              lberrytech.com                                               
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           





Thank you for replying.  There are many ways to do the same thing and at
the risk of goring someone's pet bovine, inter-platform compatibility is at
the end of the priority list for the US Government.  Because of software
restrictions that I won't even begin to explain, what we have is Xalan and
FOP, period.  Extensions are an integral part of most XSL Engines and to be
able to make use of them, opens the door to solving many problems that
could occur in the future.  Formatting a date is a small exercise and as
another helpful soul (omprakash visvanathan) pointed out to me on the yahoo
forum, the instructions do exist at:
http://xml.apache.org/xalan-j/extensions.html#format-date-stylesheet.  His
suggestion came about 12 hours late since I was able to get an example
working, but not the way I would have liked. Here is what I did:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format";
xmlns:fox="http://xml.apache.org/fop/extensions";
xmlns:xalan="http://xml.apache.org/xslt";
xmlns:Math="xalan://java.lang.Math"
xmlns:date="http://xml.apache.org/xalan/java/java.util.Date";
xmlns:java_lang="http://xml.apache.org/xalan/java/java.lang";
xmlns:java="http://xml.apache.org/xalan/java";
exclude-result-prefixes="date java format java_lang" >



    <xsl:variable name="dNow" select="date:new()"/>
   <xsl:variable name="formatter" select="
java:java.text.SimpleDateFormat.new(' yyyy.MM.dd hh:mm a zz')"/>
     <xsl:variable name="dateToday" select="java:format($formatter, $dNow)
"/>

What is really different is that I am passing the formatter object to the
instance method along with the date.  In the JDK documentation there is no
overloading shown on the format method for this class, but the ancestor has
a method with the overloaded signature.  In the example at
http://xml.apache.org/xalan-j/extensions.html#format-date-stylesheet, there
was the exact same code working with the overloaded method.  I have another
gentleman who understands much more about Java than I do, telling me that
all the above is accomplished with Reflection in Java.  Mmmmmm,
Interesting...

What does it mean?  The result is that I will not be able to instansiate a
variable in the Template and reference methods as: return =
$fomatter.format($dNow).  Some of you XSLT gurus may be shaking your heads
by now, but don't think that I want to change XSLT into something it was
not meant to be, I simply want to know the rules and limits so I can roam
freely without flattening my head too much.

I wish someone could explain succinctly what the component and script
elements are doing under the covers from start to finish.  It would make an
interesting read as far as using extensions goes.

Thanks for your help.

Curtis

Why don't you pass the current date (and all the required formating) as a
parameter to the
stylesheet? This way, you will have a platform compatible stylesheet (it
will work in any
compliant XSLT processor).

--- cfisher@xxxxxxx wrote:

> After reading Michael Kay's Xalan Chapter in the second edition of XSLT,
> backwards and forwards, I have finally been able to instantiate a
> SimpleDateFormat object.  My problem is how to access the object after it
> is in variable form.  Can this be done?  There are component and script
> elements available, however I am unsure as to their final implementation.
>
> Here is a portion of my stylesheet:
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:fo="http://www.w3.org/1999/XSL/Format";
> xmlns:fox="http://xml.apache.org/fop/extensions";
> xmlns:xalan="http://xml.apache.org/xslt";
> xmlns:Math="xalan://java.lang.Math"
> xmlns:date="http://xml.apache.org/xalan/java/java.util.Date";      --->
> Importing the date class into the namespace and declaring a prefix
> xmlns:java_lang="http://xml.apache.org/xalan/java/java.lang";      --->
> Importing the lang package into the namespace and declaring a prefix
> xmlns:format="xalan://java/java.text.SimpleDateFormat">           --->
> Importing the text package into the namespace and declaring a prefix
>
>
>     <xsl:variable name="dNow" select="date:new()"/>
>                ---> Successfully creating a date object
>    <xsl:variable name="formatter" select="format:SimpleDateFormat.new('E
> yyyy.MM.dd hh:mm:ss a zz')"/>   ---> Successfully creating a
> SimpleDateFormat object with my own special format
>      <xsl:variable name="dateToday" select="format:format($dNow)"/>
>           ---> Unsuccessfully creating a formatted date (it has the
> standard formatting applied)
>
> The reason is because the $dateToday variable is created with a
> SimpleDateFormat object that is derived from the standard class and not
the
> class I created with the new('format') string.  Without the formatting
> string applied in the constructor the format looks like this; 12/16/04
> 12:32 PM, which is my local setting.  I don't know how to access the
> instance functions in my created object.
>
> If someone could shed some light on the manner in which the component and
> script elements can be used with Xalan, then this might be the ticket for
> many extensions which would be useful.  I know they can be used, but I
have
> yet to find an example that I can get my mind around...
>
> Thanks,
>
> Curtis Fisher
>
>


=====
Marian
http://www.utdallas.edu/~mgo031000/



__________________________________
Do you Yahoo!?
Yahoo! Mail - 250MB free storage. Do more. Manage less.
http://info.mail.yahoo.com/mail_250

Current Thread