xslt in jsp

JSP

1. Producing jsp from xslt
2. How to pass xml into a JSP page
3. JSP in XSL
4. XSLT Saxon Transformation in JSP pages

1.

Producing jsp from xslt

Mike Brown


> Hi.  I'm trying to transform XML into an HTML/JSP page.

Unfortunately, JSP is not HTML. When you rely on the XSLT processor to serialize the result tree in HTML syntax, it's going to make conservative decisions as to how to make abstract elements, attributes, and character data manifest as encoded text with embedded markup. The HTML it produces is intended to be processed by a conforming HTML user agent. A JSP engine is not such an agent.

The serializer does know that certain HTML attributes are subject to different kinds of formatting, but it has no way of knowing that instead of one of the standard types of attribute values, you want to insert a completely unescaped JSP tag.

> I've followed the suggestion at
http://www.dpawson.co.uk/xsl/sect2/N1553.html#d1670e229
> 
> using:
> 
> <xsl:attribute name="action" saxon:disable-output-escaping="yes"
xmlns:saxon="http://icl.com/saxon">
>   <xsl:text
>&lt;&#37;&#61;request.getRequestURI()&#37;&gt;
</xsl:text>
> </xsl:attribute>
>
> [...]
>
> This is what I get:
> 
>   <form method="get" action="<%25=request.getRequestURI()%25>">
>
> And of course I'd like:
> 
>   <form method="get" action="<%=request.getRequestURI()%>">

Ordinarily that method would work, but the "action" attribute of an HTML form element is one of those special attributes that is supposed to be a URI reference, so the serializer, thinking it is helping you produce a proper value, is turning "%" into "%25". This behavior is not required by the XSLT spec except for non-ASCII characters, and IMHO it should not be happening at all (e.g., what if you already had properly escaped such characters in your URI?)

So I would call this a bug in Saxon. saxon:disable-output-escaping="yes" should, IMHO, disable *all* escaping.


> <xsl:attribute name="action">
>   <xsl:text
disable-output-escaping="yes"
>&lt;&#37;&#61;request.getRequestURI()&#37;&gt;
</xsl:text>
> </xsl:attribute>

That method would definitely not work because xsl:attribute discards the text nodes that were created in its content and only uses their encapsulated text. Escaping only happens at serialization time. The d-o-e flag indicates you'd prefer the serializer to not escape a text node's encapsulated text upon output, but that only applies to the serialization of the text node; it doesn't change the node's encapsulated text at all.

2.

How to pass xml into a JSP page

Steve Muench


Passing a string to new InputSource() expects
the string to represent an URL.

What you want to do is:

<%

 String New_val = request.getParameter("packet");
 ByteArrayInputStream bais = 
        new ByteArrayInputStream(New_val.getBytes());
 InputSource is = new InputSource(bais);
   :


%>

This will feed the XML document in a string as an
InputStream

            

3.

JSP in XSL

Gareth Sylvester-Bradley


>  there is apparently an XML representation of JSP pages that
> uses proper XML tags.

The sun site describes the newer XML format for JSP.

http://www.abc2xml.com/wrox/chapters/2858_Chapter10/28581002.asp is a chapter of a Wrox book describing both old and new syntax.

Both of these were on the first page of a Google search for "JSP XML syntax".

4.

XSLT Saxon Transformation in JSP pages

Vincent Blondel

Something like this ...

<%@ page contentType="text/html"
   import="net.sf.saxon.value.StringValue,
      net.sf.saxon.trans.DynamicError,
      net.sf.saxon.functions.SystemProperty,
      net.sf.saxon.trans.DynamicError,
      net.sf.saxon.FeatureKeys,
      net.sf.saxon.Configuration,
      net.sf.saxon.trace.XSLTTraceListener,
      net.sf.saxon.TransformerFactoryImpl,

      javax.xml.parsers.*,
      org.w3c.dom.*,
      javax.xml.transform.*,
      javax.xml.transform.stream.*,
      java.io.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
     <html xmlns="http://www.w3.org/1999/xhtml"; lang="en" xml:lang="en">
       <head>
         <title>Title</title>
       </head>

       <body>
<%
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");

StreamSource xml = new StreamSource(new File("c:/temp/hello.xml"));
StreamSource xsl = new StreamSource(new File("c:/temp/hello.xsl"));

StreamResult result = new StreamResult(out);

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xsl);

transformer.transform(xml, result);
%>
       </body>
     </html>

and, great, it works .

Robert Koberg added :

You will probably want to create a Templates object to cache and create you Transformer off of that. You will get better performance. Also, before you write your own taglib, you might want to check out: apache If you don't like it, at least you can use it as a starting point.