You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Craig R. McClanahan" <Cr...@eng.sun.com> on 2000/07/09 07:39:01 UTC

Performing XSLT Transformations With Struts

Several questions have arisen about using XSL transformations in
conjunction with Struts, primarily for the purpose of separating content
and presentation in the "view" portion of the MVC architecture.
Although it is not directly related to Struts, there is another Jakarta
project called "jakarta-taglibs" which aims to accumulate useful freely
available custom tag libraries, and one of those tag libraries -- the
"xsl" library -- has recently been enhanced in ways that might be useful
to Struts users.

The key tags in this library are "include", which lets you call a
servlet that renders XML output, and then assign that output to a bean
in your JSP page (or apply a transformation to it directly), and
"apply", which performs the transformations.  Some examples of usage:

1)  A JSP page which itself renders XML, and you
    want to run it through a stylesheet:

    <xsl:apply xsl="/myStylesheet.xsl">
        ... XML tags in your JSP page ...
    </xsl:apply>

2)  A JSP page which imports XML data from an
     external source (such as another servlet in your
     application) and applies the stylesheet:

    <xsl:apply xsl="/myStylesheet.xsl">
        <xsl:include page="/my/servlet/that/generates/xml"/>
    </xsl:apply>

3)  A JSP page that applies a stylesheet to an XML
    data source (such as a DOM, a SAX input handler,
    or an input stream containing XML data) that has
    been stored as a bean already:

    <jsp:useBean id="data" scope="session"
      type="org.w3c.dom.Node"/>
    <xsl:apply nameXml="data" xsl="/myStylesheet.xsl"/>

These examples only scratch the surface of what is possible.  I
encourage those interested in XSL transformations to download the
"jakarta-taglibs" distribution <http://jakarta.apache.org/taglibs> and
experiment with these tags.  They are completely compatible with Struts,
although you will probably find yourself preferring to define separate
servlets that retrieve data (say, from a database based on selection
criteria specified as request parameters) and return it as XML text, in
addition to the controller servlet from Struts that manages the user
interaction.

The tags described above use the Xerces parser and Xalan XSLT engine,
which are available at <http://xml.apache.org> under the same license
terms that Struts is available.

Craig McClanahan



Re: Performing XSLT Transformations With Struts

Posted by Eduardo Pelegri--Llopart <Ed...@eng.sun.com>.
Another, complementary, point, is that you do not need to use XSLT for
simple transformations.  
One of the design criteria for XML was that it was very easy to process
(the operative description used to be "perl or awk"), so, if the only
thing you need is a simple extract functionality, using XSLT may be an
overkill (in both having to learn a new language, and in performance
cost).

Craig and I had been talking about adding a simple tag to
jakarta-taglibs that will target this type of usage.  It would be
targetted to processing an XML doc that is a stream of rows of data, and
it would be designed for ease of subclassing to be easy to extend.  This
tag is based on the experience of some users that had found that, for
some simple transformations, there was a very significant performance
advantage of this technique over a XSLT-based approach.

	- eduard/o

"Craig R. McClanahan" wrote:
> 
> Several questions have arisen about using XSL transformations in
> conjunction with Struts, primarily for the purpose of separating content
> and presentation in the "view" portion of the MVC architecture.
> Although it is not directly related to Struts, there is another Jakarta
> project called "jakarta-taglibs" which aims to accumulate useful freely
> available custom tag libraries, and one of those tag libraries -- the
> "xsl" library -- has recently been enhanced in ways that might be useful
> to Struts users.
> 
> The key tags in this library are "include", which lets you call a
> servlet that renders XML output, and then assign that output to a bean
> in your JSP page (or apply a transformation to it directly), and
> "apply", which performs the transformations.  Some examples of usage:
> 
> 1)  A JSP page which itself renders XML, and you
>     want to run it through a stylesheet:
> 
>     <xsl:apply xsl="/myStylesheet.xsl">
>         ... XML tags in your JSP page ...
>     </xsl:apply>
> 
> 2)  A JSP page which imports XML data from an
>      external source (such as another servlet in your
>      application) and applies the stylesheet:
> 
>     <xsl:apply xsl="/myStylesheet.xsl">
>         <xsl:include page="/my/servlet/that/generates/xml"/>
>     </xsl:apply>
> 
> 3)  A JSP page that applies a stylesheet to an XML
>     data source (such as a DOM, a SAX input handler,
>     or an input stream containing XML data) that has
>     been stored as a bean already:
> 
>     <jsp:useBean id="data" scope="session"
>       type="org.w3c.dom.Node"/>
>     <xsl:apply nameXml="data" xsl="/myStylesheet.xsl"/>
> 
> These examples only scratch the surface of what is possible.  I
> encourage those interested in XSL transformations to download the
> "jakarta-taglibs" distribution <http://jakarta.apache.org/taglibs> and
> experiment with these tags.  They are completely compatible with Struts,
> although you will probably find yourself preferring to define separate
> servlets that retrieve data (say, from a database based on selection
> criteria specified as request parameters) and return it as XML text, in
> addition to the controller servlet from Struts that manages the user
> interaction.
> 
> The tags described above use the Xerces parser and Xalan XSLT engine,
> which are available at <http://xml.apache.org> under the same license
> terms that Struts is available.
> 
> Craig McClanahan