struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ravindran Ramaiah <r...@sengen.com>
Subject RE: Struts - XML - XSL
Date Mon, 24 Sep 2001 15:51:16 GMT
Following are the procedures that can used to apply the style sheet
--------------------------------------------------------------------

Once we have already parsed the XML document, the getDocument method from
the parser instance (e.g. DOMParser) should return  an object of type
Document (which is actually a DOM representation of the XML). This will be
the starting point.

To apply the XSLT transformation, you perform the following steps:

1.  Create a DOMSource object and implicitly cast it to a SAX InputSource
object. The constructor takes in the Document object as a parameter.

InputSource source = new DOMSource(document);

2.  Instantiate a StyleGenerator object and pass the fully-qualified class
names of the XML and XSL parsers you want to use.

StyleGenerator stylegen = new StyleGenerator(Consts.SOURCEDOC_PARSER,
Consts.STYLESHEET_PARSER);

Where:
SOURCEDOC_PARSER		"com.sengen.utils.DOMParserExtension"
STYLESHEET_PARSER		"org.apache.xerces.parsers.SAXParser"

You can plug-in any DOM / SAX compliant parser you like. In this case we are
using two different parsers: a DOM parser for the XML and a SAX parser for
the XSL. The setup does not have to be this way. This is also the reason why
we have to implicitly casting in step one.

3. Lastly, call the StyleGenerator's applyStylesheet method. Originally, it
only deals with filenames but I overloaded it to accomodate InputSource and
streams. The resulting or transformed XML document is either written to a
file or returned as a byte stream.

public void applyStylesheet(String sourceFileName, String styleFileName,
String outputFileName) throws Exception
public ByteArrayOutputStream applyStylesheet(String sourceFileName, String
styleFileName) throws Exception
public void applyStylesheet(InputSource sourceInput, String styleFileName,
String outputFileName) throws Exception
public ByteArrayOutputStream applyStylesheet(InputSource sourceInput, String
styleFileName) throws Exception

Where:
sourceFileName			XML filename
styleFileName			XSL filename **		
outputFileName			filename of the resulting (transformed) XML
sourceInput 			InputSource object created in step 1


** any type of stylesheet like XSL-2-HTML, XSL-2-FO,  XSL-2-whatever


-lines 3 and 4 correspond to steps 1 and 2 above.
-In line 7, he used the fourth version of the applyStylesheet method and
converted the byte array to a
string. This string is now the HTML transformation of the XML document.
	
1.		public String getPresentation() {
2.	
3.			InputSource source = new DOMSource(getDocument());
4.			StyleGenerator stylegen = new
StyleGenerator(KmpPdfgenConsts.SOURCEDOC_PARSER,KmpPdfgenConsts.STYLESHEET_P
ARSER);
5.	
6.			try {
7.				return
stylegen.applyStylesheet(source,getStylesheet()).toString();
8.    		} catch(Exception e){
9.				//
10.			}
11.	
12.			return "";
13.		}



Hope this helps.

Thanks,
Ravi






-----Original Message-----
From: Ted Husted [mailto:husted@apache.org]
Sent: Monday, September 24, 2001 11:33 AM
To: struts-user@jakarta.apache.org
Subject: Re: Struts - XML - XSL


At Jakarta, we use Ant to build our HTML pages from XML and XSL. See the
Struts source distribution for an example. 

I haven't tried it, but I keep thinking it would be interesting to do
the same with JSPs.

This would gives you the flexibility of XML,XLS without changing how you
write your applications, or incurring the overhead of the runtime
transformations.

> Frédéric Houbie - ABSIS-GROUP wrote:
> 
> Hi,
> 
> I'm involved in a project that use struts. I have a question to get
> some advice from you. The application want to separate clearly layout
> from the logic. The team manager want us to use XML, XSL to build html
> pages. But I don't see clearly how to mix all that. I have beans that
> do SQL query and return an iterator with the data, I suppose my
> controller servlet must call the beans and give the jsp access to this
> iterator. My JSP page will dynamically build XML file, but i don't
> know how to call the transformation of the XML data with a XSL file to
> build my final html layout.
> 
> Can you help me ?
> 
> 
> Frédéric Houbie
> Internet Project Manager
> 
> ABSIS-GROUP SA
> Centre Socran
> Av Pré Aily, 8
> B-4031 Angleur
> Tel : +32 4 367 89 64
> Fax : +32 4 367 89 63
> 
> **** DISCLAIMER ****
> 
> "This e-mail and any attachments thereto may contain information which
> is confidential and/or protected by intellectual property rights and
> are intended for the sole use of the recipient(s) named above. Any use
> of the information contained herein (including, but not limited to,
> total or partial reproduction, communication or distribution in any
> form) by persons other than the designated recipient(s) is prohibited.
> If you have received this e-mail in error, please notify the sender
> either by telephone or by e-mail and delete the material from any
> computer.
> 
> Thank you for your cooperation."

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel +1 716 737-3463
-- http://www.husted.com/about/struts/

Mime
View raw message